1

runPool.map_async を使用して並列に数回実行したいユーザー定義関数を含む単純なスクリプトがあります。試してみると、次のエラーが表示されます。

def getKey(where, key):
    found = re.search('<td.*?>%s:</td>.*?<td>(.*?)</td>' % key, where, re.DOTALL).group(1)
    return re.sub('<[^>]*?>', "", found).strip()


def extract(sitename):
    text = urllib.urlopen(sitename).read()
    return getKey(text, 'Name')


def run(start, span):
    links = get(start, span)
    if (len(links) == 0): return

    pool = Pool(span)
    pool.map_async(extract, links).get()
    pool.close()
    pool.join()

    run(start + span, span)

run(0, 50)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed 

http://docs.python.org/library/multiprocessing.htmlに記載されていますが、それFunctionality within this package requires that the __main__ module be importable by the childrenが実際に何を意味するのか、この問題を解決するにはどうすればよいのかわかりません。ご意見をお聞かせください。

4

1 に答える 1

0

run() を呼び出す前に、スクリプトに 1 行追加するだけです。

if __name__=="__main__":
    run(0, 50)
于 2012-10-31T13:36:26.410 に答える