关于多进程的问题, 序列化出错。 2周了啊,土豆都发芽了,跪求进程大神。
以下代码,报错内容为:_pickle.PicklingError: Can't pickle <class '__main__.DuoJincheng'>: attribute lookup DuoJincheng on __main__ failed希望大神能解答:是什么问题导致这个错误的呢, 该如何解决这个问题呢?
我换了multiprocessing,Pool模块, 也是同样的报错, 跪求了。
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import Manager,Process,Pool
class DuoJincheng():
def __init__(self):
# self.shuju =
pass
def jisuan(self,x):
print(f'我的进程ID是:{multiprocessing.current_process().pid}')
a = x*x
return a
def main(self):
with Manager() as manager:#多进程需要放在这个里面,数据才能共享。
list01 = manager.list()#设置的共享
with ProcessPoolExecutor() as pool: #在数据块中创建进程池,方便数据共享。
result = pool.map(self.jisuan,)
print(result)
for i in result:
print(i)
if __name__=='__main__':
duojincheng = DuoJincheng()
duojincheng.main() 给个完整的报错文本吧,我直接copy你的程序运行是没问题的
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=================== RESTART: C:\Users\lightninng\Desktop\1.py ==================
<generator object _chain_from_iterable_of_lists at 0x000002A77D8263C8>
0
1
4
9
16
25
36
49
64
81
>>> lightninng 发表于 2021-11-21 17:41
给个完整的报错文本吧,我直接copy你的程序运行是没问题的
你应该是mac电脑吧, 我是windows的,详细报错信息如下:
concurrent.futures.process._RemoteTraceback:
"""
Traceback (most recent call last):
File "D:\Python\lib\multiprocessing\queues.py", line 245, in _feed
obj = _ForkingPickler.dumps(obj)
File "D:\Python\lib\multiprocessing\reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <class '__main__.DuoJincheng'>: attribute lookup DuoJincheng on __main__ failed
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)# execute the script
File "D:\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "F:/python123/迭代器/diedai.py", line 25, in <module>
duojincheng.main()
File "F:/python123/迭代器/diedai.py", line 20, in main
for i in result:
File "D:\Python\lib\concurrent\futures\process.py", line 559, in _chain_from_iterable_of_lists
for element in iterable:
File "D:\Python\lib\concurrent\futures\_base.py", line 608, in result_iterator
yield fs.pop().result()
File "D:\Python\lib\concurrent\futures\_base.py", line 438, in result
return self.__get_result()
File "D:\Python\lib\concurrent\futures\_base.py", line 390, in __get_result
raise self._exception
File "D:\Python\lib\multiprocessing\queues.py", line 245, in _feed
obj = _ForkingPickler.dumps(obj)
File "D:\Python\lib\multiprocessing\reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <class '__main__.DuoJincheng'>: attribute lookup DuoJincheng on __main__ failed 从入门到富豪 发表于 2021-11-21 20:21
你应该是mac电脑吧, 我是windows的,详细报错信息如下:
concurrent.futures.process._RemoteTraceback: ...
我是windows 10 x64可能确实和系统有点关系?
lightninng 发表于 2021-11-21 22:31
我是windows 10 x64可能确实和系统有点关系?
我也是win10的 X64呀, 为啥你的能正常运行啊, 我太崩了, 我是pycharm 的IDE , python是3.10版本的。 从入门到富豪 发表于 2021-11-22 10:27
我也是win10的 X64呀, 为啥你的能正常运行啊, 我太崩了, 我是pycharm 的IDE , python是3.10版本的。
我是py3.7,如果是我,我可能会换个版本试一下,最新版本说不定会因为库的兼容性问题导致bug。 lightninng 发表于 2021-11-22 12:42
我是py3.7,如果是我,我可能会换个版本试一下,最新版本说不定会因为库的兼容性问题导致bug。
我看了一下, 需要用dill替换到python本身的序列化模块
页:
[1]