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