由于后台有 semaphore_tracker 个进程,多进程冻结

Multiprocessing freezing because of semaphore_tracker process in the background

我正在为一个项目使用 Python 多处理,有时进程会冻结,显然它发生的原因是我发现这个进程 运行 ps aux:

python -c from multiprocessing.semaphore_tracker import main;main(39)

更多信息:

问题:

根据 documentation:

On Unix using the spawn or forkserver start methods will also start a semaphore tracker process which tracks the unlinked named semaphores created by processes of the program.

为什么有人 想要 使用 spawn 启动方法让我不明白。这是 ms-windows 所必需的(非常聪明的)障碍,因为 OS 没有 fork 系统调用。

所以我怀疑 Pycharm 强制使用 forkserver 启动方法,因为它在内部使用多个线程,而标准 UNIX fork startmethod 不能很好地处理多线程程序。

从 shell 尝试 运行 您的项目。在类 UNIX 操作系统上,应该默认使用 fork 不需要信号量跟踪器进程的启动方法。