import queue,threading l=threading.Lock() class MyThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) global que self.queue = que def run(self): while True: if self.queue.empty(): break item = self.queue.get() if l.acquire(1): print( self.name,item) l.release() self.queue.task_done() return que = queue.Queue() for x in range(200): que.put(x) for x in range(3): t=MyThread() t.start() que.join()
python threading queue
时间: 2024-10-25 18:53:25