RuntimeError: release unlocked lock

是不是有小伙伴是这一样写的啊threading.Lock().acquire() ……threading.Lock().release()
因为threading.Lock()是创建了一个锁像,后面释放时,你是建立了一个新得锁对象,并且释放到那个新的对象。

class myThread(threading.Thread):
    def __init__(self, name, s):
        threading.Thread.__init__(self)
        self.s = s
        self.name = name

    def run(self):
        print("beigin ",self.name)
        threading.Lock().acquire()
        print_time(self.name,self.s)
        print("end ", self.s)
        threading.Lock().release()

所以加一个对象就行了

lock = threading.Lock()
class myThread(threading.Thread):
    def __init__(self, name, s):
        threading.Thread.__init__(self)
        self.s = s
        self.name = name

    def run(self):
        print("beigin ",self.name)
        lock.acquire()
        print_time(self.name,self.s)
        print("end ", self.s)
        lock.release()

你可能感兴趣的:(问题,python)