本文主要介紹在網絡請求中的同步與異步,以及異步的表現形式: 回調與協程,並經過python代碼展現各自的優缺點。javascript
1 def http_blockway(url1, url2): 2 import urllib2 as urllib 3 import time 4 begin = time.time() 5 data1 = urllib.urlopen(url1).read() 6 data2 = urllib.urlopen(url2).read() 7 print len(data1), len(data2) 8 print 'http_blockway cost', time.time() - begin 9 10 url_list = [ 'http://xlambda.com/gevent-tutorial/','https://www.bing.com'] 11 if __name__ == '__main__': 12 http_blockway(*url_list)
45706 121483
http_blockway cost 2.22000002861html
1 import tornado 2 from tornado.httpclient import AsyncHTTPClient 3 import time, sys 4 5 def http_callback_way(url1, url2): 6 http_client = AsyncHTTPClient() 7 begin = time.time() 8 count = [0] 9 def handle_result(response, url): 10 print('%s : handle_result with url %s' % (time.time(), url)) 11 count[0] += 1 12 if count[0] == 2: 13 print 'http_callback_way cost', time.time() - begin 14 sys.exit(0) 15 16 http_client.fetch(url1,lambda res, u = url1:handle_result(res, u)) 17 print('%s here between to request' % time.time()) 18 http_client.fetch(url2,lambda res, u = url2:handle_result(res, u)) 19 20 url_list = [ 'http://xlambda.com/gevent-tutorial/','https://www.bing.com'] 21 if __name__ == '__main__': 22 http_callback_way(*url_list) 23 tornado.ioloop.IOLoop.instance().start()
1487292402.45 here between to request
1487292403.09 : handle_result with url http://xlambda.com/gevent-tutorial/
1487292403.21 : handle_result with url https://www.bing.com
http_callback_way cost 0.759999990463java
1 import tornado 2 from tornado.httpclient import AsyncHTTPClient 3 http_client = AsyncHTTPClient() 4 def handle_request_final(response): 5 print('finally we got the result, do sth here') 6 7 def handle_request_first(response, another1, another2): 8 if response.error or 'some word' in response.body: 9 target = another1 10 else: 11 target = another2 12 http_client.fetch(target, handle_request_final) 13 14 def http_callback_way(url, another1, another2): 15 http_client.fetch(url, lambda res, u1 = another1, u2 = another2:handle_request_first(res, u1, u2)) 16 17 url_list = [ 'https://www.baidu.com', 'https://www.google.com','https://www.bing.com'] 18 if __name__ == '__main__': 19 http_callback_way(*url_list) 20 tornado.ioloop.IOLoop.instance().start()
1 import tornado, sys, time 2 from tornado.httpclient import AsyncHTTPClient 3 from tornado import gen 4 5 def http_generator_way(url1, url2): 6 begin = time.time() 7 count = [0] 8 @gen.coroutine 9 def do_fetch(url): 10 http_client = AsyncHTTPClient() 11 response = yield http_client.fetch(url, raise_error = False) 12 print url, response.error 13 count[0] += 1 14 if count[0] == 2: 15 print 'http_generator_way cost', time.time() - begin 16 sys.exit(0) 17 18 do_fetch(url1) 19 do_fetch(url2) 20 21 url_list = [ 'http://xlambda.com/gevent-tutorial/','https://www.bing.com'] 22 if __name__ == '__main__': 23 http_generator_way(*url_list) 24 tornado.ioloop.IOLoop.instance().start()
運行結果:python
http://xlambda.com/gevent-tutorial/ None
https://www.bing.com None
http_generator_way cost 1.05999994278程序員
1 def http_coroutine_way(url1, url2): 2 import gevent, time 3 from gevent import monkey 4 monkey.patch_all() 5 begin = time.time() 6 7 def looks_like_block(url): 8 import urllib2 as urllib 9 data = urllib.urlopen(url).read() 10 print url, len(data) 11 12 gevent.joinall([gevent.spawn(looks_like_block, url1), gevent.spawn(looks_like_block, url2)]) 13 print('http_coroutine_way cost', time.time() - begin) 14 15 url_list = [ 'http://xlambda.com/gevent-tutorial/','https://www.bing.com'] 16 if __name__ == '__main__': 17 http_coroutine_way(*url_list)
referencesgolang
callbackhellweb
gevent調度流程解析promise
tornado網絡