multiprocessing.pool 中的错误组参数目前必须为 None

Moh*_*med 6 python python-multiprocessing python-3.6 ubuntu-16.04

下面是我的 python 脚本。

import multiprocessing
# We must import this explicitly, it is not imported by the top-level
# multiprocessing module.
import multiprocessing.pool
import time

from random import randint


class NoDaemonProcess(multiprocessing.Process):
    # make 'daemon' attribute always return False
    def _get_daemon(self):
        return False
    def _set_daemon(self, value):
        pass
    daemon = property(_get_daemon, _set_daemon)

# We sub-class multiprocessing.pool.Pool instead of multiprocessing.Pool
# because the latter is only a wrapper function, not a proper class.
class MyPool(multiprocessing.pool.Pool):
    Process = NoDaemonProcess

def sleepawhile(t):
    print("Sleeping %i seconds..." % t)
    time.sleep(t)
    return t

def work(num_procs):
    print("Creating %i (daemon) workers and jobs in child." % num_procs)
    pool = multiprocessing.Pool(num_procs)

    result = pool.map(sleepawhile,
        [randint(1, 5) for x in range(num_procs)])

    # The following is not really needed, since the (daemon) workers of the
    # child's pool are killed when the child is terminated, but it's good
    # practice to cleanup after ourselves anyway.
    pool.close()
    pool.join()
    return result

def test():
    print("Creating 5 (non-daemon) workers and jobs in main process.")
    pool = MyPool(20)

    result = pool.map(work, [randint(1, 5) for x in range(5)])

    pool.close()
    pool.join()
    print(result)

if __name__ == '__main__':
    test()
Run Code Online (Sandbox Code Playgroud)

这是在 ubuntu 服务器中运行,我使用的是 python 3.6.7

我在 apt-get Upgrade 后正常工作,我收到错误如下

group argument must be None for now
Run Code Online (Sandbox Code Playgroud)

我面临的错误可能是什么。我应该改变Python版本吗?升级后我应该回滚更改吗?

编辑1

堆栈跟踪异常:-

Traceback (most recent call last):
  File "/src/mainapp.py", line 104, in bulkfun
    p = MyPool(20)
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 175, in __init__
    self._repopulate_pool()
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 236, in _repopulate_pool
    self._wrap_exception)
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 250, in _repopulate_pool_static
    wrap_exception)
  File "/usr/lib/python3.6/multiprocessing/process.py", line 73, in __init__
    assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now
Run Code Online (Sandbox Code Playgroud)

编辑2

该代码适用于 python2.7、python3.5 但如果我使用 python 3.6.7 运行,则会收到如下错误。

Creating 5 (non-daemon) workers and jobs in main process.
Traceback (most recent call last):
  File "multi.py", line 52, in <module>
    test()
  File "multi.py", line 43, in test
    pool = MyPool(5)
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 175, in __init__
    self._repopulate_pool()
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 236, in _repopulate_pool
    self._wrap_exception)
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 250, in _repopulate_pool_static
    wrap_exception)
  File "/usr/lib/python3.6/multiprocessing/process.py", line 73, in __init__
    assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now
Run Code Online (Sandbox Code Playgroud)

Mar*_*ech 2

同样在这里。
该代码适用于我的情况(python 3.6.7)。(/sf/answers/3722664501/

class NoDaemonProcess(multiprocessing.Process):
    @property
    def daemon(self):
        return False

    @daemon.setter
    def daemon(self, value):
        pass


class NoDaemonContext(type(multiprocessing.get_context())):
    Process = NoDaemonProcess

# We sub-class multiprocessing.pool.Pool instead of multiprocessing.Pool
# because the latter is only a wrapper function, not a proper class.
class MyPool(multiprocessing.pool.Pool):
    def __init__(self, *args, **kwargs):
        kwargs['context'] = NoDaemonContext()
        super(MyPool, self).__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

我认为这个问题来自process.py的变化(https://github.com/python/cpython/blob/8ca0fa9d2f4de6e69f0902790432e0ab2f37ba68/Lib/multiprocessing/process.py#L189