在下面的代碼中,我試圖創建一個沙盒主工作系統,其中工作人員中的全局變量更改不反映給其他工作人員。打印功能使多處理程序失敗
爲了實現這一點,每創建一個任務時都會創建一個新進程,並且爲了使執行並行,進程本身的創建由ThreadPoolExecutor
進行管理。
import time
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import Pipe, Process
def task(conn, arg):
conn.send(arg * 2)
def isolate_fn(fn, arg):
def wrapped():
parent_conn, child_conn = Pipe()
p = Process(target=fn, args=(child_conn, arg), daemon=True)
try:
p.start()
r = parent_conn.recv()
finally:
p.join()
return r
return wrapped
def main():
with ThreadPoolExecutor(max_workers=4) as executor:
pair = []
for i in range(0, 10):
pair.append((i, executor.submit(isolate_fn(task, i))))
# This function makes the program broken.
#
print('foo')
time.sleep(2)
for arg, future in pair:
if future.done():
print('arg: {}, res: {}'.format(arg, future.result()))
else:
print('not finished: {}'.format(arg))
print('finished')
main()
這個程序工作正常,直到我把print('foo')
函數放在循環中。如果該功能存在,某些任務仍未完成,更糟的是,該程序本身並未完成。
結果並不總是相同的,但下面是典型的輸出:
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
arg: 0, res: 0
arg: 1, res: 2
arg: 2, res: 4
not finished: 3
not finished: 4
not finished: 5
not finished: 6
not finished: 7
not finished: 8
not finished: 9
爲什麼這個節目如此脆弱?
我使用Python 3.4.5。
謝謝。我已經闡明瞭打印功能。 –