python---爬虫相关性能(各个异步模块的使用,和自定义异步IO模块)
一:线程池,进程池等相关文章了解
python---基础知识回顾(十)进程和线程(py2中自定义线程池和py3中的线程池使用)
python---基础知识回顾(十)进程和线程(协程gevent:线程在I/O请求上的优化)
python---异步IO(asyncio)协程
二:异步I/O模块的了解(同协程:线程在I/O请求上的优化)
(一)asyncio模块的使用
1.使用简单实例
import asyncio,time async def func1(num): print(num,"before func1.....") await asyncio.sleep(num) print(num,"after func1......") tasks = [ asyncio.ensure_future(func1(1)), asyncio.ensure_future(func1(3)), ] begin = time.time() loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.gather(*tasks)) loop.close() end = time.time() print(end-begin)
1 before func1..... 3 before func1..... 1 after func1...... 3 after func1...... 3.003171682357788
(二).异步获取网站资源(不支持requests模块),使用asyncio模块的socket请求完成
不支持requests请求的原因:
异步的实现,需要自动切换协程,需要用到await或者yield,
而requests模块不提供切换协程的条件,所以当第一条协程执行到请求部分就停下来了,等服务器响应,
不会切到其他的协程去执行。
requests会阻塞asyncio循环,所以会出现收不到服务器响应的情况。可以使用aiohttp替代requests解决这个问题。
使用asyncio实现requests请求获取资源
import asyncio,time async def func1(host,url='/'): print(host,url) reader, writer = await asyncio.open_connection(host,80) request_header_content = '''GET %s HTTP/1.0\r\nHost: %s\r\n\r\n'''%(url,host) request_header_content = bytes(request_header_content,encoding="utf-8") writer.write(request_header_content) await writer.drain() text = await reader.read() print(host,url,text) writer.close() tasks = [ asyncio.ensure_future(func1('www.cnblogs.com','/ssyfj/')), asyncio.ensure_future(func1('www.baidu.com')), asyncio.ensure_future(func1('www.ckook.com','/forum.php')), ] begin = time.time() loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.gather(*tasks)) loop.close() end = time.time() print(end-begin)
www.cnblogs.com /ssyfj/ www.baidu.com / www.ckook.com /forum.php www.baidu.com / b'HTTP/1.0 200 OK\r\nAccept-Ranges: bytes\r\nCache-Control: no-cache\r\nContent-Length: 14615\r\nContent-Type: text/html\r\nDate: Sun, 24 Jun 2018 10:57:33 GMT\r\nLast-Modified: Mon, 11 Jun 2018 11:19:00 GMT\r\nP3p: CP=" OTI DSP COR IVA OUR IND COM "\r\nPragma: no-cache\r\nServer: BWS/1.1\r\nSet-Cookie: BAIDUID=3C070A482979333DD407E21A39ECA46A:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com\r\nSet-Cookie: BIDUPSID=3C070A482979333DD407E21A39ECA46A; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com\r\nSet-Cookie: PSTM=1529837853; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com\r\nVary: Accept-Encoding\r\nX-Ua-Compatible: IE=Edge,chrome=1\r\n\r\n<!DOCTYPE html><!--STATUS OK-->\r\n<html>\r\n<head>\r\n\t<meta http-equiv="content-type" content="text/html;charset=utf-8">\r\n\t<meta http-equiv="X-UA-Compatible" content="IE=Edge">\r\n\t<link rel="dns-prefetch" href="//s1.bdstatic.com"/>\r\n\t<link rel="dns-prefetch" href="//t1.baidu.com"/>\r\n\t<link rel="dns-prefetch" href="//t2.baidu.com"/>\r\n\t<link rel="dns-prefetch" href="//t3.baidu.com"/>\r\n\t<link rel="dns-prefetch" href="//t10.baidu.com"/>\r\n\t<link rel="dns-prefetch" href="//t11.baidu.com"/>\r\n\t<link rel="dns-prefetch" href="//t12.baidu.com"/>\r\n\t<link rel="dns-prefetch" href="//b1.bdstatic.com"/>\r\n\t<title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title>\r\n\t<link href="http://s1.bdstatic.com/r/www/cache/static/home/css/index.css" rel="stylesheet" type="text/css" />\r\n\t<!--[if lte IE 8]><style index="index" >#content{height:480px\\9}#m{top:260px\\9}</style><![endif]-->\r\n\t<!--[if IE 8]><style index="index" >#u1 a.mnav,#u1 a.mnav:visited{font-family:simsun}</style><![endif]-->\r\n\t<script>var hashMatch = document.location.href.match(/#+(.*wd=[^&].+)/);if (hashMatch && hashMatch[0] && hashMatch[1]) {document.location.replace("http://"+location.host+"/s?"+hashMatch[1]);}var ns_c = function(){};</script>\r\n\t<script>function h(obj){obj.style.behavior=\'url(#default#homepage)\';var a = obj.setHomePage(\'//www.baidu.com/\');}</script>\r\n\t<noscript><meta http-equiv="refresh" content="0; url=/baidu.html?from=noscript"/></noscript>\r\n\t<script>window._ASYNC_START=new Date().getTime();</script>\r\n</head>\r\n<body link="#0000cc"><div id="wrapper" style="display:none;"><div id="u"><a href="//www.baidu.com/gaoji/preferences.html" onmousedown="return user_c({\'fm\':\'set\',\'tab\':\'setting\',\'login\':\'0\'})">\xe6\x90\x9c\xe7\xb4\xa2\xe8\xae\xbe\xe7\xbd\xae</a>|<a id="btop" href="/" onmousedown="return user_c({\'fm\':\'set\',\'tab\':\'index\',\'login\':\'0\'})">\xe7\x99\xbe\xe5\xba\xa6\xe9\xa6\x96\xe9\xa1\xb5</a>|<a id="lb" href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" onclick="return false;" onmousedown="return user_c({\'fm\':\'set\',\'tab\':\'login\'})">\xe7\x99\xbb\xe5\xbd\x95</a><a href="https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" onmousedown="return user_c({\'fm\':\'set\',\'tab\':\'reg\'})" target="_blank" class="reg">\xe6\xb3\xa8\xe5\x86\x8c</a></div><div id="head"><div class="s_nav"><a href="/" class="s_logo" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'logo\'})"><img src="//www.baidu.com/img/baidu_jgylogo3.gif" width="117" height="38" border="0" alt="\xe5\x88\xb0\xe7\x99\xbe\xe5\xba\xa6\xe9\xa6\x96\xe9\xa1\xb5" title="\xe5\x88\xb0\xe7\x99\xbe\xe5\xba\xa6\xe9\xa6\x96\xe9\xa1\xb5"></a><div class="s_tab" id="s_tab"><a href="http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=" wdfield="word" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'news\'})">\xe6\x96\xb0\xe9\x97\xbb</a> <b>\xe7\xbd\x91\xe9\xa1\xb5</b> <a href="http://tieba.baidu.com/f?kw=&fr=wwwt" wdfield="kw" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'tieba\'})">\xe8\xb4\xb4\xe5\x90\xa7</a> <a href="http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=&fr=wwwt" wdfield="word" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'zhidao\'})">\xe7\x9f\xa5\xe9\x81\x93</a> <a href="http://music.baidu.com/search?fr=ps&key=" wdfield="key" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'music\'})">\xe9\x9f\xb3\xe4\xb9\x90</a> <a href="http://image.baidu.com/i?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&word=" wdfield="word" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'pic\'})">\xe5\x9b\xbe\xe7\x89\x87</a> <a href="http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=" wdfield="word" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'video\'})">\xe8\xa7\x86\xe9\xa2\x91</a> <a href="http://map.baidu.com/m?word=&fr=ps01000" wdfield="word" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'map\'})">\xe5\x9c\xb0\xe5\x9b\xbe</a> <a href="http://wenku.baidu.com/search?word=&lm=0&od=0" wdfield="word" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'wenku\'})">\xe6\x96\x87\xe5\xba\x93</a> <a href="//www.baidu.com/more/" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'more\'})">\xe6\x9b\xb4\xe5\xa4\x9a\xc2\xbb</a></div></div><form id="form" name="f" action="/s" class="fm" ><input type="hidden" name="ie" value="utf-8"><input type="hidden" name="f" value="8"><input type="hidden" name="rsv_bp" value="1"><span class="bg s_ipt_wr"><input name="wd" id="kw" class="s_ipt" value="" maxlength="100"></span><span class="bg s_btn_wr"><input type="submit" id="su" value="\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b" class="bg s_btn" onmousedown="this.className=\'bg s_btn s_btn_h\'" onmouseout="this.className=\'bg s_btn\'"></span><span class="tools"><span id="mHolder"><div id="mCon"><span>\xe8\xbe\x93\xe5\x85\xa5\xe6\xb3\x95</span></div><ul id="mMenu"><li><a href="javascript:;" name="ime_hw">\xe6\x89\x8b\xe5\x86\x99</a></li><li><a href="javascript:;" name="ime_py">\xe6\x8b\xbc\xe9\x9f\xb3</a></li><li class="ln"></li><li><a href="javascript:;" name="ime_cl">\xe5\x85\xb3\xe9\x97\xad</a></li></ul></span><span class="shouji"><strong>\xe6\x8e\xa8\xe8\x8d\x90 : </strong><a href="http://w.x.baidu.com/go/mini/8/10000020" onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'bdbrowser\'})">\xe7\x99\xbe\xe5\xba\xa6\xe6\xb5\x8f\xe8\xa7\x88\xe5\x99\xa8\xef\xbc\x8c\xe6\x89\x93\xe5\xbc\x80\xe7\xbd\x91\xe9\xa1\xb5\xe5\xbf\xab2\xe7\xa7\x92\xef\xbc\x81</a></span></span></form></div><div id="content"><div id="u1"><a href="http://news.baidu.com" name="tj_trnews" class="mnav">\xe6\x96\xb0\xe9\x97\xbb</a><a href="http://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a><a href="http://map.baidu.com" name="tj_trmap" class="mnav">\xe5\x9c\xb0\xe5\x9b\xbe</a><a href="http://v.baidu.com" name="tj_trvideo" class="mnav">\xe8\xa7\x86\xe9\xa2\x91</a><a href="http://tieba.baidu.com" name="tj_trtieba" class="mnav">\xe8\xb4\xb4\xe5\x90\xa7</a><a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" id="lb" onclick="return false;">\xe7\x99\xbb\xe5\xbd\x95</a><a href="//www.baidu.com/gaoji/preferences.html" name="tj_settingicon" id="pf">\xe8\xae\xbe\xe7\xbd\xae</a><a href="//www.baidu.com/more/" name="tj_briicon" id="bri">\xe6\x9b\xb4\xe5\xa4\x9a\xe4\xba\xa7\xe5\x93\x81</a></div><div id="m"><p id="lg"><img src="//www.baidu.com/img/bd_logo.png" width="270" height="129"></p><p id="nv"><a href="http://news.baidu.com">\xe6\x96\xb0 \xe9\x97\xbb</a>\xe3\x80\x80<b>\xe7\xbd\x91 \xe9\xa1\xb5</b>\xe3\x80\x80<a href="http://tieba.baidu.com">\xe8\xb4\xb4 \xe5\x90\xa7</a>\xe3\x80\x80<a href="http://zhidao.baidu.com">\xe7\x9f\xa5 \xe9\x81\x93</a>\xe3\x80\x80<a href="http://music.baidu.com">\xe9\x9f\xb3 \xe4\xb9\x90</a>\xe3\x80\x80<a href="http://image.baidu.com">\xe5\x9b\xbe \xe7\x89\x87</a>\xe3\x80\x80<a href="http://v.baidu.com">\xe8\xa7\x86 \xe9\xa2\x91</a>\xe3\x80\x80<a href="http://map.baidu.com">\xe5\x9c\xb0 \xe5\x9b\xbe</a></p><div id="fm"><form id="form1" name="f1" action="/s" class="fm"><span class="bg s_ipt_wr"><input type="text" name="wd" id="kw1" maxlength="100" class="s_ipt"></span><input type="hidden" name="rsv_bp" value="0"><input type=hidden name=ch value=""><input type=hidden name=tn value="baidu"><input type=hidden name=bar value=""><input type="hidden" name="rsv_spt" value="3"><input type="hidden" name="ie" value="utf-8"><span class="bg s_btn_wr"><input type="submit" value="\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b" id="su1" class="bg s_btn" onmousedown="this.className=\'bg s_btn s_btn_h\'" onmouseout="this.className=\'bg s_btn\'"></span></form><span class="tools"><span id="mHolder1"><div id="mCon1"><span>\xe8\xbe\x93\xe5\x85\xa5\xe6\xb3\x95</span></div></span></span><ul id="mMenu1"><div class="mMenu1-tip-arrow"><em></em><ins></ins></div><li><a href="javascript:;" name="ime_hw">\xe6\x89\x8b\xe5\x86\x99</a></li><li><a href="javascript:;" name="ime_py">\xe6\x8b\xbc\xe9\x9f\xb3</a></li><li class="ln"></li><li><a href="javascript:;" name="ime_cl">\xe5\x85\xb3\xe9\x97\xad</a></li></ul></div><p id="lk"><a href="http://baike.baidu.com">\xe7\x99\xbe\xe7\xa7\x91</a>\xe3\x80\x80<a href="http://wenku.baidu.com">\xe6\x96\x87\xe5\xba\x93</a>\xe3\x80\x80<a href="http://www.hao123.com">hao123</a><span> | <a href="//www.baidu.com/more/">\xe6\x9b\xb4\xe5\xa4\x9a>></a></span></p><p id="lm"></p></div></div><div id="ftCon"><div id="ftConw"><p id="lh"><a id="seth" onClick="h(this)" href="/" onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'homepage\',\'pos\':0})">\xe6\x8a\x8a\xe7\x99\xbe\xe5\xba\xa6\xe8\xae\xbe\xe4\xb8\xba\xe4\xb8\xbb\xe9\xa1\xb5</a><a id="setf" href="//www.baidu.com/cache/sethelp/index.html" onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'favorites\',\'pos\':0})" target="_blank">\xe6\x8a\x8a\xe7\x99\xbe\xe5\xba\xa6\xe8\xae\xbe\xe4\xb8\xba\xe4\xb8\xbb\xe9\xa1\xb5</a><a onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'tj_about\'})" href="http://home.baidu.com">\xe5\x85\xb3\xe4\xba\x8e\xe7\x99\xbe\xe5\xba\xa6</a><a onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'tj_about_en\'})" href="http://ir.baidu.com">About Baidu</a></p><p id="cp">©2018 Baidu <a href="/duty/" name="tj_duty">\xe4\xbd\xbf\xe7\x94\xa8\xe7\x99\xbe\xe5\xba\xa6\xe5\x89\x8d\xe5\xbf\x85\xe8\xaf\xbb</a> \xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7 <img src="http://s1.bdstatic.com/r/www/cache/static/global/img/gs_237f015b.gif"></p></div></div><div id="wrapper_wrapper"></div></div><div class="c-tips-container" id="c-tips-container"></div>\r\n<script>window.__async_strategy=2;</script>\r\n<script>var bds={se:{},su:{urdata:[],urSendClick:function(){}},util:{},use:{},comm : {domain:"http://www.baidu.com",ubsurl : "http://sclick.baidu.com/w.gif",tn:"baidu",queryEnc:"",queryId:"",inter:"",templateName:"baidu",sugHost : "http://suggestion.baidu.com/su",query : "",qid : "",cid : "",sid : "",indexSid : "",stoken : "",serverTime : "",user : "",username : "",loginAction : [],useFavo : "",pinyin : "",favoOn : "",curResultNum:"",rightResultExist:false,protectNum:0,zxlNum:0,pageNum:1,pageSize:10,newindex:0,async:1,maxPreloadThread:5,maxPreloadTimes:10,preloadMouseMoveDistance:5,switchAddMask:false,isDebug:false,ishome : 1},_base64:{domain : "http://b1.bdstatic.com/",b64Exp : -1,pdc : 0}};var name,navigate,al_arr=[];var selfOpen = window.open;eval("var open = selfOpen;");var isIE=navigator.userAgent.indexOf("MSIE")!=-1&&!window.opera;var E = bds.ecom= {};bds.se.mon = {\'loadedItems\':[],\'load\':function(){},\'srvt\':-1};try {bds.se.mon.srvt = parseInt(document.cookie.match(new RegExp("(^| )BDSVRTM=([^;]*)(;|$)"))[2]);document.cookie="BDSVRTM=;expires=Sat, 01 Jan 2000 00:00:00 GMT"; }catch(e){}</script>\r\n<script>if(!location.hash.match(/[^a-zA-Z0-9]wd=/)){document.getElementById("ftCon").style.display=\'block\';document.getElementById("u1").style.display=\'block\';document.getElementById("content").style.display=\'block\';document.getElementById("wrapper").style.display=\'block\';setTimeout(function(){try{document.getElementById("kw1").focus();document.getElementById("kw1").parentNode.className += \' iptfocus\';}catch(e){}},0);}</script>\r\n<script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/static/jquery/jquery-1.10.2.min_f2fb5194.js"></script>\r\n<script>(function(){var index_content = $(\'#content\');var index_foot= $(\'#ftCon\');var index_css= $(\'head [index]\');var index_u= $(\'#u1\');var result_u= $(\'#u\');var wrapper=$("#wrapper");window.index_on=function(){index_css.insertAfter("meta:eq(0)");result_common_css.remove();result_aladdin_css.remove();result_sug_css.remove();index_content.show();index_foot.show();index_u.show();result_u.hide();wrapper.show();if(bds.su&&bds.su.U&&bds.su.U.homeInit){bds.su.U.homeInit();}setTimeout(function(){try{$(\'#kw1\').get(0).focus();window.sugIndex.start();}catch(e){}},0);if(typeof initIndex==\'function\'){initIndex();}};window.index_off=function(){index_css.remove();index_content.hide();index_foot.hide();index_u.hide();result_u.show();result_aladdin_css.insertAfter("meta:eq(0)");result_common_css.insertAfter("meta:eq(0)");result_sug_css.insertAfter("meta:eq(0)");wrapper.show();};})();</script>\r\n<script>window.__switch_add_mask=1;</script>\r\n<script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/static/global/js/instant_search_newi_redirect1_20bf4036.js"></script>\r\n<script>initPreload();$("#u,#u1").delegate("#lb",\'click\',function(){try{bds.se.login.open();}catch(e){}});if(navigator.cookieEnabled){document.cookie="NOJS=;expires=Sat, 01 Jan 2000 00:00:00 GMT";}</script>\r\n<script>$(function(){for(i=0;i<3;i++){u($($(\'.s_ipt_wr\')[i]),$($(\'.s_ipt\')[i]),$($(\'.s_btn_wr\')[i]),$($(\'.s_btn\')[i]));}function u(iptwr,ipt,btnwr,btn){if(iptwr && ipt){iptwr.on(\'mouseover\',function(){iptwr.addClass(\'ipthover\');}).on(\'mouseout\',function(){iptwr.removeClass(\'ipthover\');}).on(\'click\',function(){ipt.focus();});ipt.on(\'focus\',function(){iptwr.addClass(\'iptfocus\');}).on(\'blur\',function(){iptwr.removeClass(\'iptfocus\');}).on(\'render\',function(e){var $s = iptwr.parent().find(\'.bdsug\');var l = $s.find(\'li\').length;if(l>=5){$s.addClass(\'bdsugbg\');}else{$s.removeClass(\'bdsugbg\');}});}if(btnwr && btn){btnwr.on(\'mouseover\',function(){btn.addClass(\'btnhover\');}).on(\'mouseout\',function(){btn.removeClass(\'btnhover\');});}}});</script>\r\n<script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/static/home/js/bri_7f1fa703.js"></script>\r\n<script>(function(){var _init=false;window.initIndex=function(){if(_init){return;}_init=true;var w=window,d=document,n=navigator,k=d.f1.wd,a=d.getElementById("nv").getElementsByTagName("a"),isIE=n.userAgent.indexOf("MSIE")!=-1&&!window.opera;(function(){if(/q=([^&]+)/.test(location.search)){k.value=decodeURIComponent(RegExp["\\x241"])}})();(function(){var u = G("u1").getElementsByTagName("a"), nv = G("nv").getElementsByTagName("a"), lk = G("lk").getElementsByTagName("a"), un = "";var tj_nv = ["news","tieba","zhidao","mp3","img","video","map"];var tj_lk = ["baike","wenku","hao123","more"];un = bds.comm.user == "" ? "" : bds.comm.user;function _addTJ(obj){addEV(obj, "mousedown", function(e){var e = e || window.event;var target = e.target || e.srcElement;if(target.name){ns_c({\'fm\':\'behs\',\'tab\':target.name,\'un\':encodeURIComponent(un)});}});}for(var i = 0; i < u.length; i++){_addTJ(u[i]);}for(var i = 0; i < nv.length; i++){nv[i].name = \'tj_\' + tj_nv[i];}for(var i = 0; i < lk.length; i++){lk[i].name = \'tj_\' + tj_lk[i];}})();(function() {var links = {\'tj_news\': [\'word\', \'http://news.baidu.com/ns?tn=news&cl=2&rn=20&ct=1&ie=utf-8\'],\'tj_tieba\': [\'kw\', \'http://tieba.baidu.com/f?ie=utf-8\'],\'tj_zhidao\': [\'word\', \'http://zhidao.baidu.com/search?pn=0&rn=10&lm=0\'],\'tj_mp3\': [\'key\', \'http://music.baidu.com/search?fr=ps&ie=utf-8\'],\'tj_img\': [\'word\', \'http://image.baidu.com/i?ct=201326592&cl=2&nc=1&lm=-1&st=-1&tn=baiduimage&istype=2&fm=&pv=&z=0&ie=utf-8\'],\'tj_video\': [\'word\', \'http://video.baidu.com/v?ct=301989888&s=25&ie=utf-8\'],\'tj_map\': [\'wd\', \'http://map.baidu.com/?newmap=1&ie=utf-8&s=s\'],\'tj_baike\': [\'word\', \'http://baike.baidu.com/search/word?pic=1&sug=1&enc=utf8\'],\'tj_wenku\': [\'word\', \'http://wenku.baidu.com/search?ie=utf-8\']};var domArr = [G(\'nv\'), G(\'lk\'),G(\'cp\')],kw = G(\'kw1\');for (var i = 0, l = domArr.length; i < l; i++) {domArr[i].onmousedown = function(e) {e = e || window.event;var target = e.target || e.srcElement,name = target.getAttribute(\'name\'),items = links[name],reg = new RegExp(\'^\\\\s+|\\\\s+\\x24\'),key = kw.value.replace(reg, \'\');if (items) {if (key.length > 0) {var wd = items[0], url = items[1],url = url + ( name === \'tj_map\' ? encodeURIComponent(\'&\' + wd + \'=\' + key) : ( ( url.indexOf(\'?\') > 0 ? \'&\' : \'?\' ) + wd + \'=\' + encodeURIComponent(key) ) );target.href = url;} else {target.href = target.href.match(new RegExp(\'^http:\\/\\/.+\\.baidu\\.com\'))[0];}}name && ns_c({\'fm\': \'behs\',\'tab\': name,\'query\': encodeURIComponent(key),\'un\': encodeURIComponent(bds.comm.user || \'\') });};}})();};if(window.pageState==0){initIndex();}})();document.cookie = \'IS_STATIC=1;expires=\' + new Date(new Date().getTime() + 10*60*1000).toGMTString();</script>\r\n</body></html>\r\n' www.cnblogs.com /ssyfj/ b'HTTP/1.1 200 OK\r\nDate: Sun, 24 Jun 2018 10:57:33 GMT\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 14922\r\nConnection: close\r\nVary: Accept-Encoding\r\nCache-Control: private, max-age=10\r\nExpires: Sun, 24 Jun 2018 10:57:43 GMT\r\nLast-Modified: Sun, 24 Jun 2018 10:57:33 GMT\r\nX-UA-Compatible: IE=10\r\n\r\n\r\n<!DOCTYPE html>\r\n<html lang="zh-cn">\r\n<head>\r\n<meta charset="utf-8"/>\r\n<meta name="viewport" content="width=device-width, initial-scale=1" />\r\n<title>\xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf - \xe5\x8d\x9a\xe5\xae\xa2\xe5\x9b\xad</title>\r\n<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=-hy83QNg62d4qYibixJzxMJkbf1P9fTBlqv7SK5zVL01"/>\n<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/darkgreentrip/bundle-darkgreentrip.css?v=xPXJVC4GCITs1yYYgSeLkcicCxapqseaPVQLFGMO1wc1"/>\n<link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="/skins/darkgreentrip/bundle-darkgreentrip-mobile.css?v=6NcJHqsIyaE4w19VtgFvCFahrnr2rYCTRRTdxlMDhhQ1"/>\r\n<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/ssyfj/rss"/>\r\n<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/ssyfj/rsd.xml"/>\n<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/ssyfj/wlwmanifest.xml"/>\r\n<script src="//common.cnblogs.com/scripts/jquery-2.2.0.min.js"></script>\r\n<script type="text/javascript">var currentBlogApp = \'ssyfj\', cb_enable_mathjax=false;var isLogined=false;</script>\r\n<script src="/bundles/blog-common.js?v=hrXhVUZRBrM1QWdCTm1D-5NdRKBmg-PqI2KBNh-fiD41" type="text/javascript"></script>\r\n</head>\r\n<body>\r\n<a name="top"></a>\r\n\r\n<!--done-->\r\n<div id="home">\r\n<div id="header">\r\n\t<div id="blogTitle">\r\n\t<a id="lnkBlogLogo" href="http://www.cnblogs.com/ssyfj/"><img id="blogLogo" src="/Skins/custom/images/logo.gif" alt="\xe8\xbf\x94\xe5\x9b\x9e\xe4\xb8\xbb\xe9\xa1\xb5" /></a>\t\t\t\r\n\t\t\r\n<!--done-->\r\n<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/ssyfj/">\xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf</a></h1>\r\n<h2>\xe5\x91\xa8\xe5\x9b\xb4\xe7\x9a\x84\xe4\xba\xba\xe9\x83\xbd\xe6\xaf\x94\xe4\xbd\xa0\xe5\x8e\x89\xe5\xae\xb3\xef\xbc\x8c\xe4\xbd\xa0\xe6\x89\x8d\xe4\xbc\x9a\xe6\x85\xa2\xe6\x85\xa2\xe5\x8f\x98\xe5\xbc\xba</h2>\r\n\r\n\r\n\r\n\t\t\r\n\t</div><!--end: blogTitle \xe5\x8d\x9a\xe5\xae\xa2\xe7\x9a\x84\xe6\xa0\x87\xe9\xa2\x98\xe5\x92\x8c\xe5\x89\xaf\xe6\xa0\x87\xe9\xa2\x98 -->\r\n\t<div id="navigator">\r\n\t\t\r\n<ul id="navList">\r\n<li><a id="blog_nav_sitehome" class="menu" href="http://www.cnblogs.com/">\xe5\x8d\x9a\xe5\xae\xa2\xe5\x9b\xad</a></li>\r\n<li><a id="blog_nav_myhome" class="menu" href="http://www.cnblogs.com/ssyfj/">\xe9\xa6\x96\xe9\xa1\xb5</a></li>\r\n<li><a id="blog_nav_newpost" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">\xe6\x96\xb0\xe9\x9a\x8f\xe7\xac\x94</a></li>\r\n<li><a id="blog_nav_contact" class="menu" rel="nofollow" href="https://msg.cnblogs.com/send/%E5%B1%B1%E4%B8%8A%E6%9C%89%E9%A3%8E%E6%99%AF">\xe8\x81\x94\xe7\xb3\xbb</a></li>\r\n<li><a id="blog_nav_rss" class="menu" href="http://www.cnblogs.com/ssyfj/rss">\xe8\xae\xa2\xe9\x98\x85</a>\r\n<!--<a id="blog_nav_rss_image" class="aHeaderXML" href="http://www.cnblogs.com/ssyfj/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="\xe8\xae\xa2\xe9\x98\x85" /></a>--></li>\r\n<li><a id="blog_nav_admin" class="menu" rel="nofollow" href="https://i.cnblogs.com/">\xe7\xae\xa1\xe7\x90\x86</a></li>\r\n</ul>\r\n\t\t<div class="blogStats">\r\n\t\t\t\r\n\t\t\t<div id="blog_stats">\r\n<span id="stats_post_count">\xe9\x9a\x8f\xe7\xac\x94 - 165 </span>\r\n<span id="stats_article_count">\xe6\x96\x87\xe7\xab\xa0 - 0 </span>\r\n<span id="stats-comment_count">\xe8\xaf\x84\xe8\xae\xba - 0</span>\r\n</div>\r\n\t\t\t\r\n\t\t</div><!--end: blogStats -->\r\n\t</div><!--end: navigator \xe5\x8d\x9a\xe5\xae\xa2\xe5\xaf\xbc\xe8\x88\xaa\xe6\xa0\x8f -->\r\n</div><!--end: header \xe5\xa4\xb4\xe9\x83\xa8 -->\r\n\r\n<div id="main">\r\n\t<div id="mainContent">\r\n\t<div class="forFlow">\r\n\t\t\r\n\r\n<!--done-->\r\n\r\n\r\n<div class="day">\r\n\t<div class="dayTitle">\r\n\t\t<a id="homepage1_HomePageDays_DaysList_ctl00_ImageLink" href="//www.cnblogs.com/ssyfj/archive/2018/06/24.html">2018\xe5\xb9\xb46\xe6\x9c\x8824\xe6\x97\xa5</a>\t\t\t\t \r\n\t</div>\r\n\r\n\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl00_DayList_TitleUrl_0" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9219360.html">python---\xe5\xbc\x82\xe6\xad\xa5IO(asyncio)\xe5\x8d\x8f\xe7\xa8\x8b</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: \xe7\xae\x80\xe5\x8d\x95\xe4\xba\x86\xe8\xa7\xa3 \xe5\x9c\xa8py3\xe4\xb8\xad\xe5\x86\x85\xe7\xbd\xae\xe4\xba\x86asyncio\xe6\xa8\xa1\xe5\x9d\x97\xe3\x80\x82\xe5\x85\xb6\xe7\xbc\x96\xe7\xa8\x8b\xe6\xa8\xa1\xe5\x9e\x8b\xe5\xb0\xb1\xe6\x98\xaf\xe4\xb8\x80\xe4\xb8\xaa\xe6\xb6\x88\xe6\x81\xaf\xe5\xbe\xaa\xe7\x8e\xaf\xe3\x80\x82 \xe6\xa8\xa1\xe5\x9d\x97\xe6\x9f\xa5\xe7\x9c\x8b\xef\xbc\x9a \xe8\xb0\x83\xe7\x94\xa8\xe6\xad\xa5\xe9\xaa\xa4\xef\xbc\x9a \xe7\xae\x80\xe5\x8d\x95\xe4\xbd\xbf\xe7\x94\xa8\xef\xbc\x9a 1 before func1 2 before func1 1 after func1 2 after func1 5.00528621673584 \xe5\xae\x9a\xe4\xb9\x89\xe4\xb8\x80\xe4\xb8\xaa\xe5\x8d\x8f\xe7\xa8\x8b\xef\xbc\x88\xe4\xb8\x8d\xe5\x90\x8c\xe4\xba\x8e\xe4\xb8\x8a\xe9\x9d\xa2\xe7\x9a\x84\xe5\xae\x9e\xe4\xbe\x8b\xef\xbc\x89 D:<a href="https://www.cnblogs.com/ssyfj/p/9219360.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-24 16:28 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(4) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9219360" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n</div>\r\n\r\n\r\n<div class="day">\r\n\t<div class="dayTitle">\r\n\t\t<a id="homepage1_HomePageDays_DaysList_ctl01_ImageLink" href="//www.cnblogs.com/ssyfj/archive/2018/06/23.html">2018\xe5\xb9\xb46\xe6\x9c\x8823\xe6\x97\xa5</a>\t\t\t\t \r\n\t</div>\r\n\r\n\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl01_DayList_TitleUrl_0" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9219257.html">python---await/async\xe5\x85\xb3\xe9\x94\xae\xe5\xad\x97</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: \xe6\x8e\xa8\xe6\x96\x87\xef\xbc\x9a\xe7\x8e\xa9\xe8\xbd\xac Python 3.5 \xe7\x9a\x84 await/async \xe9\xa6\x96\xe5\x85\x88\xe7\x9c\x8b\xe6\xad\xa3\xe5\xb8\xb8\xe7\x9a\x84\xe4\xb8\xa4\xe4\xb8\xaa\xe5\x87\xbd\xe6\x95\xb0\xe4\xb9\x8b\xe9\x97\xb4\xe7\x9a\x84\xe6\x89\xa7\xe8\xa1\x8c \xe6\x97\xa0\xe6\xb3\x95\xe5\xae\x9e\xe7\x8e\xb0\xe4\xb8\xa4\xe4\xb8\xaa\xe5\x87\xbd\xe6\x95\xb0\xe4\xb9\x8b\xe9\x97\xb4\xe7\x9a\x84\xe4\xba\xa4\xe4\xba\x92\xe3\x80\x82 \xe5\xb0\x86\xe8\xbf\x99\xe4\xb8\xa4\xe4\xb8\xaa\xe5\x87\xbd\xe6\x95\xb0\xe8\xae\xbe\xe7\xbd\xae\xe4\xb8\xba\xe5\x8d\x8f\xe7\xa8\x8b\xef\xbc\x8c\xe5\x8a\xa0\xe4\xb8\x8aasync\xe5\x85\xb3\xe9\x94\xae\xe5\xad\x97 \xe9\x82\xa3\xe4\xb9\x88\xef\xbc\x8c\xe4\xb8\xba\xe4\xbb\x80\xe4\xb9\x88\xe8\xa6\x81\xe6\x9c\x89\xe4\xb8\x80\xe4\xb8\xaa\xe5\x8d\x8f\xe7\xa8\x8b\xe5\xaf\xb9\xe8\xb1\xa1\xef\xbc\x9f\xe4\xbb\xa3\xe7\xa0\x81\xe5\x88\xb0\xe5\xba\x95\xe5\xa6\x82\xe4\xbd\x95\xe6\x89\xa7\xe8\xa1\x8c\xef\xbc\x9f \xe5\x85\xb3\xe9\x94\xae\xe4\xb9\x8b\xe5\xa4\x84\xe6\x98\xaf\xe5\x8d\x8f\xe7\xa8\x8b\xe7\xa1\xae\xe5\xae\x9e\xe6\x98\xaf\xe4\xb8\x8e Python \xe7\x9a\x84\xe7\x94\x9f\xe6\x88\x90\xe5\x99\xa8\xe9\x9d\x9e\xe5\xb8\xb8\xe7\x9b\xb8\xe4\xbc\xbc\xef\xbc\x8c\xe4\xb9\x9f\xe9\x83\xbd\xe6\x9c\x89\xe4\xb8\x80\xe4\xb8\xaa send \xe6\x96\xb9<a href="https://www.cnblogs.com/ssyfj/p/9219257.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-23 23:28 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(7) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9219257" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n\t\t\t<div class="postSeparator"></div>\r\n\t\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl01_DayList_TitleUrl_1" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9218730.html">python---\xe5\x8d\x8f\xe7\xa8\x8b\xe7\x90\x86\xe8\xa7\xa3</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: \xe6\x8e\xa8\xe6\x96\x87\xef\xbc\x9apython \xe5\x9f\xba\xe7\xa1\x80\xe7\x9f\xa5\xe8\xaf\x86\xe5\x9b\x9e\xe9\xa1\xbe\xef\xbc\x88\xe4\xb8\x83\xef\xbc\x89\xe8\xbf\xad\xe4\xbb\xa3\xe5\x99\xa8\xe5\x92\x8c\xe7\x94\x9f\xe6\x88\x90\xe5\x99\xa8 \xe6\x8e\xa8\xe6\x96\x87\xef\xbc\x9aPython\xe5\x8d\x8f\xe7\xa8\x8b\xe6\xb7\xb1\xe5\x85\xa5\xe7\x90\x86\xe8\xa7\xa3\xef\xbc\x88\xe6\x9c\xac\xe6\x96\x87\xe8\xbd\xac\xe8\xbd\xbd\xe4\xba\x8e\xe8\xaf\xa5\xe6\x96\x87\xe7\xab\xa0\xef\xbc\x89 \xe4\xbb\x8e\xe8\xaf\xad\xe6\xb3\x95\xe4\xb8\x8a\xe6\x9d\xa5\xe7\x9c\x8b\xef\xbc\x8c\xe5\x8d\x8f\xe7\xa8\x8b\xe5\x92\x8c\xe7\x94\x9f\xe6\x88\x90\xe5\x99\xa8\xe7\xb1\xbb\xe4\xbc\xbc\xef\xbc\x8c\xe9\x83\xbd\xe6\x98\xaf\xe5\xae\x9a\xe4\xb9\x89\xe4\xbd\x93\xe4\xb8\xad\xe5\x8c\x85\xe5\x90\xabyield\xe5\x85\xb3\xe9\x94\xae\xe5\xad\x97\xe7\x9a\x84\xe5\x87\xbd\xe6\x95\xb0\xe3\x80\x82yield\xe5\x9c\xa8\xe5\x8d\x8f\xe7\xa8\x8b\xe4\xb8\xad\xe7\x9a\x84\xe7\x94\xa8\xe6\xb3\x95\xef\xbc\x9a \xe5\x9c\xa8\xe5\x8d\x8f\xe7\xa8\x8b\xe4\xb8\xadyield\xe9\x80\x9a\xe5\xb8\xb8\xe5\x87\xba\xe7\x8e\xb0\xe5\x9c\xa8\xe8\xa1\xa8\xe8\xbe\xbe\xe5\xbc\x8f\xe7\x9a\x84\xe5\x8f\xb3\xe8\xbe\xb9\xef\xbc\x8c\xe4\xbe\x8b\xe5\xa6\x82\xef\xbc\x9adatum = yield,\xe5\x8f\xaf\xe4\xbb\xa5\xe4\xba\xa7\xe5\x87\xba\xe5\x80\xbc\xef\xbc\x8c\xe4\xb9\x9f\xe5\x8f\xaf\xe4\xbb\xa5<a href="https://www.cnblogs.com/ssyfj/p/9218730.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-23 20:09 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(8) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9218730" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n</div>\r\n\r\n\r\n<div class="day">\r\n\t<div class="dayTitle">\r\n\t\t<a id="homepage1_HomePageDays_DaysList_ctl02_ImageLink" href="//www.cnblogs.com/ssyfj/archive/2018/06/21.html">2018\xe5\xb9\xb46\xe6\x9c\x8821\xe6\x97\xa5</a>\t\t\t\t \r\n\t</div>\r\n\r\n\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl02_DayList_TitleUrl_0" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9210725.html">python---web\xe5\xbe\xae\xe4\xbf\xa1\xe5\xbc\x80\xe5\x8f\x91</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: \xe4\xb8\x80\xef\xbc\x9a\xe8\xbd\xae\xe8\xaf\xa2\xef\xbc\x8c\xe9\x95\xbf\xe8\xbd\xae\xe8\xaf\xa2\xef\xbc\x8cWebSocket\xe4\xba\x86\xe8\xa7\xa3 \xe8\xbd\xae\xe8\xaf\xa2\xef\xbc\x9a \xe9\x95\xbf\xe8\xbd\xae\xe8\xaf\xa2\xef\xbc\x9a WebSocket\xef\xbc\x9a \xe4\xba\x8c\xef\xbc\x9aweb\xe5\xbe\xae\xe4\xbf\xa1\xe6\xb5\x81\xe7\xa8\x8b\xe4\xbb\x8b\xe7\xbb\x8d \xe4\xb8\x89\xef\xbc\x9a\xe5\xbe\xae\xe4\xbf\xa1\xe7\x99\xbb\xe5\xbd\x95\xe5\xbc\x80\xe5\x8f\x91 \xe7\x99\xbb\xe5\xbd\x95\xe8\xa7\x86\xe5\x9b\xbelogin\xef\xbc\x8c\xe7\x94\xa8\xe4\xba\x8e\xe6\x98\xbe\xe7\xa4\xba\xe4\xba\x8c\xe7\xbb\xb4\xe7\xa0\x81 check_login\xe7\x94\xa8\xe4\xba\x8e\xe6\xa3\x80\xe6\xb5\x8b\xe7\x99\xbb\xe5\xbd\x95\xe7\x8a\xb6\xe6\x80\x81\xef\xbc\x9a408\xe6\x9c\xaa\xe6\x89\xab\xe6\x8f\x8f\xef\xbc\x8c201\xe6\x89\xab\xe6\x8f\x8f\xe4\xba\x8c\xe7\xbb\xb4\xe7\xa0\x81\xe4\xbd\x86\xe6\x98\xaf\xe6\x9c\xaa\xe7\x99\xbb\xe5\xbd\x95\xef\xbc\x8c200\xe7\x82\xb9\xe5\x87\xbb\xe7\x99\xbb\xe5\xbd\x95 ''' \xe6\x96\xb0\xe8\xaf\xb7\xe6\xb1\x82 GET \xe8\x8e\xb7\xe5\x8f\x96\xe8\xb7\xb3\xe8\xbd\xac\xe5\x9c\xb0\xe5\x9d\x80red<a href="https://www.cnblogs.com/ssyfj/p/9210725.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-21 23:33 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(13) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9210725" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n</div>\r\n\r\n\r\n<div class="day">\r\n\t<div class="dayTitle">\r\n\t\t<a id="homepage1_HomePageDays_DaysList_ctl03_ImageLink" href="//www.cnblogs.com/ssyfj/archive/2018/06/19.html">2018\xe5\xb9\xb46\xe6\x9c\x8819\xe6\x97\xa5</a>\t\t\t\t \r\n\t</div>\r\n\r\n\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl03_DayList_TitleUrl_0" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9200602.html">python---requests\xe5\x92\x8cbeautifulsoup4\xe6\xa8\xa1\xe5\x9d\x97\xe7\x9a\x84\xe4\xbd\xbf\xe7\x94\xa8</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: Requests\xef\xbc\x9a\xe6\x98\xaf\xe4\xbd\xbf\xe7\x94\xa8 Apache2 Licensed \xe8\xae\xb8\xe5\x8f\xaf\xe8\xaf\x81\xe7\x9a\x84 \xe5\x9f\xba\xe4\xba\x8ePython\xe5\xbc\x80\xe5\x8f\x91\xe7\x9a\x84HTTP \xe5\xba\x93\xef\xbc\x8c\xe5\x85\xb6\xe5\x9c\xa8Python\xe5\x86\x85\xe7\xbd\xae\xe6\xa8\xa1\xe5\x9d\x97\xe7\x9a\x84\xe5\x9f\xba\xe7\xa1\x80\xe4\xb8\x8a\xe8\xbf\x9b\xe8\xa1\x8c\xe4\xba\x86\xe9\xab\x98\xe5\xba\xa6\xe7\x9a\x84\xe5\xb0\x81\xe8\xa3\x85\xef\xbc\x8c\xe4\xbb\x8e\xe8\x80\x8c\xe4\xbd\xbf\xe5\xbe\x97Pythoner\xe8\xbf\x9b\xe8\xa1\x8c\xe7\xbd\x91\xe7\xbb\x9c\xe8\xaf\xb7\xe6\xb1\x82\xe6\x97\xb6\xef\xbc\x8c\xe5\x8f\x98\xe5\xbe\x97\xe7\xbe\x8e\xe5\xa5\xbd\xe4\xba\x86\xe8\xae\xb8\xe5\xa4\x9a\xef\xbc\x8c\xe4\xbd\xbf\xe7\x94\xa8Requests\xe5\x8f\xaf\xe4\xbb\xa5\xe8\xbd\xbb\xe8\x80\x8c\xe6\x98\x93\xe4\xb8\xbe\xe7\x9a\x84\xe5\xae\x8c\xe6\x88\x90\xe6\xb5\x8f\xe8\xa7\x88\xe5\x99\xa8\xe5\x8f\xaf\xe6\x9c\x89\xe7\x9a\x84\xe4\xbb\xbb\xe4\xbd\x95\xe6\x93\x8d\xe4\xbd\x9c\xe3\x80\x82 BeautifulSoup<a href="https://www.cnblogs.com/ssyfj/p/9200602.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-19 18:42 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(14) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9200602" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n\t\t\t<div class="postSeparator"></div>\r\n\t\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl03_DayList_TitleUrl_1" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9198020.html">python---RabbitMQ\xef\xbc\x885\xef\xbc\x89\xe6\xb6\x88\xe6\x81\xafRPC\xef\xbc\x88\xe8\xbf\x9c\xe7\xa8\x8b\xe8\xbf\x87\xe7\xa8\x8b\xe8\xb0\x83\xe7\x94\xa8\xef\xbc\x89</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: \xe6\x9c\x8d\xe5\x8a\xa1\xe5\x99\xa8\xe7\xab\xaf\xef\xbc\x9a \xe5\xae\xa2\xe6\x88\xb7\xe7\xab\xaf\xef\xbc\x9a \xe6\xb3\xa8\xe6\x84\x8f\xef\xbc\x9a self.connection.process_data_events()\xe4\xbc\x9a\xe5\x8e\xbb\xe9\x98\x9f\xe5\x88\x97\xe4\xb8\xad\xe8\x8e\xb7\xe5\x8f\x96\xe5\xa4\x84\xe7\x90\x86\xe6\x95\xb0\xe6\x8d\xae\xe4\xba\x8b\xe4\xbb\xb6\xef\xbc\x8c\xe5\xbd\x93\xe6\x95\xb0\xe6\x8d\xae\xe6\x9d\xa5\xe4\xb8\xb4\xe7\x9a\x84\xe6\x97\xb6\xe5\x80\x99\xef\xbc\x8c\xe4\xbc\x9a\xe7\x9b\xb4\xe6\x8e\xa5\xe5\x8e\xbb\xe8\xb0\x83\xe7\x94\xa8\xe5\x9b\x9e\xe8\xb0\x83\xe5\x87\xbd\xe6\x95\xb0\xe5\x8e\xbb\xe5\xa4\x84\xe7\x90\x86\xe6\x95\xb0\xe6\x8d\xae<a href="https://www.cnblogs.com/ssyfj/p/9198020.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-19 12:22 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(3) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9198020" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n</div>\r\n\r\n\r\n<div class="day">\r\n\t<div class="dayTitle">\r\n\t\t<a id="homepage1_HomePageDays_DaysList_ctl04_ImageLink" href="//www.cnblogs.com/ssyfj/archive/2018/06/16.html">2018\xe5\xb9\xb46\xe6\x9c\x8816\xe6\x97\xa5</a>\t\t\t\t \r\n\t</div>\r\n\r\n\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl04_DayList_TitleUrl_0" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9191756.html">restful api</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: RESTful API \xe8\xae\xbe\xe8\xae\xa1\xe6\x8c\x87\xe5\x8d\x97 \xe7\x90\x86\xe8\xa7\xa3RESTful\xe6\x9e\xb6\xe6\x9e\x84 \xe7\x90\x86\xe8\xa7\xa3\xe6\x9c\xac\xe7\x9c\x9f\xe7\x9a\x84REST\xe6\x9e\xb6\xe6\x9e\x84\xe9\xa3\x8e\xe6\xa0\xbc Python \xe4\xb9\x8b\xe8\xb7\xaf\xef\xbc\x8cRestful API\xe8\xae\xbe\xe8\xae\xa1\xe8\xa7\x84\xe8\x8c\x83 python \xe4\xb9\x8b\xe8\xb7\xaf\xef\xbc\x8cDjango rest framework \xe5\x88\x9d\xe6\x8e\xa2 Django-rest-frameworl\xe7\x9a\x84\xe4\xbd\xbf\xe7\x94\xa8 1.djangorestframework<a href="https://www.cnblogs.com/ssyfj/p/9191756.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-16 23:26 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(3) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9191756" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n\t\t\t<div class="postSeparator"></div>\r\n\t\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl04_DayList_TitleUrl_1" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9189422.html">Git\xe5\x92\x8cGithub\xe5\x85\xa5\xe9\x97\xa8</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: \xe6\x8e\xa8\xe6\x96\x87\xef\xbc\x9a\xe5\xae\x98\xe6\x96\xb9\xe6\x89\x8b\xe5\x86\x8c\xef\xbc\x8c\xe5\x8d\x81\xe5\x88\x86\xe8\xaf\xa6\xe7\xbb\x86 \xe6\x8e\xa8\xe6\x96\x87\xef\xbc\x9agit\xe5\x92\x8cgithub\xe5\xbf\xab\xe9\x80\x9f\xe5\x85\xa5\xe9\x97\xa8 \xe4\xb8\x80.git\xe4\xbd\xbf\xe7\x94\xa8 1.git\xe5\xae\x89\xe8\xa3\x85 \xef\xbc\x881\xef\xbc\x89windows \xe7\xbd\x91\xe7\xab\x99\xef\xbc\x9ahttps://git-scm.com/download/win\xe4\xb8\x8b\xe8\xbd\xbd\xe5\xae\x89\xe8\xa3\x85\xe5\x8d\xb3\xe5\x8f\xaf \xef\xbc\x882\xef\xbc\x89linux\xe5\xae\x89\xe8\xa3\x85 2.\xe7\x89\x88\xe6\x9c\xac\xe5\xba\x93\xe5\x88\x9b\xe5\xbb\xba \xef\xbc\x881\xef\xbc\x89\xe5\x88\x9b\xe5\xbb\xba\xe9\xa1\xb9\xe7\x9b\xae\xe7\x9b\xae\xe5\xbd\x95 \xef\xbc\x882\xef\xbc\x89\xe7\x89\x88\xe6\x9c\xac\xe5\xba\x93\xe5\x88\x9b\xe5\xbb\xba \xef\xbc\x883\xef\xbc\x89\xe5\x88\x9b\xe5\xbb\xba\xe4\xb8\x80\xe4\xb8\xaa\xe6\x96\x87\xe4\xbb\xb6\xef\xbc\x8c\xe5\xb0\x86\xe6\x96\x87<a href="https://www.cnblogs.com/ssyfj/p/9189422.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-16 00:03 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(3) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9189422" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n</div>\r\n\r\n\r\n<div class="day">\r\n\t<div class="dayTitle">\r\n\t\t<a id="homepage1_HomePageDays_DaysList_ctl05_ImageLink" href="//www.cnblogs.com/ssyfj/archive/2018/06/13.html">2018\xe5\xb9\xb46\xe6\x9c\x8813\xe6\x97\xa5</a>\t\t\t\t \r\n\t</div>\r\n\r\n\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl05_DayList_TitleUrl_0" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9179587.html">linux\xe4\xb8\x8b\xe5\xae\x89\xe8\xa3\x85shellinabox\xe5\xae\x9e\xe7\x8e\xb0web\xe7\x99\xbb\xe5\xbd\x95\xe6\x9c\x8d\xe5\x8a\xa1\xe5\x99\xa8</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: GitHub\xe5\x9c\xb0\xe5\x9d\x80\xef\xbc\x88\xe5\x90\xab\xe6\x9c\x89\xe6\x96\x87\xe4\xbb\xb6\xe4\xb8\x8b\xe8\xbd\xbd\xe5\x92\x8c\xe8\xaf\xa6\xe7\xbb\x86\xe5\xae\x89\xe8\xa3\x85\xe6\xb5\x81\xe7\xa8\x8b\xef\xbc\x89\xef\xbc\x9ahttps://github.com/shellinabox/shellinabox \xe8\xbf\x99\xe9\x87\x8c\xe6\x88\x91\xe4\xbb\xac\xe4\xbd\xbf\xe7\x94\xa8\xe7\x9a\x84\xe6\x98\xafredhat\xe5\xae\x89\xe8\xa3\x85\xe6\x96\xb9\xe6\xb3\x95\xe5\xa6\x82\xe4\xb8\x8b\xef\xbc\x9a 1.\xe9\x85\x8d\xe7\xbd\xae\xe5\xae\x89\xe8\xa3\x85\xe4\xbe\x9d\xe8\xb5\x96\xe7\x8e\xaf\xe5\xa2\x83 2.\xe4\xb8\x8b\xe8\xbd\xbdshellinabox\xe5\x8c\x85,\xe5\xbc\x80\xe5\xa7\x8b\xe5\xae\x89\xe8\xa3\x85 \xef\xbc\x881\xef\xbc\x89git\xe8\xbf\x9c\xe7\xa8\x8b\xe8\x8e\xb7\xe5\x8f\x96,\xe5\x85\x8b\xe9\x9a\x86\xe6\xba\x90\xe6\x96\x87\xe4\xbb\xb6\xe5\x88\xb0\xe9\xa1\xb9\xe7\x9b\xae\xe7\x9b\xae\xe5\xbd\x95 \xef\xbc\x882\xef\xbc\x89\xe5\x9c\xa8\xe9\xa1\xb5\xe9\x9d\xa2<a href="https://www.cnblogs.com/ssyfj/p/9179587.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-13 20:06 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(4) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9179587" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n\t\t\t<div class="postSeparator"></div>\r\n\t\t\r\n\t\t\t<div class="postTitle">\r\n\t\t\t\t<a id="homepage1_HomePageDays_DaysList_ctl05_DayList_TitleUrl_1" class="postTitle2" href="https://www.cnblogs.com/ssyfj/p/9178745.html">linux\xe4\xb8\x8byum\xe9\x94\x99\xe8\xaf\xaf\xef\xbc\x9a[Errno 14] problem making ssl connection Trying other mirror.</a>\r\n\t\t\t</div>\r\n\t\t\t<div class="postCon"><div class="c_b_p_desc">\xe6\x91\x98\xe8\xa6\x81: \xe4\xbb\x8a\xe5\xa4\xa9\xe6\x98\xaf\xe8\xa6\x81yum\xe5\x91\xbd\xe4\xbb\xa4\xe5\xae\x89\xe8\xa3\x85EPEL\xe4\xbb\x93\xe5\xba\x93\xe5\x90\x8e \xe7\xaa\x81\xe7\x84\xb6\xe5\x8f\x91\xe7\x8e\xb0yum\xe5\xae\x89\xe8\xa3\x85\xe5\x85\xb6\xe4\xbb\x96\xe7\x9a\x84\xe8\xbd\xaf\xe4\xbb\xb6\xe5\x87\xba\xe9\x94\x99\xe3\x80\x82 \xe4\xba\x8e\xe6\x98\xaf\xe5\x8e\xbb\xe7\xbd\x91\xe4\xb8\x8a\xe6\x9f\xa5\xe4\xba\x86\xe6\x9f\xa5\xef\xbc\x8c\xe5\x8f\x91\xe7\x8e\xb0\xe8\xae\xb8\xe5\xa4\x9a\xe8\xa7\xa3\xe5\x86\xb3\xe6\x96\xb9\xe6\xb3\x95\xe3\x80\x82\xe4\xbc\xbc\xe4\xb9\x8e\xe6\x98\xafssl\xe8\xaf\x81\xe4\xb9\xa6\xe7\x9a\x84\xe5\x8e\x9f\xe5\x9b\xa0\xef\xbc\x8c\xe6\x9c\x80\xe7\xbb\x88\xe9\x80\x89\xe6\x8b\xa9 vim /etc/yum.repos.d/epel.repo \xe5\xb0\x86baseurl\xe6\xb3\xa8\xe9\x87\x8a\xe5\x8e\xbb\xe6\x8e\x89\xef\xbc\x8c\xe4\xb8\xbamirrorlist\xe5\x8a\xa0\xe4\xb8\x8a\xe6\xb3\xa8\xe9\x87\x8a\xe3\x80\x82\xe7\x84\xb6\xe5\x90\x8e\xe5\xb0\x86enabeld=1\xe5\x8f\x98\xe4\xb8\xba0\xef\xbc\x8c\xe7\xa6\x81\xe7\x94\xa8<a href="https://www.cnblogs.com/ssyfj/p/9178745.html" class="c_b_p_desc_readmore">\xe9\x98\x85\xe8\xaf\xbb\xe5\x85\xa8\xe6\x96\x87</a></div></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\t<div class="postDesc">posted @ 2018-06-13 17:06 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf \xe9\x98\x85\xe8\xaf\xbb(13) \xe8\xaf\x84\xe8\xae\xba(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9178745" rel="nofollow">\xe7\xbc\x96\xe8\xbe\x91</a></div>\r\n\t\t\t<div class="clear"></div>\r\n\t\t\r\n</div>\r\n\r\n<div class="topicListFooter"><div id="nav_next_page"><a href="http://www.cnblogs.com/ssyfj/default.html?page=2">\xe4\xb8\x8b\xe4\xb8\x80\xe9\xa1\xb5</a></div></div>\r\n\r\n\r\n\t</div><!--end: forFlow -->\r\n\t</div><!--end: mainContent \xe4\xb8\xbb\xe4\xbd\x93\xe5\x86\x85\xe5\xae\xb9\xe5\xae\xb9\xe5\x99\xa8-->\r\n\r\n\t<div id="sideBar">\r\n\t\t<div id="sideBarMain">\r\n\t\t\t\r\n<!--done-->\r\n<div class="newsItem">\r\n<h3 class="catListTitle">\xe5\x85\xac\xe5\x91\x8a</h3>\r\n\t<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>\r\n</div>\r\n\r\n\t\t\t<div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script>\r\n\t\t\t\r\n\t\t\t<div id="leftcontentcontainer">\r\n\t\t\t\t<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>\r\n\t\t\t</div>\r\n\t\t\t\r\n\t\t</div><!--end: sideBarMain -->\r\n\t</div><!--end: sideBar \xe4\xbe\xa7\xe8\xbe\xb9\xe6\xa0\x8f\xe5\xae\xb9\xe5\x99\xa8 -->\r\n\t<div class="clear"></div>\r\n\t</div><!--end: main -->\r\n\t<div class="clear"></div>\r\n\t<div id="footer">\r\n\t\t\r\n<!--done-->\r\nCopyright ©2018 \xe5\xb1\xb1\xe4\xb8\x8a\xe6\x9c\x89\xe9\xa3\x8e\xe6\x99\xaf\r\n\t</div><!--end: footer -->\r\n</div><!--end: home \xe8\x87\xaa\xe5\xae\x9a\xe4\xb9\x89\xe7\x9a\x84\xe6\x9c\x80\xe5\xa4\xa7\xe5\xae\xb9\xe5\x99\xa8 -->\r\n</body>\r\n</html>\r\n' www.ckook.com /forum.php b'HTTP/1.1 301 Moved Permanently\r\nDate: Sun, 24 Jun 2018 10:57:33 GMT\r\nServer: Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45\r\nX-Powered-By: PHP/5.4.45\r\nLocation: https://www.ckook.com/forum.php\r\nContent-Length: 133\r\nContent-Type: text/html\r\n\r\n<br />\n<b>Notice</b>: Undefined index: HTTP_USER_AGENT in <b>C:\\www\\web\\www\\source\\class\\class_core.php</b> on line <b>19</b><br />\n' 5.589319705963135
(三)使用asyncio相关模块aiohttp(需要先安装,pip3即可)
asyncio支持的相关模块:
import asyncio,aiohttp async def fetch_async(url): print(url) async with aiohttp.request("GET",url) as r: reponse = await r.text(encoding="utf-8") #或者直接await r.read()不编码,直接读取,适合于图像等无法编码文件 print(reponse) tasks = [fetch_async('http://www.baidu.com/'), fetch_async('http://www.chouti.com/')] event_loop = asyncio.get_event_loop() results = event_loop.run_until_complete(asyncio.gather(*tasks)) event_loop.close()
(四)asyncio+requests,使用了事件循环中run_in_executor(创建了线程池去执行requests模块函数)
import asyncio import requests async def fetch_async(func, *args): loop = asyncio.get_event_loop() future = loop.run_in_executor(None, func, *args) #在内部创建了一个线程池,并以相同顺序返回结果 response = await future #等待线程执行结果返回后,才去获取 print(response.url, response.content) tasks = [ fetch_async(requests.get, 'http://www.cnblogs.com/ssyfj/'), fetch_async(requests.get, 'https://www.cnblogs.com/ssyfj/p/9222342.html') ] loop = asyncio.get_event_loop() results = loop.run_until_complete(asyncio.gather(*tasks)) loop.close()
源码:
开启线程池:
def run_in_executor(self, executor, func, *args): self._check_closed() if self._debug: self._check_callback(func, 'run_in_executor') if executor is None: executor = self._default_executor if executor is None: executor = concurrent.futures.ThreadPoolExecutor() #创建线程池 self._default_executor = executor return futures.wrap_future(executor.submit(func, *args), loop=self) #线程池执行函数
调用线程安全处理协程对象call_soon_threadsafe。主线程处理事件循环,子线程处理任务。
response = await future #response是future对象,futures是异步并发模块
def __iter__(self): #可迭代,异步对象 if not self.done(): self._asyncio_future_blocking = True yield self # This tells Task to wait for completion. assert self.done(), "yield from wasn't used with future" return self.result() # May raise too.
(五)gevent+requests
import gevent,requests,time from gevent import monkey monkey.patch_all() #将socket变为异步 def fetch_async(url): reponse = requests.get(url) print(reponse.url,reponse.content) begin = time.time() gevent.joinall([ gevent.spawn(fetch_async("https://www.python.org/")), gevent.spawn(fetch_async("https://www.baidu.com/")), gevent.spawn(fetch_async("https://www.github.com/")), ]) end = time.time() print(end-begin)
gevent线程池
import gevent,time,requests from gevent.pool import Pool #协程池,一次最多数量限制 pool = Pool(10) def fetch_async(method, url, req_kwargs): print(method, url, req_kwargs) response = requests.request(method=method, url=url, **req_kwargs) print(response.url, response.content) begin = time.time() gevent.joinall([ pool.spawn(fetch_async, method='get', url='https://www.python.org/', req_kwargs={}), pool.spawn(fetch_async, method='get', url='https://www.yahoo.com/', req_kwargs={}), pool.spawn(fetch_async, method='get', url='https://www.github.com/', req_kwargs={}), ]) end = time.time() print(end-begin)
(六)grequests===>gevent+request是对上面的结合使用
import grequests request_list = [ grequests.get('https://www.python.org/'), grequests.get('https://www.yahoo.com/'), grequests.get('https://www.github.com/'), ] reponse_list = grequests.map(request_list) print(reponse_list)
(七)twisted的使用
推文:网络框架twisted
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("all done") reactor.stop() @defer.inlineCallbacks def task(url): res = getPage(bytes(url,encoding="utf-8")) #获取页面,发送http请求,是使用select池将所有socket请求保存,依据此进行计数。 print('6',type(res)) #<class 'twisted.internet.defer.Deferred'> res.addCallback(one_done) #对每一个请求都添加一个回调方法 yield res #返回他 start_url_list = [ 'http://www.baidu.com', 'http://www.github.com', ] defer_list = [] for url in start_url_list: v = task(url) #发送请求后立即返回,不等待返回,v是一个特殊对象,标志你发送到那个请求 print(v,type(v)) defer_list.append(v) d = defer.DeferredList(defer_list) #将上面的特殊对象列表一起放入DeferredList d.addBoth(all_done) #为所有对象添加回调 reactor.run() #会一直循环,我们需要在任务执行完毕后关闭。含有计数器,执行一个任务,会执行一次one_done,计数减一。单任务执行完毕,计数为0,执行all_done
(八)tornado的使用
from tornado.httpclient import AsyncHTTPClient from tornado.httpclient import HTTPRequest from tornado import ioloop COUNT = 0 def handle_response(response): """ 处理返回值内容(需要维护计数器,来停止IO循环),调用 ioloop.IOLoop.current().stop() :param response: :return: """ global COUNT COUNT -= 1 if response.error: print("Error:", response.error) else: print(response.body) 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()
选用原则:gevent>twisted>tornado>asyncio
三:自定义异步IO模块开发
# coding:utf8 # __author: Administrator # date: 2018/6/25 0025 # /usr/bin/env python import socket import select class HttpRequest: def __init__(self,sk,host,callback): self.socket = sk self.host= host self.callback = callback def fileno(self): #select需要的是文件描述符,传递的数据对象,必须含有fileno方法,返回文件描述符 return self.socket.fileno() class HttpResponse: def __init__(self,recv_data): self.recv_data = recv_data self.header_dict = {} self.body = None self.initialize() def initialize(self): #先分割请求头 headers,body = self.recv_data.split(b'\r\n\r\n',1) #在分割各个请求头数据 header_list = headers.split(b"\r\n") for hd in header_list: hd_str = str(hd,encoding="utf-8") item = hd_str.split(":") if len(item) == 2: self.header_dict[item[0]]=item[1] self.body = str(body,encoding="utf-8") class AsyncRequest(): def __init__(self): self.read_list = [] self.write_list = [] self.error_list = [] def create_task(self,task): try: sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.setblocking(False) sk.connect((task['host'], 80,)) except BlockingIOError as e: pass request = HttpRequest(sk,task['host'],task['callback']) self.read_list.append(request) self.write_list.append(request) def run(self): while True: r_list,w_list,e_list = select.select(self.read_list,self.write_list,self.error_list) for w in w_list: #向网站发送请求头,开始获取资源 print(w.host,"连接成功...") tpl = "GET / HTTP/1.0\r\nHost:%s\r\n\r\n"%(w.host,) w.socket.send(bytes(tpl,encoding="utf-8")) self.write_list.remove(w) for r in r_list: print(r.host,"开始采集资源...") recv_data = bytes() while True: try: chunck = r.socket.recv(1024) recv_data += chunck except BlockingIOError as e: #当数据为空,且不是由于阻塞抛出异常时,才是完成数据采集 continue else: if not chunck: break #采集结束,封装数据,开始回调操作 reponse = HttpResponse(recv_data) r.callback(reponse) r.socket.close() self.read_list.remove(r) #所有数据采集完成,退出 if len(self.read_list) == 0: break def f1(reponse): print("f1",reponse.header_dict) def f2(reponse): print("f2",reponse.header_dict) def f3(reponse): print("f3",reponse.header_dict) url_list = [ {"host":'www.baidu.com',"callback":f1}, {'host':'cn.bing.com',"callback":f2}, {'host':'www.cctv.com',"callback":f3} ] AsynReq = AsyncRequest() for url in url_list: AsynReq.create_task(url) AsynReq.run()