在最新的python3.6中使用协程,启用gevent时,可能会出现警告,代码如下:
import gevent
from gevent import monkey
monkey.patch_all()
# monkey.patch_socket()
# monkey.patch_ssl()
import urllib.request
def run_task(url):
print("Visit --> %s" % url)
try:
response = urllib.request.urlopen(url)
data = response.read()
print("%d bytes received from %s" % (len(data), url))
except Exception as e:
print("程序运行错误", e)
if __name__ == '__main__':urls = ['https://github.com/', 'https://www.python.org/', 'http://www.cnblogs.com/']
greenlets = [gevent.spawn(run_task, url) for url in urls]
gevent.joinall(greenle