如何在Python2.7.10中使用多处理功能创建子进程而不让子进程与父进程共享资源?

时间:2019-03-14 16:56:19

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

我们正在尝试将python 2.7.10代码库从Windows迁移到Linux。我们最近发现,Python 2.7中的多处理库在Windows和Linux上的行为有所不同。我们已经找到许多类似this one的文章来描述问题,但是,我们无法在线找到针对Python 2.7的解决方案。 This is a fix针对Python 3.4中的此问题,但是,我们无法升级到Python 3.4。没有子级和父级共享内存的情况下,有什么方法可以在Linux上的Python 2.7中使用多处理?我们还可以使用有关在python 2.7中修改forking.py代码的指南,以确保子进程和父进程不会共享内存并进行写时复制。谢谢!

2 个答案:

答案 0 :(得分:1)

一个可能的解决方案是使用loky,该库提供Processfork-execpython2.7的实现。 fork-exec start方法的行为与spawn类似,在新生成的过程中使用新的解释器。该库主要旨在提供concurrent.futures API,但您可以使用mp = loky.backend.get_context()获得与multiprocessing相同的API。

from loky.backend import get_context
import multiprocessing as mp


def child_without_os():
    print("Hello from {}".format(os.getpid()))


def child_with_os():
    import os
    print("Hello from {}".format(os.getpid()))


if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser('Test loky backend')
    parser.add_argument('--use-fork', action="store_true",
                        help="Use start_method='fork' instead of 'loky'")
    parser.add_argument('--with-os', action="store_true",
                        help='Import os module in the child interpreter')
    args = parser.parse_args()

    # Only import os in the main module, this should fail if the interpreter is
    # not shared
    import os
    print("Main is {}".format(os.getpid()))
    if args.use_fork:
        ctx = mp
        print("Using fork context")
    else:
        ctx = get_context('loky_init_main')
        print("Using loky context")

    if args.with_os:
        target = child_with_os
    else:
        target = child_without_os

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

这给

# Use the default context, the child process has a copy-on-write interpreter
# state and can use the os module.
$ python2 test.py --use-fork
Main is 14630
Using fork context
Hello from 14633


# Use the loky context, the child process has a fresh interpreter
# state and need to import the os module.
$ python2 test.py
Main is 14661
Using loky context
Process LokyInitMainProcess-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 267, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/home/tom/Work/prog/loky/test.py", line 6, in child_without_os
    print("Hello from {}".format(os.getpid()))
NameError: global name 'os' is not defined


# Now using the correct child function which import the os module
$ python2 test.py --with-os
Main is 14700
Using loky context
Hello from 14705

(免责声明:我是loky的维护者之一)。

答案 1 :(得分:0)

毫无疑问,CPython bug跟踪器中的补丁不适用于Python 2.7的多处理版本,并且补丁包含processing的一些额外功能,因此可以清除信号量然后适当地调整。

我认为您最好的选择是从Python 3反向移植多处理模块。将Python代码复制过来,将其重命名为sys.executable,发现缺少的C功能并加以解决(例如,清理自己的C功能)信号量或不使用它们)。尽管库很大,但仅移植您使用的功能可能很简单。如果您能够发布反向端口,那么我相信很多人都会对该项目感兴趣。

取决于您对多处理的依赖程度,一个不同的选择是通过使用subprocess模块运行 return redirect()->guest('auth.signin'); 来运行更多的Python。