找不到 eventlet.green.urllib2 模块



我正在尝试在 http://eventlet.net/doc/examples.html 上运行第一个代码示例,webcrawler.py

#!/usr/bin/env python
"""
This is a simple web "crawler" that fetches a bunch of urls using a pool to
control the number of outbound connections. It has as many simultaneously open
connections as coroutines in the pool.
The prints in the body of the fetch function are there to demonstrate that the
requests are truly made in parallel.
"""
import eventlet
from eventlet.green import urllib2

urls = [
    "https://www.google.com/intl/en_ALL/images/logo.gif",
    "http://python.org/images/python-logo.gif",
    "http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif",
]

def fetch(url):
    print("opening", url)
    body = urllib2.urlopen(url).read()
    print("done with", url)
    return url, body

pool = eventlet.GreenPool(200)
for url, body in pool.imap(fetch, urls):
    print("got body from", url, "of length", len(body))

但是,这会导致

ModuleNotFoundError: No module named 'urllib2'

我正在使用事件版本0.21.0。此模块是否已在事件中移动?

这些例子似乎已经过时了(现在更是如此)。

对于 python 2,请使用以下命令:

import eventlet
from eventlet.green import urllib2 as request
request.urlopen(...)

对于 python 3,请使用以下命令:

import eventlet
from eventlet.green.urllib import request
request.urlopen(...)

我终于将我的构建系统(在Sublime Editor中)切换到Python 2而不是Python 3。现在它按预期运行:

('opening', 'https://www.google.com/intl/en_ALL/images/logo.gif')
('opening', 'http://python.org/images/python-logo.gif')
('opening', 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif')
('done with', 'https://www.google.com/intl/en_ALL/images/logo.gif')
('got body from', 'https://www.google.com/intl/en_ALL/images/logo.gif', 'of length', 8558)
('done with', 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif')
('done with', 'http://python.org/images/python-logo.gif')
('got body from', 'http://python.org/images/python-logo.gif', 'of length', 2549)
('got body from', 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif', 'of length', 1874)
[Finished in 0.8s]

顺便说一下,这表明绿色线程正在按预期异步运行。

eventlet.green包模仿Python stdlib模块层次结构。

Python2 有 urllib2 模块。Python3 有 urllib 包,其中包含详细的子模块。

一般思路:查看普通Python代码,更改阻塞模块导入eventlet.green版本或mod = eventlet.import_patched('mod')并享受。

最新更新