Python使用多处理构建字典

Python使用多处理构建字典,python,dictionary,tkinter,multiprocess,Python,Dictionary,Tkinter,Multiprocess,我是多处理模块的初学者。在我的代码中,我试图用给定路径的图像构建一个字典。我编写了以下代码: 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 = (

我是多处理模块的初学者。在我的代码中,我试图用给定路径的图像构建一个字典。我编写了以下代码:

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”:0x7f6b66507190处的PIL.ImageTk.PhotoImage对象。。。 等等}

。。。但我得到了以下错误:

`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,)'`

如何解决这个问题?

多处理不是线程。这是一个完全独立的过程,有自己的解释器。这有一些优点-您不会意外地创建共享可变状态,这很好!但它也有一些缺点——这是其中之一

传递给多处理进程或从多处理进程传递的所有数据结构都必须序列化/反序列化。因此,该函数的返回值,在幕后,必须进行pickle处理——正如您所看到的,这是不可能的


在当前的设计中,使用线程而不是多处理。

不应该
name=(path.split('/')[1])。split('.')[0]
be
os.path.splitext(os.path.split(path)[1])[0]
来获取文件名而不带扩展名?确保函数返回可pickle的对象可能是最简单的方法。注意,您的代码缺少很多细节,我对tkinter不太熟悉,但请看哪一个与之相关。