未找到atrribute的奇怪错误

时间:2017-08-05 15:30:48

标签: python python-3.x

从repl运行时有一段代码会在win 7上生成一个奇怪的错误

from multiprocessing import Process, Pool
import time
from urllib import request

def millis():
  return int(round(time.time() * 1000))

def http_get(url):
  start_time = millis()
  result = {"url": url, "data": request.urlopen(url, timeout=5).read()[:100]}
  print(url + " took " + str(millis() - start_time) + " ms")
  return result



start_time = millis()
if __name__ == "__main__":
  urls = ['http://www.google.com/', 'https://foursquare.com/', 'http://www.yahoo.com/', 'http://www.bing.com/', "https://www.yelp.com/"]
  pool = Pool(processes=5)
  results = pool.map(http_get, urls)
  print("\nTotal took " + str(millis() - start_time) + " ms\n")
  for result in results:
    print(result)

错误是

Traceback (most recent call last):
  File "c:\python36\lib\multiprocessing\process.py", line 249, in _bootstrap
    self.run()
  File "c:\python36\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "c:\python36\lib\multiprocessing\pool.py", line 108, in worker
    task = get()
  File "c:\python36\lib\multiprocessing\queues.py", line 345, in get
    return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'http_get' on <module '__main__' (built-in)>

我不知道我做错了什么。没有多处理它就像魅力一样

1 个答案:

答案 0 :(得分:1)

来自multiprocessing documentation

  

请注意

     

此程序包中的功能要求__main__模块可由子项导入。这在编程指南中有所涉及,但值得在此指出。这意味着某些示例(例如multiprocessing.pool.Pool示例)在交互式解释器中不起作用。