1
我在Python(3.4)中嘗試使用多線程,並對以下代碼有疑問。調用task_done()時發生死鎖,因爲任務已被新線程拉動
當我的工作量大於NUM_WORKER_THREADS
時,代碼工作正常;然而,一旦隊列縮小到小於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)
謝謝 - 我給一個旋轉。 –