1

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

4

1 回答 1

0

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

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