Python多处理PicklingError:不能选择<type‘>函数很抱歉,我不能用一个简单的例子来重现错误,而且我的代码太复杂了,无法发布。如果我用IPythonshell而不是普通的Python运行这个程序,事情就会很顺利。我查了一些以前关于这个问题的笔记。它们都是由使用池调用类函数中定义的函数引起的。但对我来说不是这样。Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 313, in _handle_tasks
put(task)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed如果有什么帮助我会很感激的。更新:在模块的顶层定义了函数I泡菜。尽管它调用了一个包含嵌套函数的函数。也就是说,f()打电话g()打电话h()它有一个嵌套函数i(),我打电话给你pool.apply_async(f). f(), g(), h()都是在顶层定义的。我用这个模式尝试了一个更简单的例子,但它还是起作用了。
3 回答
jeck猫
TA贡献1909条经验 获得超7个赞
multiprocessing
dill
dill
pathos
:
import osfrom multiprocessing import Poolimport dilldef run_dill_encoded(payload): fun, args = dill.loads(payload) return fun(*args)def apply_async(pool, fun, args): payload = dill.dumps((fun, args)) return pool.apply_async(run_dill_encoded, (payload,))if __name__ == "__main__": pool = Pool(processes=5) # asyn execution of lambda jobs = [] for i in range(10): job = apply_async(pool, lambda a, b: (a, b, a * b), (i, i + 1)) jobs.append(job) for job in jobs: print job.get() print # async execution of static method class O(object): @staticmethod def calc(): return os.getpid() jobs = [] for i in range(10): job = apply_async(pool, O.calc, ()) jobs.append(job) for job in jobs: print job.get()
添加回答
举报
0/150
提交
取消