多处理中的类变量 - python

时间:2016-04-23 23:08:55

标签: python class multiprocessing dill

这是我的代码:

import multiprocessing
import dill

class Some_class():

    class_var = 'Foo'

    def __init__(self, param):
        self.name = param

    def print_name(self):

        print("we are in object "+self.name)
        print(Some_class.class_var)

def run_dill_encoded(what):
    fun, args = dill.loads(what)
    return fun(*args)


def apply_async(pool, fun, args):
    return pool.apply_async(run_dill_encoded, (dill.dumps((fun, args)),))


if __name__ == '__main__':

    list_names = [Some_class('object_1'), Some_class('object_2')]

    pool = multiprocessing.Pool(processes=4)
    results = [apply_async(pool, Some_class.print_name, args=(x,)) for x in list_names]
    output = [p.get() for p in results]
    print(output)

它返回错误:

multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "C:\Python34\lib\multiprocessing\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "C:\...\temp_obj_output_standard.py", line 18, in run_dill_encoded
    return fun(*args)
  File "C:/...temp_obj_output_standard.py", line 14, in print_name
    print(Some_class.class_var)
NameError: name 'Some_class' is not defined
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/...temp_obj_output_standard.py", line 31, in <module>
    output = [p.get() for p in results]
  File "C:/...temp_obj_output_standard.py", line 31, in <listcomp>
    output = [p.get() for p in results]
  File "C:\Python34\lib\multiprocessing\pool.py", line 599, in get
    raise self._value
NameError: name 'Some_class' is not defined

Process finished with exit code 1

代码在没有行print(Some_class.class_var)的情况下正常工作。访问类变量有什么问题,两个对象都应该拥有它,我不认为进程应该与它发生冲突。我错过了什么吗? 有关如何排除故障的任何建议吗?不要担心run_dill_encodedapply_async,我正在使用此解决方案,直到我在Python 3.x上编译multiprocess

P.S。这已经足够了,但是stackoverflow希望我提供更多细节,而不是确定要放什么。

0 个答案:

没有答案