0

这是我的代码:

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

该代码无需 line 即可正常工作print(Some_class.class_var)。访问类变量有什么问题,两个对象都应该有它,我不认为进程应该对此发生冲突。我错过了什么吗?关于如何解决它的任何建议?不用担心run_dill_encodedand ,在我在 Python 3.x 上apply_async编译之前,我一直在使用这个解决方案。multiprocess

PS这已经足够了,但是stackoverflow要我放更多细节,不太确定放什么。

4

0 回答 0