Python中两个多进程之间共享列表

发布于 2025-01-10 14:48:54 字数 1734 浏览 0 评论 0原文

我正在学习如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈年往事 2025-01-17 14:48:54

在 ms-windows 和 macOS 上,多处理附带了一些额外的编程指南。

由于多处理必须在这些平台上工作,因此您的脚本必须可安全导入
这意味着在 if __name__ == "__main__": 块之外,理想情况下应该只具有导入、函数和类定义。

而且您当然不应该尝试在该块之外实例化任何 multiprocessing.Processmultiprocessing.Poolmultiprocessing.Manager 类!

考虑一下当您在 "__main__" 块之外创建一个 Process 时会发生什么:

  1. 您启动运行脚本的 Python。
  2. 您启动一个 Process
  3. Python 启动一个新的 Python 解释器,它会导入您的脚本。
  4. 这将返回到步骤 2。

如果不选中,这将用 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:

  1. You start up Python that runs your script.
  2. You start a Process
  3. Python starts a new Python interpreter, which imports your script.
  4. Which returns you to step 2.

Unchecked, this would fill your machine's memory up with Python processes. Hence the RuntimeError.

淡墨 2025-01-17 14:48:54

您的代码没问题,只需注意 文档 因为参数应该是一个元组而不是一个 list()

尝试改变这一行(注意参数后面的逗号):

l1 = mp.Process(target=update_array_list,args=(datalist,))
l2 = mp.Process(target=display_array_list,args=(datalist,))
$ python3 multi.py
loading list array with 1 elements : [1]
_________________________________________
Items in array [1] 
Items in array [1] 
Items in array [1, 2] 
loading list array with 2 elements : [1, 2]
_________________________________________
Items in array [1, 2] 
Items in array [1, 2, 3] 
loading list array with 3 elements : [1, 2, 3]
_________________________________________
Items in array [1, 2, 3] 
Items in array [1, 2, 3, 4] 
loading list array with 4 elements : [1, 2, 3, 4]
_________________________________________
Items in array [1, 2, 3, 4] 
Items in array [1, 2, 3, 4] 
loading list array with 5 elements : [1, 2, 3, 4, 5]
_________________________________________

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):

l1 = mp.Process(target=update_array_list,args=(datalist,))
l2 = mp.Process(target=display_array_list,args=(datalist,))
$ python3 multi.py
loading list array with 1 elements : [1]
_________________________________________
Items in array [1] 
Items in array [1] 
Items in array [1, 2] 
loading list array with 2 elements : [1, 2]
_________________________________________
Items in array [1, 2] 
Items in array [1, 2, 3] 
loading list array with 3 elements : [1, 2, 3]
_________________________________________
Items in array [1, 2, 3] 
Items in array [1, 2, 3, 4] 
loading list array with 4 elements : [1, 2, 3, 4]
_________________________________________
Items in array [1, 2, 3, 4] 
Items in array [1, 2, 3, 4] 
loading list array with 5 elements : [1, 2, 3, 4, 5]
_________________________________________

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文