标签:lse close div ons git web def callback rgs
gevent:mokey.path_all (找到所有socket,进行异步IO)
gevent+requests=》grequests
Twisted: reactor.run() 死循环
Tornado
=》gevent > Twisted > Tornado > asyncio
www.cnblogs.com/wupeiqi/articles/6229292.html
""" import gevent import requests from gevent import monkey monkey.patch_all() def task(method, url, req_kwargs): print(method, url, req_kwargs) response = requests.request(method=method, url=url, **req_kwargs) print(response.url, response.content) # ##### 发送请求 ##### # gevent.joinall([ # gevent.spawn(task, method=‘get‘, url=‘https://www.python.org/‘, req_kwargs={}), # gevent.spawn(task, method=‘get‘, url=‘https://www.yahoo.com/‘, req_kwargs={}), # gevent.spawn(task, method=‘get‘, url=‘https://github.com/‘, req_kwargs={}), # ]) # ##### 发送请求(协程池控制最大协程数量) ##### from gevent.pool import Pool pool = Pool(5) gevent.joinall([ pool.spawn(task, method=‘get‘, url=‘https://www.python.org/‘, req_kwargs={}), pool.spawn(task, method=‘get‘, url=‘https://www.yahoo.com/‘, req_kwargs={}), pool.spawn(task, method=‘get‘, url=‘https://www.github.com/‘, req_kwargs={}), ]) """ import grequests import grequests request_list = [ grequests.get(‘http://httpbin.org/delay/1‘, timeout=0.001), grequests.get(‘http://fakedomain/‘), grequests.get(‘http://httpbin.org/status/500‘) ] # ##### 执行并获取响应列表 ##### response_list = grequests.map(request_list,size=5) print(response_list)
#!/usr/bin/env python # -*- coding:utf-8 -*- from twisted.internet import defer from twisted.web.client import getPage from twisted.internet import reactor def one_done(arg): print(arg) def all_done(arg): print(‘done‘) reactor.stop() @defer.inlineCallbacks def task(url): res = getPage(bytes(url, encoding=‘utf8‘)) # 发送Http请求 res.addCallback(one_done) yield res url_list = [ ‘http://www.cnblogs.com‘, ‘http://www.cnblogs.com‘, ‘http://www.cnblogs.com‘, ‘http://www.cnblogs.com‘, ] defer_list = [] # [特殊,特殊,特殊(已经向url发送请求)] for url in url_list: v = task(url) defer_list.append(v) d = defer.DeferredList(defer_list) d.addBoth(all_done) reactor.run() # 死循环
#!/usr/bin/env python # -*- coding:utf-8 -*- from tornado.httpclient import AsyncHTTPClient from tornado.httpclient import HTTPRequest from tornado import ioloop COUNT = 0 def handle_response(response): global COUNT COUNT -= 1 if response.error: print("Error:", response.error) else: print(response.body) # 方法同twisted # ioloop.IOLoop.current().stop() if COUNT == 0: ioloop.IOLoop.current().stop() def func(): url_list = [ ‘http://www.baidu.com‘, ‘http://www.bing.com‘, ] global COUNT COUNT = len(url_list) for url in url_list: print(url) http_client = AsyncHTTPClient() http_client.fetch(HTTPRequest(url), handle_response) ioloop.IOLoop.current().add_callback(func) ioloop.IOLoop.current().start() # 死循环
标签:lse close div ons git web def callback rgs
原文地址:https://www.cnblogs.com/jintian/p/11406558.html