如何使Pool.map_async可以访问函数

时间:2012-10-20 16:54:56

标签: python map pool

我有一个带有用户定义函数run的简单脚本,我希望使用Pool.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,但我不明白这实际意味着什么,我该怎么做才能解决这个问题。请指教。

1 个答案:

答案 0 :(得分:0)

在调用run()之前,只需在脚本中添加一行:

if __name__=="__main__":
    run(0, 50)