Я даю Python (3.4) попытку с многопоточным чтением и задаю вопрос по следующему коду.Тупик при вызове task_done(), когда задача была вытащена новым потоком
Код работает хорошо, когда у меня больше работы, чем NUM_WORKER_THREADS
; однако после того, как очередь сжимается до менее чем , новые итерации могут принимать один и тот же элемент из-за времени между items.get()
и вызовом task_done
. Это приводит к тупику при вызове task_done
.
Каков правильный способ справиться с этим?
import time
import threading
from queue import Queue
NUM_WORKER_THREADS = 8
def worker():
try:
while items.qsize() > 0:
print("{} items left to process".format(items.qsize()))
item = items.get()
print("Processing {}".format(item))
itemrec = getItemRecord(item) # external call to webservice ~3 second response.
items.task_done()
except Exception as inst:
print("---------------EXCEPTION OCCURRED----------------")
print(type(inst))
print(inst.args)
print(inst)
# start counter to monitor performance
start = time.perf_counter()
items = Queue()
# get the items we need to work on for allocations
searchResults = getSearchResults() # external call to webservice
# add results of search to a collection
for itemid in searchResults:
if itemid['recordtype'] == 'inventoryitem':
items.put(itemid['id'])
for i in range(NUM_WORKER_THREADS):
try:
t = threading.Thread(target=worker)
t.daemon = True
t.start()
except Exception as inst:
print("---------------EXCEPTION OCCURRED----------------")
print(type(inst))
print(inst.args)
print(inst)
items.join()
# print end of execution performance counter
print('time:', time.perf_counter() - start)
Спасибо - я дам, что спина. –