Flask 浣滆€?Armin Ronacher锛氭垜涓嶈寰楁湁寮傛鍘嬪姏

Posted Python鐚?/a> Fl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask 浣滆€?Armin Ronacher锛氭垜涓嶈寰楁湁寮傛鍘嬪姏相关的知识,希望对你有一定的参考价值。


馃憜  鈥?/span> Python鐚?/span> 鈥?锛屼竴涓€煎緱鍔犳槦鏍囩殑 鍏紬鍙?/span>

鑻辨枃 | I'm not feeling the async pressure

鍘熶綔 | Armin Ronacher锛?020.01.01

璇戣€?/strong> | 璞岃眴鑺变笅鐚獲Python鐚?/span>

澹版槑 锛?/span> 鏈炕璇戝熀浜?/span> CC BY-NC-SA 4.0 鎺堟潈鍗忚锛屽唴瀹圭暐鏈夋敼鍔紝杞浇璇蜂繚鐣欏師鏂囧嚭澶勶紝璇峰嬁鐢ㄤ簬鍟嗕笟鎴栭潪娉曠敤閫斻€?/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>

鐩稿叧閾炬帴
[1] I'm not feeling the async pressure: https://lucumr.pocoo.org/2020/1/1/async-pressure/
[2] CC BY-NC-SA 4.0: https://creativecommons.org/licenses/by-nc-sa/4.0/
[3] Backpressure explained 鈥?the resisted flow of data through software: https://medium.com/@jayphelps/backpressure-explained-the-flow-of-data-through-software-2350b3e77ce7
[4] 娴侀噺鎺у埗: https://en.wikipedia.org/wiki/Flow_control_(data)
[5] tower: https://github.com/tower-rs/tower
[6] actix-service: https://docs.rs/actix-service/
[7] hyper-h2: https://github.com/python-hyper/hyper-h2
[8] 鏂囦欢涓婁紶鏈嶅姟鍣ㄧず渚? https://python-hyper.org/projects/h2/en/stable/curio-example.html
[9] Dask: https://dask.org/
[10] 鑳屽帇: https://github.com/dask/distributed/issues/2602
[11] 鍏充簬缁欐墍鏈夋枃浠剁郴缁烮O娣诲姞淇″彿閲? https://github.com/golang/go/issues/7903

[12] 鏈変竴涓棶棰樺彲杩芥函鍒?016骞达紝: https://github.com/aio-libs/aiohttp/issues/1368

Flask 浣滆€?Armin Ronacher锛氭垜涓嶈寰楁湁寮傛鍘嬪姏
瀹冩湁涓€浜涙缁忥細

Flask 浣滆€?Armin Ronacher锛氭垜涓嶈寰楁湁寮傛鍘嬪姏

瀹冩湁涓€浜涘畨闈欙細

瀹冩湁涓€浜涚儹闂癸細

以上是关于Flask 浣滆€?Armin Ronacher锛氭垜涓嶈寰楁湁寮傛鍘嬪姏的主要内容,如果未能解决你的问题,请参考以下文章

銆怑xcel銆慔YPERLINK瀵规湁鐗规畩瀛楃鐨勫伐浣滆〃鍚嶄笉鐢熸晥闂

Flask 在其 URL 路由中是不是支持正则表达式?

Flutter 鍑変簡鍚楋紵

Spring 瀹炰緥鍖栨柟寮忔湁鍑犵锛熶负浠€涔堜細鐢ㄥ埌 Cglib锛?

鍐嶈锛孠otlin 锛佷綘濂? Java 锛?/h1>