【发布时间】:2020-08-18 01:36:37
【问题描述】:
我只是想看看windows上多处理的简单代码实现,但它既不进入/运行函数,也不在jupyternotebook或运行保存的.py中p>
import time
import multiprocessing
s=[1,4]
def subu(remo):
s[remo-1]=remo*9
print(f'here{remo}')
return
if __name__=="__main__":
p1=multiprocessing.Process(target=subu , args=[1])
p2=multiprocessing.Process(target=subu , args=[2])
p1.start()
p2.start()
p1.join()
p2.join()
# print("2222here")
print(s)
input()
.py 的输出是:
[1, 4]
[1, 4]
jupyternotebook 的输出是:
[1,4]
我希望是这样的:
here1
here2
[9,18]
上面的代码有什么问题?那么这段代码呢:
import concurrent
thread_num=2
s=[1,4]
def subu(remo):
s[remo-1]=remo*9
print(f'here{remo}')
return
with concurrent.futures.ProcessPoolExecutor() as executor:
## or if __name__=="__main__":
##... with concurrent.futures.ProcessPoolExecutor() as executor:
results=[executor.submit(subu,i) for i in range(thread_num)]
for f in concurrent.futures.as_completed(results):
print(f.result())
input()
在 jupyter pull 错误中根本不运行
BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
我知道我不能指望 jupyter 运行多处理。但 saved.py 也无法运行它。它不等待输入()就退出了
【问题讨论】:
-
在 linux 的命令行上运行我得到了希望的结果。无法重现。
-
好的,编辑那个,不太理想。
here1\nhere2\n[1,4]。 [9,18] 不会发生,因为父进程看不到子进程内存(写时复制或独立进程)。 -
@tdelaney 那么如何通过多处理对父级应用更改,以及'here1'和'here2'呢,为什么它们不会发生?
-
我不是一个普通的 jupyter 用户,但只是四处寻找这个question 建议将工作人员放在一个单独的模块中,以便它可以由子进程导入。
-
至于父级中的更改,您需要某种方式从进程与父级进行通信。
multiprocessing.queue()可以是那个频道。而multiprocessing.Pool也可以。由于我不知道jupyter部分,我不确定回答。