[英]Python building a dictionary with multiprocessing
我是多處理模塊的初學者。 在我的代碼中,我試圖用給定路徑中的圖像構建字典。 我寫了以下代碼:
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,)'`
我該如何解決?
多重處理不是線程化。 它是一個完全獨立的過程,帶有自己的解釋器。 這有一些優點-您不能意外創建共享可變狀態,這很棒! 盡管它有一些缺點-這是其中之一。
傳入或傳出多處理過程的所有數據結構都必須進行序列化/反序列化。 因此,必須在后台隱藏該函數的返回值-正如您所看到的那樣,實際上不能。
在您當前的設計中,請使用Threading而不是Multiprocessing。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.