Flask 浣滆€?Armin Ronacher锛氭垜涓嶈寰楁湁寮傛鍘嬪姏
Posted Python鐚?/a> Fl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask 浣滆€?Armin Ronacher锛氭垜涓嶈寰楁湁寮傛鍘嬪姏相关的知识,希望对你有一定的参考价值。
鑻辨枃 | I'm not feeling the async pressure
鍘熶綔 | Armin Ronacher锛?020.01.01
璇戣€?/strong> | 璞岃眴鑺变笅鐚獲Python鐚?/span>
寮傛锛坅sync锛夋椋庨潯涓€鏃躲€傚紓姝ython銆佸紓姝ust銆乬o銆乶ode銆?NET锛屼换閫変竴涓綘鏈€鐖辩殑璇█鐢熸€侊紝瀹冮兘鍦ㄤ娇鐢ㄧ潃涓€浜涘紓姝ャ€傚紓姝ヨ繖涓滆タ鏈夊濂斤紝杩欏湪寰堝ぇ绋嬪害涓婂彇鍐充簬璇█鐨勭敓鎬佸強鍏惰繍琛屾椂闂达紝浣嗘€讳綋鑰岃█锛屽畠鏈変竴浜涗笉閿欑殑濂藉銆傚畠浣垮緱杩欑浜嬫儏鍙樺緱闈炲父绠€鍗曪細绛夊緟鍙兘闇€瑕佷竴浜涙椂闂存墠鑳藉畬鎴愮殑鎿嶄綔銆?/p>
瀹冩槸濡傛绠€鍗曪紝浠ヨ嚦浜庡垱閫犱簡鏃犳暟鏂扮殑鏂规硶鏉ュ潙浜猴紙blow ones foot off锛夈€傛垜鎯宠璁虹殑涓€绉嶆儏鍐垫槸锛岀洿鍒扮郴缁熷嚭鐜拌秴杞斤紝浣犳墠鎰忚瘑鍒拌嚜宸辫俯鍒颁簡鑴氱殑閭d竴绉嶏紝杩欏氨鏄儗鍘嬶紙back pressure锛夌鐞嗙殑涓婚銆傚湪鍗忚璁捐涓湁涓€涓浉鍏虫湳璇槸娴侀噺鎺у埗锛坒low control锛夈€?/p>
浠€涔堟槸鑳屽帇
鍏充簬鑳屽帇鐨勮В閲婃湁寰堝锛屾垜鎺ㄨ崘闃呰鐨勪竴涓緢濂界殑瑙i噴鏄細Backpressure explained 鈥?the resisted flow of data through software銆?銆戙€傚洜姝わ紝涓庡叾璇︾粏浠嬬粛浠€涔堟槸鑳屽帇锛屾垜鍙兂瀵瑰叾鍋氫竴涓潪甯哥畝鐭殑瀹氫箟鍜岃В閲婏細鑳屽帇鏄樆纰嶆暟鎹湪绯荤粺涓祦閫氱殑闃诲姏銆傝儗鍘嬪惉璧锋潵寰堣礋闈⑩€斺€旇皝閮戒細鎯宠薄娴寸几鍥犵閬撳牭濉炶€屾孩鍑衡€斺€斾絾杩欐槸涓轰簡鑺傜渷浣犵殑鏃堕棿銆?/p>
锛堣瘧娉細back pressure锛岄櫎浜嗚儗鍘嬶紝杩樻湁浜鸿瘧涓衡€滃洖鍘嬧€濄€佲€滃弽鍘嬧€濓級
鍦ㄨ繖閲岋紝鎴戜滑瑕佸鐞嗙殑涓滆タ鍦ㄦ墍鏈夋儏鍐典笅鎴栧鎴栧皯閮芥槸鐩稿悓鐨勶細鎴戜滑鏈変竴涓郴缁熷皢涓嶅悓缁勪欢缁勫悎鎴愪竴涓閬擄紝鑰岃绠¢亾闇€瑕佹帴鏀朵竴瀹氭暟閲忕殑浼犲叆娑堟伅銆?/p>
浣犲彲浠ユ兂璞¤繖灏卞儚鍦ㄦ満鍦烘ā鎷熻鏉庤繍閫佷竴鏍枫€傝鏉庡埌杈撅紝缁忚繃鍒嗙被锛岃鍏ラ鏈猴紝鏈€鍚庡嵏涓嬨€傚湪杩欒繃绋嬩腑锛屼竴浠惰鏉庤璺熷叾瀹冭鏉庝竴璧凤紝琚墧杩涢泦瑁呯杩涜杩愯緭銆傚綋涓€涓泦瑁呯瑁呮弧鍚庯紝闇€瑕佸皢鍏惰繍璧般€傚綋娌℃湁鍓╀綑鐨勯泦瑁呯鏃讹紝杩欏氨鏄儗鍘嬬殑鑷劧绀轰緥銆傜幇鍦紝鏀捐鏉庤€呬笉鑳芥斁浜嗭紝鍥犱负娌℃湁闆嗚绠便€?/p>
姝ゆ椂蹇呴』鍋氬嚭鍐冲畾銆備竴绉嶉€夋嫨鏄瓑寰咃細杩欓€氬父琚О涓烘帓闃燂紙queueing 锛夋垨缂撳啿锛坆uffering锛夈€傚彟涓€绉嶉€夋嫨鏄墧鎺変竴浜涜鏉庯紝鐩村埌鏈変竴涓泦瑁呯鍒拌揪涓烘鈥斺€旇繖琚О涓轰涪寮冿紙dropping锛夈€傝繖鍚捣鏉ュ緢绯熺硶锛屼絾鏄◢鍚庢垜浠皢鎺㈣涓轰粈涔堟湁鏃跺緢閲嶈銆?/p>
浣嗘槸锛岃繖閲岃繕鏈夊彟涓€浠朵簨銆傛兂璞′竴涓嬶紝璐熻矗灏嗚鏉庢斁鍏ラ泦瑁呯鐨勪汉鍦ㄨ緝闀跨殑鏃堕棿鍐咃紙渚嬪涓€鍛級閮芥病绛夊埌闆嗚绠便€傛渶缁堬紝濡傛灉浠栦滑娌℃湁涓㈠純琛屾潕锛岄偅涔堜粬浠懆鍥村皢鏈夋暟閲忓簽澶х殑琛屾潕銆傛渶缁堬紝浠栦滑琚揩瑕佹暣鐞嗙殑琛屾潕鏁伴噺澶锛岀敤鍏変簡瀛樺偍琛屾潕鐨勭墿鐞嗙┖闂淬€傚埌閭f椂锛屼粬浠渶濂芥槸鍛婅瘔鏈哄満锛屽湪瑙e喅濂介泦瑁呯闂涔嬪墠锛屼笉鑳藉啀鎺ユ敹鏂扮殑琛屾潕浜嗐€傝繖閫氬父琚О涓烘祦閲忔帶鍒躲€?銆戯紝鏄竴涓嚦鍏抽噸瑕佺殑缃戠粶姒傚康銆?/p>
閫氬父杩欎簺澶勭悊绠¢亾鍦ㄦ瘡娈垫椂闂村唴鍙兘瀹圭撼涓€瀹氭暟閲忕殑娑堟伅锛堝鏈緥涓殑琛屾潕绠憋級銆傚鏋滄暟閲忚秴杩囦簡瀹冿紝鎴栬€呮洿绯熺硶鐨勬槸绠¢亾鍋滄粸锛屽垯鍙兘鍙戠敓鍙€曠殑浜嬫儏銆傜幇瀹炰笘鐣屼腑鐨勪竴涓緥瀛愭槸浼︽暒甯屾€濈綏鏈哄満 5 鍙疯埅绔欐ゼ寮€鏀撅紝鐢变簬鍏?IT 鍩虹鏋舵瀯鏃犳硶姝e父杩愯锛屽湪 10 澶╁唴鏈兘瀹屾垚杩愰€?42,000 浠惰鏉庛€備粬浠笉寰椾笉鍙栨秷 500 澶氫釜鑸彮锛屽苟涓旀湁涓€娈垫椂闂达紝鑸┖鍏徃鍐冲畾鍙厑璁搁殢韬惡甯﹁鏉庛€?/p>
鑳屽帇寰堥噸瑕?/span>
鎴戜滑浠庡笇鎬濈綏鐏鹃毦涓鍒扮殑鏄紝鑳藉浜ゆ祦鑳屽帇鑷冲叧閲嶈銆傚湪鐜板疄鐢熸椿涓互鍙婂湪璁$畻涓紝鏃堕棿鎬绘槸鏈夐檺鐨勩€傛渶缁堜汉浠細鏀惧純绛夊緟鏌愪簺浜嬫儏銆傜壒鍒槸鍗充娇鏌愪簺浜嬬墿鍦ㄥ唴閮ㄥ彲浠ユ案杩滅瓑寰咃紝浣嗗湪澶栭儴鍗翠笉鑳姐€?/p>
涓句竴涓幇瀹炵殑渚嬪瓙锛氬鏋滀綘鐨勮鏉庨渶閫氳繃浼︽暒甯屾€濈綏鏈哄満鍒拌揪鐩殑鍦板反榛庯紝浣嗘槸浣犲彧鑳藉湪閭e憜 7 澶╋紝閭d箞濡傛灉琛屾潕寤惰繜鎴?10 澶╁埌杈撅紝杩欏氨姣棤鎰忎箟浜嗐€傚疄闄呬笂锛屼綘甯屾湜灏嗚鏉庨噸鏂拌矾鐢憋紙re-routed锛夊洖浣犵殑瀹朵埂鏈哄満銆?/p>
瀹為檯涓婏紝鎵胯澶辫触锛堜綘瓒呰礋杞戒簡锛夋瘮鍋囪鍙繍浣滃苟鎸佺画淇濇寔缂撳啿鐘舵€佽濂斤紝鍥犱负鍒颁簡鏌愪釜鏃跺€欙紝瀹冨彧浼氫护鎯呭喌鍙樺緱鏇寸碂銆?/p>
閭d箞锛屼负浠€涔堝湪鎴戜滑缂栧啓浜嗗骞寸殑鍩轰簬绾跨▼鐨勮蒋浠舵椂锛岃儗鍘嬮兘娌℃湁琚彁鍑猴紝鐜板湪鍗寸獊鐒舵垚涓鸿璁虹殑璇濋鍛紵鏈夎澶氬洜绱犵殑缁撳悎锛屽叾涓竴浜涘洜绱犲緢瀹规槗浣夸汉闄峰叆鍥板銆?/p>
绯熺硶鐨勯粯璁ゆ柟寮?/span>
涓轰簡鐞嗚В涓轰粈涔堣儗鍘嬪湪寮傛浠g爜涓緢閲嶈锛屾垜鎯充负浣犳彁渚涗竴娈电湅浼肩畝鍗曠殑 Python asyncio 浠g爜锛屽畠灞曠ず浜嗕竴浜涙垜浠笉鎱庡繕璁颁簡鑳屽帇鐨勬儏鍐碉細
from asyncio import start_server, run
async def on_client_connected(reader, writer):
while True:
data = await reader.readline()
if not data:
break
writer.write(data)
async def server():
srv = await start_server(on_client_connected, '127.0.0.1', 8888)
async with srv:
await srv.serve_forever()
run(server())
濡傛灉浣犲垰鎺ヨЕ async/await 姒傚康锛岃鎯宠薄涓€涓嬪湪璋冪敤 await 鐨勬椂鍊欙紝鍑芥暟浼氭寕璧凤紝鐩村埌琛ㄨ揪寮忚В鏋愬畬姣曘€傚湪杩欓噷锛孭ython 鐨?asyncio 搴撴彁渚涚殑 start_server 鍑芥暟浼氳繍琛屼竴涓殣钘忕殑 accept 寰幆銆傚畠渚﹀惉濂楁帴瀛楋紝骞朵负姣忎釜杩炴帴鐨勫鎺ュ瓧鐢熸垚涓€涓嫭绔嬬殑浠诲姟杩愯鐫€ on_client_connected 鍑芥暟銆?/p>
鐜板湪锛岃繖鐪嬭捣鏉ラ潪甯哥畝鍗曟槑浜嗐€備綘鍙互鍒犻櫎鎵€鏈夌殑 await 鍜?async 鍏抽敭瀛楋紝鏈€缁堢殑浠g爜鐪嬭捣鏉ヤ笌浣跨敤绾跨▼鏂瑰紡缂栧啓鐨勪唬鐮侀潪甯哥浉浼笺€?/p>
浣嗘槸锛屽畠闅愯棌浜嗕竴涓潪甯稿叧閿殑闂锛岃繖鏄垜浠墍鏈夐棶棰樼殑鏍规簮锛氬湪鏌愪簺鍑芥暟璋冪敤鐨勫墠闈㈡病鏈?await銆傚湪绾跨▼浠g爜涓紝浠讳綍鍑芥暟閮藉彲浠?yield銆傚湪寮傛浠g爜涓紝鍙湁寮傛鍑芥暟鍙互銆傚湪鏈緥涓紝杩欐剰鍛崇潃 writer.write 鏂规硶鏃犳硶闃诲銆傞偅涔堝畠鏄浣曞伐浣滅殑鍛紵瀹冨皢灏濊瘯灏嗘暟鎹洿鎺ュ啓鍏ュ埌鎿嶄綔绯荤粺鐨勬棤闃诲濂楁帴瀛楃紦鍐插尯涓€?/p>
浣嗘槸锛屽鏋滅紦鍐插尯宸叉弧骞朵笖濂楁帴瀛椾細闃诲锛屼細鍙戠敓浠€涔堬紵鍦ㄧ敤绾跨▼鐨勬儏鍐典笅锛屾垜浠彲浠ュ湪姝ゅ灏嗗叾闃诲锛岃繖寰堢悊鎯筹紝鍥犱负杩欐剰鍛崇潃鎴戜滑姝e湪鏂藉姞涓€浜涜儗鍘嬨€傜劧鑰岋紝鍥犱负杩欓噷娌℃湁绾跨▼锛屾墍浠ユ垜浠笉鑳借繖鏍峰仛銆傚洜姝わ紝鎴戜滑鍙兘鍦ㄦ澶勮繘琛岀紦鍐叉垨鑰呭垹闄ゆ暟鎹€傚洜涓哄垹闄ゆ暟鎹槸闈炲父绯熺硶鐨勶紝鎵€浠?Python 閫夋嫨浜嗙紦鍐层€?/p>
鐜板湪锛屽鏋滄湁浜哄悜鍏朵腑鍙戦€佷簡寰堝鏁版嵁鍗存病鏈夎鍙栵紝浼氬彂鐢熶粈涔堬紵濂戒簡鍦ㄩ偅绉嶆儏鍐典笅锛岀紦鍐插尯浼氬澶э紝澧炲ぇ锛屽啀澧炲ぇ銆傝繖涓?API 缂洪櫡灏辨槸涓轰粈涔?Python 鐨勬枃妗d腑璇达紝涓嶈鍙槸鍗曠嫭浣跨敤 write锛岃繕瑕佹帴鐫€鍐?drain锛堣瘧娉細娑堣€椼€佹帓姘达級锛?/p>
writer.write(data)
await writer.drain()
drain 浼氭帓鍑虹紦鍐插尯涓婂浣欑殑涓滆タ銆傚畠涓嶄細鎺掔┖鏁翠釜缂撳啿鍖猴紝鍙細鍋氬埌浠や簨鎯呬笉鑷村け鎺х殑绋嬪害銆傞偅涔堜负浠€涔?write 涓嶅仛闅愬紡 drain 鍛紵濂藉惂锛岃繖浼氭槸涓€涓ぇ瑙勬ā鐨?API 鐩戞帶锛屾垜涓嶇‘瀹氳濡備綍鍋氬埌銆?/p>
杩欓噷闈炲父閲嶈鐨勬槸澶у鏁板鎺ュ瓧閮藉熀浜?TCP锛岃€?TCP 鍏锋湁鍐呯疆鐨勬祦閲忔帶鍒躲€倃riter 鍙細鎸夌収 reader 鍙帴鍙楃殑閫熷害鍐欏叆锛堢粰浜堟垨鍗犵敤涓€浜涚紦鍐茬┖闂达級銆傝繖瀵瑰紑鍙戣€呭畬鍏ㄦ槸闅愯棌鐨勶紝鍥犱负鐢氳嚦 BSD 濂楁帴瀛楀簱閮芥病鏈夊叕寮€杩欑闅愬紡鐨勬祦閲忔帶鍒舵搷浣溿€?/p>
閭f垜浠湪杩欓噷瑙e喅鑳屽帇闂浜嗗悧锛熷ソ鍚э紝璁╂垜浠湅涓€鐪嬪湪绾跨▼涓栫晫涓細鏄€庢牱銆傚湪绾跨▼涓栫晫涓紝鎴戜滑鐨勪唬鐮佸緢鍙兘浼氳繍琛屽浐瀹氭暟閲忕殑绾跨▼锛岃€?accept 寰幆浼氫竴鐩寸瓑寰咃紝鐩村埌绾跨▼鍙樺緱鍙敤鍐嶆帴绠¤姹傘€?/p>
鐒惰€岋紝鍦ㄦ垜浠殑寮傛绀轰緥涓紝鏈夋棤鏁扮殑杩炴帴瑕佸鐞嗐€傝繖灏辨剰鍛崇潃鎴戜滑鍙兘鏀跺埌澶ч噺杩炴帴锛屽嵆浣胯繖鎰忓懗鐫€绯荤粺鍙兘浼氳繃杞姐€傚湪杩欎釜闈炲父绠€鍗曠殑绀轰緥涓紝鍙兘涓嶆垚闂锛屼絾璇锋兂璞′竴涓嬶紝濡傛灉鎴戜滑鍋氱殑鏄暟鎹簱璁块棶锛屼細鍙戠敓浠€涔堛€?/p>
鎯宠薄涓€涓暟鎹簱杩炴帴姹狅紝瀹冩渶澶氭彁渚?50 涓繛鎺ャ€傚綋澶у鏁拌繛鎺ヤ細鍦ㄨ繛鎺ユ睜澶勯樆濉炴椂锛屾帴鍙?10000 涓繛鎺ュ張鏈変粈涔堢敤锛?/p>
绛夊緟涓庣瓑寰呯潃绛夊緟
濂藉暒锛岀粓浜庡洖鍒颁簡鎴戞渶鍒濇兂璁ㄨ鐨勫湴鏂广€傚湪澶у鏁板紓姝ョ郴缁熶腑锛岀壒鍒槸鎴戝湪 Python 涓亣鍒扮殑澶у鏁版儏鍐典腑锛屽嵆浣夸綘淇浜嗘墍鏈夊鎺ュ瓧灞傜殑缂撳啿琛屼负锛屼篃鏈€缁堜細闄峰叆涓€涓皢涓€鍫嗗紓姝ュ嚱鏁伴摼鎺ュ湪涓€璧凤紝鑰屼笉鑰冭檻鑳屽帇鐨勪笘鐣屻€?/p>
濡傛灉鎴戜滑浠ユ暟鎹簱杩炴帴姹犱负渚嬶紝鍋囪鍙湁 50 涓彲鐢ㄨ繛鎺ャ€傝繖鎰忓懗鐫€鎴戜滑鐨勪唬鐮佹渶澶氬彲浠ユ湁 50 涓苟鍙戠殑鏁版嵁搴撲細璇濄€傚亣璁炬垜浠笇鏈涘鐞?4 鍊嶅鐨勮姹傦紝鍥犱负鎴戜滑鏈熸湜搴旂敤绋嬪簭鎵ц鐨勮澶氭搷浣滄槸鐙珛浜庢暟鎹簱鐨勩€備竴绉嶈В鍐虫柟娉曟槸鍒朵綔涓€涓甫鏈?200 涓护鐗岀殑淇″彿閲忥紙semaphore锛夛紝骞跺湪寮€濮嬫椂鑾峰彇涓€涓€傚鏋滄垜浠敤瀹屼簡浠ょ墝锛屽氨闇€绛夊緟淇″彿閲忓彂鏀句护鐗屻€?/p>
浣嗘槸绛変竴涓嬨€傜幇鍦ㄦ垜浠張鍙樻垚浜嗘帓闃燂紒鎴戜滑鍙槸鍦ㄦ洿鍓嶉潰鎺掋€傚鏋滀护绯荤粺涓ラ噸瓒呰礋鑽凤紝閭d箞鎴戜滑浼氫粠涓€寮€濮嬪氨涓€鐩村湪鎺掗槦銆傚洜姝わ紝鐜板湪姣忎釜浜洪兘灏嗙瓑寰呬粬浠効鎰忕瓑寰呯殑鏈€澶ф椂闂达紝鐒跺悗鏀惧純銆傛洿绯熺硶鐨勬槸锛氭湇鍔″櫒鍙兘浠嶄細鑺变竴娈垫椂闂村鐞嗚繖浜涜姹傦紝鐩村埌瀹冩剰璇嗗埌瀹㈡埛绔凡娑堝け锛岃€屼笖涓嶅啀瀵瑰搷搴旀劅鍏磋叮銆?/p>
鍥犳锛屼笌鍏朵竴鐩寸瓑寰呬笅鍘伙紝鎴戜滑鏇村笇鏈涚珛鍗宠幏寰楀弽棣堛€傛兂璞′綘鍦ㄤ竴涓偖灞€锛屽苟涓旀鍦ㄤ粠鏈哄櫒涓婂彇绁紝绁ㄤ笂浼氳浠€涔堟椂鍊欒疆鍒颁綘銆傝繖寮犵エ寰堝ソ鍦拌〃鏄庝簡浣犻渶瑕佺瓑寰呭闀挎椂闂淬€傚鏋滅瓑寰呮椂闂村お闀匡紝浣犱細鍐冲畾寮冪エ璧颁汉锛屼互鍚庡啀鏉ャ€傝娉ㄦ剰锛屼綘鍦ㄩ偖灞€閲岀殑鎺掗槦绛夊緟鏃堕棿锛屼笌瀹為檯澶勭悊浣犵殑璇锋眰鐨勬椂闂存棤鍏筹紙渚嬪锛屽洜涓烘湁浜洪渶瑕佹彁鍙栧寘瑁癸紝妫€鏌ユ枃浠跺苟閲囬泦绛惧悕锛夈€?/p>
鍥犳锛岃繖鏄ぉ鐪熺殑鐗堟湰锛屾垜浠彧鐭ラ亾鑷繁鍦ㄧ瓑寰咃細
from asyncio.sync import Semaphore
semaphore = Semaphore(200)
async def handle_request(request):
await semaphore.acquire()
try:
return generate_response(request)
finally:
semaphore.release()
瀵逛簬 handle_request 寮傛鍑芥暟鐨勮皟鐢ㄨ€咃紝鎴戜滑鍙兘鐪嬪埌鎴戜滑姝e湪绛夊緟骞朵笖浠€涔堥兘娌℃湁鍙戠敓銆傛垜浠湅涓嶅埌鏄洜涓鸿繃杞借€屽湪绛夊緟锛岃繕鏄洜涓虹敓鎴愬搷搴旈渶鑺辫垂寰堥暱鏃堕棿鑰屽湪绛夊緟銆傚熀鏈笂锛屾垜浠竴鐩村湪杩欓噷缂撳啿锛岀洿鍒版湇鍔″櫒鏈€缁堣€楀敖鍐呭瓨骞跺穿婧冦€?/p>
杩欐槸鍥犱负鎴戜滑娌℃湁鍏充簬鑳屽帇鐨勬矡閫氭笭閬撱€傞偅涔堟垜浠皢濡備綍瑙e喅鍛紵涓€绉嶉€夋嫨鏄坊鍔犱竴涓腑闂村眰銆傜幇鍦ㄤ笉骞哥殑鏄紝杩欓噷鐨?asyncio 淇″彿閲忔病鏈夌敤锛屽洜涓哄畠鍙細璁╂垜浠瓑寰呫€備絾鏄亣璁炬垜浠彲浠ヨ闂俊鍙烽噺杩樺墿涓嬪灏戜釜浠ょ墝锛岄偅涔堟垜浠彲浠ユ墽琛岀被浼艰繖鏍风殑鎿嶄綔锛?/p>
from hypothetical_asyncio.sync import Semaphore, Service
semaphore = Semaphore(200)
class RequestHandlerService(Service):
async def handle(self, request):
await semaphore.acquire()
try:
return generate_response(request)
finally:
semaphore.release()
@property
def is_ready(self):
return semaphore.tokens_available()
鐜板湪锛屾垜浠绯荤粺鍋氫簡涓€浜涙洿鏀广€傜幇鍦紝鎴戜滑鏈変竴涓?RequestHandlerService锛屽叾涓寘鍚簡鏇村淇℃伅銆傜壒鍒槸瀹冨叿鏈変簡鍑嗗灏辩华鐨勬蹇点€傝鏈嶅姟鍙互琚闂槸鍚﹀噯澶囧氨缁€傝鎿嶄綔鍦ㄦ湰璐ㄤ笂鏄棤闃诲鐨勶紝骞朵笖鏄渶浣充及閲忋€?/p>
鐜板湪锛岃皟鐢ㄨ€呬細灏嗚繖涓細
response = await handle_request(request)
鍙樻垚杩欎釜锛?/p>
request_handler = RequestHandlerService()
if not request_handler.is_ready:
response = Response(status_code=503)
else:
response = await request_handler.handle(request)
鏈夊绉嶆柟娉曞彲浠ュ畬鎴愶紝浣嗘槸鎬濇兂鏄竴鏍风殑銆傚湪鎴戜滑鐪熸鐫€鎵嬪仛鏌愪欢浜嬩箣鍓嶏紝鎴戜滑鏈変竴绉嶆柟娉曟潵寮勬竻妤氭垚鍔熺殑鍙兘鎬э紝濡傛灉鎴戜滑瓒呰礋鑽蜂簡锛屾垜浠皢鍚戜笂娌熼€氥€?/p>
鐜板湪锛屾垜娌℃湁鎯冲埌濡備綍缁欒繖绉嶆湇鍔′笅瀹氫箟銆傚叾璁捐鏉ヨ嚜 Rust 鐨則ower銆?銆戝拰 Rust 鐨刟ctix-service銆?銆戙€備袱鑰呭鏈嶅姟鐗瑰緛鐨勫畾涔夐兘璺熷畠闈炲父鐩镐技銆?/p>
鐜板湪锛岀敱浜庡畠鏄姝ょ殑 racy锛屽洜姝や粛鏈夊彲鑳藉爢绉俊鍙烽噺銆傜幇鍦紝浣犲彲浠ュ啋杩欑椋庨櫓锛屾垨鑰呰繕鏄湪 handle 琚皟鐢ㄦ椂灏辨姏鍑哄け璐ャ€?/p>
涓€涓瘮 asyncio 鏇村ソ鍦拌В鍐虫闂鐨勫簱鏄?trio锛屽畠浼氬湪淇″彿閲忎笂鏆撮湶鍐呴儴璁℃暟鍣紝骞舵彁渚涗竴涓?CapacityLimiter锛屽畠鏄瀹归噺闄愬埗鍋氫簡浼樺寲鐨勪俊鍙烽噺锛屽彲浠ラ槻姝竴浜涘父瑙佺殑闄烽槺銆?/p>
鏁版嵁娴佸拰鍗忚
鐜板湪锛屼笂闈㈢殑绀轰緥涓烘垜浠В鍐充簡 RPC 鏍峰紡鐨勬儏鍐点€傚浜庢瘡娆¤皟鐢紝濡傛灉绯荤粺杩囪浇浜嗭紝鎴戜滑浼氬敖鏃╁緱鐭ャ€傝澶氬崗璁兘鏈夐潪甯哥洿鎺ョ殑鏂瑰紡鏉ヤ紶杈锯€滄湇鍔″櫒姝e湪鍔犺浇鈥濈殑淇℃伅銆備緥濡傦紝鍦?HTTP 涓紝浣犲彲浠ュ彂鍑?503锛屽苟鍦?header 涓惡甯︿竴涓?retry-after 瀛楁锛屽畠浼氬憡鐭ュ鎴风浣曟椂鍙互閲嶈瘯銆傚湪涓嬫閲嶈瘯鏃朵細娣诲姞涓€涓噸鏂拌瘎浼扮殑鑷劧鐐癸紝鍒ゆ柇鏄惁瑕佷娇鐢ㄧ浉鍚岀殑璇锋眰閲嶈瘯锛屾垨鑰呮洿鏀规煇浜涘唴瀹广€備緥濡傦紝濡傛灉浣犳棤娉曞湪 15 绉掑唴閲嶈瘯锛岄偅涔堟渶濂藉悜鐢ㄦ埛鏄剧ず杩欑鏃犺兘锛岃€屼笉鏄樉绀轰竴涓棤浼戞鐨勫姞杞藉浘鏍囥€?/p>
浣嗘槸锛岃姹?鍝嶅簲锛坮equest/response锛夊紡鐨勫崗璁苟涓嶆槸鍞竴鐨勫崗璁€傝澶氬崗璁兘鎵撳紑浜嗘寔涔呰繛鎺ワ紝璁╀綘浼犺緭澶ч噺鐨勬暟鎹€傚湪浼犵粺涓婏紝杩欎簺鍗忚涓湁寰堝鏄熀浜?TCP 鐨勶紝濡傚墠鎵€杩帮紝瀹冨叿鏈夊唴缃殑娴侀噺鎺у埗銆備絾鏄紝姝ゆ祦閲忔帶鍒跺苟娌℃湁鐪熸閫氳繃濂楁帴瀛楀簱鍏紑锛岃繖灏辨槸涓轰粈涔堥珮绾у崗璁€氬父闇€瑕佸悜鍏舵坊鍔犺嚜宸辩殑娴侀噺鎺у埗鐨勫師鍥犮€備緥濡傦紝鍦?HTTP2 涓紝灏卞瓨鍦ㄤ竴涓嚜瀹氫箟娴侀噺鎺у埗鍗忚锛屽洜涓?HTTP2 鍦ㄥ崟涓?TCP 杩炴帴涓婏紝澶氳矾澶嶇敤澶氫釜鐙珛鐨勬暟鎹祦锛坰treams锛夈€?/p>
鍥犱负 TCP 鍦ㄥ悗鍙板娴侀噺鎺у埗杩涜闈欓粯寮忕鐞嗭紝杩欏彲鑳戒細浣垮紑鍙戜汉鍛橀櫡鍏ヤ竴鏉″嵄闄╃殑閬撹矾锛屼粬浠彧鐭ヤ粠濂楁帴瀛椾腑璇诲彇瀛楄妭锛屽苟璇互涓鸿繖鏄墍鏈夎鐭ラ亾鐨勪俊鎭€備絾鏄紝TCP API 鍏锋湁璇鎬э紝鍥犱负浠?API 瑙掑害鏉ョ湅锛屾祦閲忔帶鍒跺鐢ㄦ埛瀹屽叏鏄殣钘忕殑銆傚綋浣犺璁¤嚜宸辩殑鍩轰簬鏁版嵁娴佺殑鍗忚鏃讹紝浣犻渶瑕佺粷瀵圭‘淇濆瓨鍦ㄥ弻鍚戦€氫俊閫氶亾锛屽嵆鍙戦€佹柟涓嶄粎瑕佸彂閫侊紝杩樿璇诲彇锛屼互鏌ョ湅鏄惁鍏佽瀹冧滑缁х画鍙戙€?/p>
瀵逛簬鏁版嵁娴侊紝鍏虫敞鐐归€氬父鏄笉鍚岀殑銆傝澶氭暟鎹祦鍙槸瀛楄妭鎴栨暟鎹抚鐨勬祦锛屼綘涓嶈兘浠呭湪瀹冧滑涔嬮棿涓㈠純鏁版嵁鍖呫€傛洿绯熺硶鐨勬槸锛氬彂閫佹柟閫氬父涓嶅鏄撳療瑙夊埌瀹冧滑鏄惁搴旇鏀炬參閫熷害銆傚湪 HTTP2 涓紝浣犻渶瑕佸湪鐢ㄦ埛绾у埆涓婁笉鏂氦閿欏湴璇诲啓銆備綘蹇呯劧瑕佸湪閭i噷澶勭悊娴侀噺鎺у埗銆傚綋浣犲湪鍐欏苟涓旇鍏佽鍐欏叆鏃讹紝鏈嶅姟鍣ㄥ皢鍚戜綘鍙戦€?WINDOW_UPDATE 甯с€?/p>
杩欐剰鍛崇潃鏁版嵁娴佷唬鐮佸彉寰楁洿涓哄鏉傦紝鍥犱负浣犻鍏堥渶瑕佺紪鍐欎竴涓彲浠ュ浼犲叆娴侀噺浣滄帶鍒剁殑妗嗘灦銆備緥濡傦紝hyper-h2銆?銆慞ython 搴撳叿鏈変护浜烘儕璁剁殑澶嶆潅鐨勬枃浠朵笂浼犳湇鍔″櫒绀轰緥锛屻€?銆戣绀轰緥鍩轰簬 curio 鐨勬祦閲忔帶鍒讹紝浣嗘槸杩樻湭瀹屾垚銆?/p>
鏂版鏋?/span>
async/await 寰堟锛屼絾鏄畠鎵€榧撳姳缂栧啓鐨勫唴瀹瑰湪杩囪浇鏃朵細瀵艰嚧鐏鹃毦銆備竴鏂归潰鏄洜涓哄畠濡傛瀹规槗灏辨帓闃燂紝浣嗗悓鏃跺洜涓哄湪浣垮嚱鏁板彉寮傛鍚庯紝浼氶€犳垚 API 鎹熷潖銆傛垜鍙兘鍋囪杩欏氨鏄负浠€涔?Python 鍦ㄦ暟鎹祦 writer 涓婁粛鐒朵娇鐢ㄤ笉鍙瓑寰呯殑 write 鍑芥暟銆?/p>
涓嶈繃锛屾渶澶х殑鍘熷洜鏄?async/await 浣夸綘鍙互缂栧啓璁稿浜烘渶鍒濇棤娉曠敤绾跨▼缂栧啓鐨勪唬鐮併€傛垜璁や负杩欐槸涓€浠跺ソ浜嬶紝鍥犱负瀹冮檷浣庝簡瀹為檯缂栧啓澶у瀷绯荤粺鐨勯殰纰嶃€傚叾缂虹偣鏄紝杩欎篃鎰忓懗鐫€璁稿浠ュ墠瀵瑰垎甯冨紡绯荤粺缂轰箯缁忛獙鐨勫紑鍙戜汉鍛樼幇鍦ㄥ嵆浣垮彧缂栧啓涓€涓▼搴忥紝涔熼亣鍒颁簡鍒嗗竷寮忕郴缁熺殑璁稿闂銆傜敱浜庡璺鐢ㄧ殑鎬ц川锛孒TTP2 鏄竴绉嶉潪甯稿鏉傜殑鍗忚锛屽敮涓€鍚堢悊鐨勫疄鐜版柟娉曟槸鍩轰簬 async/await 鐨勪緥瀛愩€?/p>
閬囧埌杩欎簺闂鐨勪笉浠呮槸 async/await 浠g爜銆備緥濡傦紝Dask銆?銆戞槸鏁版嵁绉戝绋嬪簭鍛樹娇鐢ㄧ殑 Python 骞惰搴擄紝灏界娌℃湁浣跨敤 async/await锛屼絾鐢变簬缂轰箯鑳屽帇锛屻€?0銆戜粛鏈変竴浜?bug 鎶ュ憡鎻愮ず绯荤粺鍐呭瓨涓嶈冻銆備絾鏄繖浜涢棶棰樻槸鐩稿綋鏍规湰鐨勩€?/p>
鐒惰€岋紝鑳屽帇鐨勭己澶辨槸涓€绉嶅叿鏈夌伀绠瓛澶у皬鐨勬鏋€傚鏋滀綘澶櫄鎰忚瘑鍒拌嚜宸辨瀯寤轰簡涓€墿锛岄偅涔堝湪涓嶅浠g爜搴撹繘琛岄噸澶ф洿鏀圭殑鎯呭喌涓嬶紝鍑犱箮涓嶅彲鑳戒慨澶嶅畠锛屽洜涓轰綘鍙兘蹇樹簡鍦ㄦ煇浜涙湰搴斾娇鐢ㄥ紓姝ョ殑鍑芥暟涓婁娇鐢ㄥ紓姝ャ€?/p>
鍏跺畠鐨勭紪绋嬬幆澧冨姝や篃鏃犳祹浜庝簨銆備汉浠湪鎵€鏈夌紪绋嬬幆澧冧腑閮介亣鍒颁簡鍚屾牱鐨勯棶棰橈紝鍖呮嫭鏈€鏂扮増鏈殑 go 鍜?Rust銆傚嵆浣垮湪闀挎湡寮€婧愮殑闈炲父鍙楁杩庣殑椤圭洰涓紝鎵惧埌鏈夊叧鈥滃鐞嗘祦绋嬫帶鍒垛€濇垨鈥滃鐞嗚儗鍘嬧€濈殑寮€鏀鹃棶棰橈紙open issue锛変篃骞堕潪缃曡锛屽洜涓轰簨瀹炶瘉鏄庯紝浜嬪悗娣诲姞杩欎竴鐐圭‘瀹炲緢鍥伴毦銆備緥濡傦紝go 浠?2014 骞磋捣灏卞瓨鍦ㄤ竴涓紑鏀鹃棶棰橈紝鍏充簬缁欐墍鏈夋枃浠剁郴缁烮O娣诲姞淇″彿閲忥紝銆?1銆戝洜涓哄畠鍙兘浼氫娇涓绘満瓒呰浇銆俛iohttp 鏈変竴涓棶棰樺彲杩芥函鍒?016骞达紝銆?2銆戝叧浜庡鎴风鐢变簬鑳屽帇涓嶈冻鑰屽鑷寸牬鍧忔湇鍔″櫒銆傝繕鏈夊緢澶氬緢澶氱殑渚嬪瓙銆?/p>
濡傛灉浣犳煡鐪?Python 鐨?hyper-h2鏂囨。锛屽皢浼氱湅鍒板ぇ閲忎护浜洪渿鎯婄殑绀轰緥锛屽叾涓寘鍚被浼尖€滀笉澶勭悊娴侀噺鎺у埗鈥濄€佲€滃畠涓嶉伒瀹?HTTP/2 娴侀噺鎺у埗锛岃繖鏄竴涓己闄凤紝浣嗗湪鍏跺畠鏂归潰鏄病闂鐨勨€滐紝绛夌瓑銆傚湪娴侀噺鎺у埗涓€鍑虹幇鐨勬椂鍊欙紝鎴戝氨璁や负瀹冮潪甯稿鏉傘€傚緢瀹规槗鍋囪杩欎笉鏄釜闂锛岃繖灏辨槸涓轰粈涔堟垜浠細澶勪簬杩欑娣蜂贡鐘舵€佺殑鏍规湰鍘熷洜銆傛祦閲忔帶鍒惰繕浼氬鍔犲ぇ閲忓紑閿€锛屽苟涓斿湪鍩哄噯娴嬭瘯涓晥鏋滀笉浣炽€?/p>
閭d箞锛屽浜庝綘浠繖浜涘紓姝ュ簱寮€鍙戜汉鍛橈紝杩欓噷缁欎綘浠竴涓柊骞寸殑瑙e喅鏂规锛氬湪鏂囨。鍜?API 涓紝璧嬩簣鑳屽帇鍜屾祦閲忔帶鍒跺叾搴斿緱鐨勯噸瑙嗐€?/p>
[12] 鏈変竴涓棶棰樺彲杩芥函鍒?016骞达紝: https://github.com/aio-libs/aiohttp/issues/1368
瀹冩湁涓€浜涚儹闂癸細
以上是关于Flask 浣滆€?Armin Ronacher锛氭垜涓嶈寰楁湁寮傛鍘嬪姏的主要内容,如果未能解决你的问题,请参考以下文章
銆怑xcel銆慔YPERLINK瀵规湁鐗规畩瀛楃鐨勫伐浣滆〃鍚嶄笉鐢熸晥闂