我想在Python中使用多處理庫中的隊列。在執行下面的代碼之後(打印語句正常工作),但在我調用加入隊列並且仍然存在之後,進程不會退出。我怎樣才能終止剩餘的進程?Python中的多處理隊列
謝謝!
def MultiprocessTest(self):
print "Starting multiprocess."
print "Number of CPUs",multiprocessing.cpu_count()
num_procs = 4
def do_work(message):
print "work",message ,"completed"
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = multiprocessing.JoinableQueue()
for i in range(num_procs):
p = multiprocessing.Process(target=worker)
p.daemon = True
p.start()
source = ['hi','there','how','are','you','doing']
for item in source:
q.put(item)
print "q close"
q.join()
#q.close()
print "Finished everything...."
print "num active children:",multiprocessing.active_children()
你有沒有任何理由在完成後將None放入隊列中?我認爲task_done()可以幫助避免這個問題?我試圖在本頁面底部的示例之後對代碼進行建模:http://docs.python.org/library/queue.html – aerain
這實際上不工作:( – aerain
未對解決方案進行評級,但暗示如何讓它運行:在def worker的第一次使用之前移動「q =」聲明行()... ;-) – Dilettant