Python:Queue

来自WHY42
imported>Soleverlee2016年12月14日 (三) 02:40的版本 (以“<source lang="python"> def worker(): while True: item = q.get() if item is None: break do_work(item) q.task_done() q...”为内容创建页面)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
def worker():
    while True:
        item = q.get()
        if item is None:
            break
        do_work(item)
        q.task_done()

q = queue.Queue()
threads = []
for i in range(num_worker_threads):
    t = threading.Thread(target=worker)
    t.start()
    threads.append(t)

for item in source():
    q.put(item)

# block until all tasks are done
q.join()

# stop workers
for i in range(num_worker_threads):
    q.put(None)
for t in threads:
    t.join()

The origin post could be found here