我試圖爲了我自己的目的實現這個多處理tutorial。起初我認爲它不能很好地擴展,但是當我做出一個可重複的例子時,我發現如果項目列表超過124,它似乎永遠不會返回答案。在x = 124
它運行在0.4秒,但是當我將它設置爲x = 125
它永遠不會結束。我在Windows 7上運行Python 2.7。Python多處理> = 125列表永遠不會完成
from multiprocessing import Lock, Process, Queue, current_process
import time
class Testclass(object):
def __init__(self, x):
self.x = x
def toyfunction(testclass):
testclass.product = testclass.x * testclass.x
return testclass
def worker(work_queue, done_queue):
try:
for testclass in iter(work_queue.get, 'STOP'):
print(testclass.counter)
newtestclass = toyfunction(testclass)
done_queue.put(newtestclass)
except:
print('error')
return True
def main(x):
counter = 1
database = []
while counter <= x:
database.append(Testclass(10))
counter += 1
print(counter)
workers = 8
work_queue = Queue()
done_queue = Queue()
processes = []
start = time.clock()
counter = 1
for testclass in database:
testclass.counter = counter
work_queue.put(testclass)
counter += 1
print(counter)
print('items loaded')
for w in range(workers):
p = Process(target=worker, args=(work_queue, done_queue))
p.start()
processes.append(p)
work_queue.put('STOP')
for p in processes:
p.join()
done_queue.put('STOP')
newdatabase = []
for testclass in iter(done_queue.get, 'STOP'):
newdatabase.append(testclass)
print(time.clock()-start)
print("Done")
return(newdatabase)
if __name__ == '__main__':
database = main(124)
database2 = main(125)
它適合我。 – Veedrac
他在這裏發佈了另一個對我有效的問題。 http://stackoverflow.com/questions/19070638/python-multiprocessing-ioerror-errno-232-the-pipe-is-being-closed#comment28188856_19070638 –
我解決了另一篇文章中的問題。我試過在兩臺計算機上運行上面的代碼,並且它在'x = 125'結束時掛起,並且從不打印完成。 – Michael