Python多处理冻结forkbarm

2024-09-29 02:20:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的Python脚本:

import multiprocessing


def foo():
    print('running foo')


def main():
    print('start')
    ctx = multiprocessing.get_context('spawn')

    p = ctx.Process(target=foo)
    p.start()
    p.join()


if __name__ == '__main__':
    main()

当使用python解释器调用时,它的运行方式完全正确:

^{pr2}$

另一方面,尝试冻结脚本就不那么好了。在

两者兼而有之

pyinstaller test.py

以及

cxfreeze test.py

结果实际上是一个叉形炸弹:

$ ./dist/test/test
start
start
start
start
.
.
.

在htop中,我们可以看到,确实有许多进程被派生出来,机器很快就会锁定。在

将start方法更改为fork而不是spawn,不会导致叉形炸弹。在

ctx = multiprocessing.get_context('fork')

冷冻过程中如何处理好fork而不是产卵?冻结过程可以改变以允许繁殖吗?在


Tags: pytest脚本getfoomaindefcontext
1条回答
网友
1楼 · 发布于 2024-09-29 02:20:36

我设法用^{} script in the cpython repo重现了这个问题,因此这个问题确实比cxu freeze或pyinstaller更深。在

经过进一步的挖掘,我发现我并不是唯一一个有问题的人——以下问题是在我发现问题前一个月才公开的:

https://bugs.python.org/issue32146

他们有一些修复的想法,希望能在3.7.x版本中使用。在

相关问题 更多 >