Python中两个多进程之间共享列表
我正在学习如何在 Python 中使用 multiprocessing
模块,并按照在线教程尝试我自己的代码。这是我的代码,
import multiprocessing as mp
import time
manager = mp.Manager()
def update_array_list(datalist):
for i in range(1,10):
datalist.append(i)
print(f"loading list array with {i} elements : {datalist}")
print("_________________________________________")
time.sleep(2)
def display_array_list(datalist):
while True:
print(f"Items in array {datalist} ")
time.sleep(1)
if __name__ == '__main__':
datalist = manager.list()
datalist.clear()
l1 = mp.Process(target=update_array_list,args=(datalist))
l2 = mp.Process(target=display_array_list,args=(datalist))
l1.start()
l2.start()
l1.join()
l2.join()
我的 l1
进程以两秒的间隔更新共享列表 datalist
,而我的 l2
进程以 1 秒的间隔打印它们。
我的代码无法运行,尽管这与教程几乎相同。
我收到以下错误。
...............
_check_not_importing_main()
File "C:\Users\pgooneti\Anaconda3\envs\NOUS\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
is not going to be frozen to produce an executable.''')
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
这里有什么错误?
I am learning how to use the multiprocessing
module in Python and experimenting with my own codes following online tutorials. Here is my code
import multiprocessing as mp
import time
manager = mp.Manager()
def update_array_list(datalist):
for i in range(1,10):
datalist.append(i)
print(f"loading list array with {i} elements : {datalist}")
print("_________________________________________")
time.sleep(2)
def display_array_list(datalist):
while True:
print(f"Items in array {datalist} ")
time.sleep(1)
if __name__ == '__main__':
datalist = manager.list()
datalist.clear()
l1 = mp.Process(target=update_array_list,args=(datalist))
l2 = mp.Process(target=display_array_list,args=(datalist))
l1.start()
l2.start()
l1.join()
l2.join()
My l1
process updates shared list datalist
at two seconds intervals while my l2
process print them at 1-second intervals.
My code does not run, although this is nearly the same as tutorials.
I get the following error.
...............
_check_not_importing_main()
File "C:\Users\pgooneti\Anaconda3\envs\NOUS\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
is not going to be frozen to produce an executable.''')
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
What is the error here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 ms-windows 和 macOS 上,
多处理
附带了一些额外的编程指南。由于
多处理
必须在这些平台上工作,因此您的脚本必须可安全导入。这意味着在
if __name__ == "__main__":
块之外,理想情况下应该只具有导入、函数和类定义。而且您当然不应该尝试在该块之外实例化任何
multiprocessing.Process
、multiprocessing.Pool
、multiprocessing.Manager
类!考虑一下当您在
"__main__"
块之外创建一个Process
时会发生什么:Process
如果不选中,这将用 Python 进程填满您的计算机内存。因此出现了
运行时错误
。On ms-windows and macOS,
multiprocessing
comes with some extra programming guidelines.Due to the way
multiprocessing
has to work on those platforms, your script has to be safely importable.That means that outside the
if __name__ == "__main__":
block, you should ideally only have imports, function and class definitions.And you should certainly not try to instantiate any of the
multiprocessing.Process
,multiprocessing.Pool
,multiprocessing.Manager
classes outside of that block!Consider what happens when you create a
Process
outside of the"__main__"
block:Process
Unchecked, this would fill your machine's memory up with Python processes. Hence the
RuntimeError
.您的代码没问题,只需注意 文档 因为参数应该是一个元组而不是一个 list()
尝试改变这一行(注意参数后面的逗号):
Your code is Ok, just be aware of documentation because the args shoul be a tuple and not a list()
Try chage this lines (Note the comma after the argument):