2017-12-08 8 views
1

私はマルチプロセッシングモジュールの初心者です。私のコードでは、指定されたパスから画像を含む辞書を作成しようとしています。コードが正常に動作する場合、私は、次のような何か期待するPythonはマルチプロセッシングで辞書を構築しています

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': PIL.ImageTk.PhotoImage object at 0x7f6b66507150,

'-23': PIL.ImageTk.PhotoImage object at 0x7f6b66507190, ... and so on}

を...代わりに、私は次のエラーを取得:

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

どうすればこの問題を解決できますか?

+1

'name =(path.split( '/')[1])。split( '。')[0]' be 'os.path.splitext(os.path.split(path)[ 1])[0] '拡張子なしでファイル名を取得するには? – Ivonet

+1

関数がpickleableであるオブジェクトを返すのがおそらく最も簡単な方法であることを確認してください。 –

+1

あなたのコードには詳細がたくさんありますが、私はtkinterに精通していませんが、[この質問](https://stackoverflow.com/questions/21173582/cannot-pool-map-function-because-of)を参照してください。 -unpickleableerror)は関連性のあるように見えます。 –

答えて

2

マルチプロセッシングはスレッド化ではありません。それはそれ自身の通訳者と全く別のプロセスです。これにはいくつかの利点があります。間違って共有可能な可変状態を作成することはできません。これにはいくつかの欠点がありますが、これもその一つです。

マルチプロセッシングプロセスとの間でやりとりされるすべてのデータ構造は、シリアル化/デシリアライズする必要があります。したがって、その機能の戻り値は、舞台裏で、ピクルスされなければなりません。あなたが見ることができるように、それはできません。

現在のデザインでは、マルチプロセッシングではなくスレッド化を使用しています。

関連する問題