关于使用gevent.monkey.patch_all()协程池遇到阻塞解决办法:
import time
...
class Example:
...
time.sleep(0.000001)
在进行多线程时monkey会阻塞住线程的继续执行,需要对monkey.patch_all进行处理, 在实例中添加一个sleep()可以解决,这里时间可以设置一个非常小的数就可以了~
import gevent.monkey
gevent.monkey.patch_all()
......
class Example:
......
def run(self): # 实现做主要逻辑
# 1. 准备url
self.get_url_list()
# 2. 遍历发送请求,获取响应
for i in range(5): # 设置并发数为5
self.pool.apply_async(self._execute_request_content_save, callback=self._callback)
while True:
time.sleep(0.0000001)
if self.total_response_num >= self.total_request_num:
self.is_running = False
break
对于这个问题可以访问stackoverflow具体看一下:
https://stackoverflow.com/questions/9192539/using-gevent-monkey-patching-with-threading-makes-thread-work-serially