简体   繁体   English

找不到arrribute的奇怪错误

[英]weird error with atrribute not found

Have a piece of code when run from the repl generates an weird error on win 7 从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)

And the error is 错误是

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)>

I have no clue what i am doing wrong .Without multiprocessing it works like charm 我不知道我在做什么错。没有多重处理,它就像魅力一样

From the multiprocessing documentation : 多处理文档中

Note 注意

Functionality within this package requires that the __main__ module be importable by the children. 此软件包中的功能要求__main__模块可由子级导入。 This is covered in Programming guidelines however it is worth pointing out here. 编程指南中对此进行了介绍,但是这里值得指出。 This means that some examples, such as the multiprocessing.pool.Pool examples will not work in the interactive interpreter. 这意味着某些示例(例如multiprocessing.pool.Pool示例)在交互式解释器中将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM