- 这是我写的两个有关的博客,建议如果觉得下面的代码比较费解,可以先看下面的文章
Python并发Gevent库(一)
AttributeError: module ‘select’ has no attribute ‘error’解决方法
相比于 AttributeError: module ‘select’ has no attribute ‘error’解决方法 这篇文章,这里我加了一句print("End of File")
这样之后输出就是成了下面这样子
GET: https://www.python.org/
GET: https://www.yahoo.com/
GET: https://github.com/
51558 bytes received from https://github.com/.
48866 bytes received from https://www.python.org/.
507204 bytes received from https://www.yahoo.com/.
End of File
可以看出: 在文章末尾,调用完所有的join的协程之后,才会到下面的过程。也就是平时所说的,阻塞
住了~
from gevent import monkey
monkey.patch_all(select=False)
import requests
import gevent
def f(url):
print('GET: %s' % url)
data = requests.get(url).text
print('%d bytes received from %s.' % (len(data), url))
gevent.joinall([
gevent.spawn(f, 'https://www.python.org/'),
gevent.spawn(f, 'https://www.yahoo.com/'),
gevent.spawn(f, 'https://github.com/'),
])
print("End of File")