python gevent pool 异步请求使用
import time from gevent import monkey import gevent from gevent.pool import Pool monkey.patch_all() # 保证放在上面 import requests def f(_url: str): print('GET: %s' % _url) res = requests.get(_url) time.sleep(3) # 假装网络很慢 3秒才拿到数据 return res.text if __name__ == '__main__': start = time.time() p = Pool(2) result = [] apps = [ p.spawn(f, 'https://www.qq.com/'), p.spawn(f, 'https://www.qq.com/'), p.spawn(f, 'https://www.qq.com/'), ] gevent.joinall(apps) # 等待这些处理结束 end = time.time() cost = end - start print("花费了 %.3f 秒" % cost) gevent.joinall(apps) # gevent.wait() for i, g in enumerate(apps): print(f"返回值长度 {len(g.value)}")
D:\app\Miniconda3\envs\torch-learn\python.exe C:/Users/shuai2/PycharmProjects/torch-learn/tt.py
GET: https://www.qq.com/
GET: https://www.qq.com/
GET: https://www.qq.com/
花费了 6.401 秒
返回值长度 99430
返回值长度 99430
返回值长度 99430
Process finished with exit code 0
本文来自博客园,作者:一支小白,转载请注明原文链接:https://www.cnblogs.com/startnow/p/13693676.html