multiprocessing.pool 中的错误组参数现在必须为 None [英] Error group argument must be None for now in multiprocessing.pool

查看:101
本文介绍了multiprocessing.pool 中的错误组参数现在必须为 None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的python脚本.

Below is my python script.

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()

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

This is running in ubuntu server and i'm using python 3.6.7

我在 apt-get upgrade 后正常工作我收到错误

I had this working properly after apt-get upgrade Im getting error as

group argument must be None for now

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

What might be the error that I'm facing. Should i change the python version. Should I roll back the changes after upgrading.

编辑 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

编辑 2

代码适用于python2.7、python3.5但是,如果我使用 python 3.6.7 运行,则会出现如下错误.

The code works for python2.7, python3.5 But if i run with python 3.6.7 i got the error as below.

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

推荐答案

我在将 Travis 发行版从 14.04 升级到 16.04 时遇到了这个问题,并且 python 3.6 开始失败.我找到了这个问题的解决方案,因为它是对另一个包的修复 - FIX: Python 2.7-3.7.1 兼容 NonDaemonPool

I came across to this issue while upgrading Travis distribution from 14.04 to 16.04 and python 3.6 started to fail. I have found a solution to this problem as it was a fix to another package - FIX: Python 2.7-3.7.1 compatible NonDaemonPool

class NonDaemonPool(multiprocessing.pool.Pool):
    def Process(self, *args, **kwds):
        proc = super(NonDaemonPool, self).Process(*args, **kwds)

        class NonDaemonProcess(proc.__class__):
            """Monkey-patch process to ensure it is never daemonized"""
            @property
            def daemon(self):
                return False

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

        proc.__class__ = NonDaemonProcess
        return proc

这篇关于multiprocessing.pool 中的错误组参数现在必须为 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆