我是多处理模块的初学者。在我的代码中,我试图用给定路径的图像构建一个字典。我编写了以下代码:
from multiprocessing import Pool
from PIL import Image, ImageTk
import glob
def process(path):
print path
im=ImageTk.PhotoImage(Image.open(path).resize((600, 600), Image.ANTIALIAS))
name = (path.split('/')[1]).split('.')[0]
return (name, im)
p = Pool(4)
input = glob.glob('./*.jpg')
image_list = dict(p.map(process, input))如果代码工作正常,那么我希望如下所示:
{'-22':0x7f6b66507150处的PIL.ImageTk.PhotoImage对象, '-23':PIL.ImageTk.PhotoImage对象在0x7f6b66507190,等等}
..。但是,我得到了以下错误:
`multiprocessing.pool.MaybeEncodingError: Error sending result:`
`'[('-51', PIL.ImageTk.PhotoImage object at 0x7f6b664f6990),`
`('-47', PIL.ImageTk.PhotoImage object at 0x7f6b664f6fd0),`
`('-54', PIL.ImageTk.PhotoImage object at 0x7f6b66507050),`
`('-13', PIL.ImageTk.PhotoImage object at 0x7f6b665070d0),`
`('-45', PIL.ImageTk.PhotoImage object at 0x7f6b66507110),`
`('-49', PIL.ImageTk.PhotoImage object at 0x7f6b66507150),`
`('-48', PIL.ImageTk.PhotoImage object at 0x7f6b66507190),`
`('-26', PIL.ImageTk.PhotoImage object at 0x7f6b665071d0),`
`('-10', PIL.ImageTk.PhotoImage object at 0x7f6b66507210)]'.`
`Reason: 'UnpickleableError(tkapp object at 0x7f6b67c88e30,)'`我怎么才能解决这个问题?
发布于 2017-12-09 01:45:44
多处理不是线程处理。它是一个完全独立的进程,有自己的解释器。这有一些优点-您不能意外地创建共享可变状态,这是很好的!不过它也有一些缺点--这就是其中之一。
传递到或从多处理进程传递的所有数据结构都必须序列化/反序列化。因此,这个函数的返回值,在幕后,必须被腌制--正如你所看到的,它不可能。
在您当前的设计中,使用线程而不是多重处理。
https://stackoverflow.com/questions/47720439
复制相似问题