【问题标题】:error with module multiprocessing under python3.8python3.8下的模块多处理错误
【发布时间】:2021-01-11 19:46:43
【问题描述】:

直到今天,我的脚本还可以进行多处理。为了重现该问题,我简化了与下面显示的函数并行化的函数:

    from multiprocessing import Process, Queue
    import random

    def rand_num():
        num = random.random()
        print(num)

    if __name__ == "__main__":
        queue = Queue()

        processes = [Process(target=rand_num, args=()) for x in range(4)]

        for p in processes:
            p.start()

        for p in processes:
            p.join()

呈现完全相同的错误消息(重复 4 次,为了便于阅读,我省略了重复):

    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
    File "/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
    File "/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
    File "/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
    File "/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 262, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
    File "/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 232, in _get_code_from_file
    with io.open_code(fname) as f:
    FileNotFoundError: [Errno 2] No such file or directory: '/Users/myUserName/<stdin>'

我不知道从哪里开始调试这个错误。我在mac os Catalina(自制安装)下运行python3.8。请帮忙。

【问题讨论】:

  • 顺便说一句,您应该考虑使用 Pool,它将简化代码。
  • 你是从文件 - python script.py - 还是从解释器运行它?我看到 File "&lt;string&gt;"&lt;stdin&gt; 这可能意味着你从解释器运行它,也许它会出现问题。
  • Python 3.8 是非常新的版本,它可能存在错误,并且某些模块可能不适用于此版本。最好等待几个月并使用旧版本 3.7。
  • MacOS 上的 Python 3.8 现在默认使用“spawn”而不是“fork”作为新进程的启动方法。尝试在if __name__ == "__main__": 下方的第一行中使用multiprocessing.set_start_method("fork")
  • 感谢@Darkonaut!我遇到了类似的问题,基本上我的代码在升级到 3.8 后就崩溃了……你应该写一篇中等的帖子;)——并在下面添加一个答案,以便我们投票

标签: python python-3.x python-multiprocessing


【解决方案1】:

从 Python 3.7 升级到 3.8 时,我遇到了同样的问题。 特别是现在在 OSX 10.15.6 上运行 3.8.6,Python 由 pyenv 安装。

Darkonaut 的建议帮助解决了这个问题,但它并不那么明显,所以让我在这里重新表述一下:

MacOS 上的 Python 3.8 现在默认使用 spawn 而不是 fork 作为新进程的启动方法。试试

multiprocessing.set_start_method("fork")

显然,spawn 的行为是错误的,如下简单示例所示:

import multiprocessing

def parallel_function(x):
    print("Function called with", x)

def test_pool():
    print("Running test_pool")
    with multiprocessing.Pool(4) as pool:
        pool.map(parallel_function, range(10))

print("Starting the test")
test_pool()

这会产生以下输出:

Starting the test
Running test_pool
Starting the test
Running test_pool
Starting the test
Running test_pool
Starting the test
Running test_pool
Starting the test
Running test_pool
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/karel/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/Users/karel/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/Users/karel/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/Users/karel/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path

因此,Pool 不会正确创建工作程序,而是尝试在每个生成的进程中运行整个脚本。

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 2011-03-09
    • 2018-10-19
    • 1970-01-01
    • 2015-03-05
    • 2011-04-04
    • 2017-12-11
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多