{"id":29491,"date":"2025-04-20T09:55:58","date_gmt":"2025-04-20T01:55:58","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/29491.html"},"modified":"2025-04-20T09:55:58","modified_gmt":"2025-04-20T01:55:58","slug":"python-%e9%ab%98%e6%80%a7%e8%83%bd-asgi-%e6%9c%8d%e5%8a%a1%e5%99%a8-uvicorn-%e5%ba%93%e4%bd%bf%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/29491.html","title":{"rendered":"Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357"},"content":{"rendered":"<h2>Python Uvicorn\u5e93\u4f7f\u7528\u6307\u5357<\/h2>\n<p>Uvicorn\u662f\u4e00\u4e2a\u57fa\u4e8ePython\u7684\u9ad8\u6027\u80fdASGI(Asynchronous Server Gateway Interface)\u670d\u52a1\u5668&#xff0c;\u4e13\u4e3a\u5f02\u6b65Web\u6846\u67b6(\u5982FastAPI\u3001Starlette\u7b49)\u8bbe\u8ba1\u3002\u4ee5\u4e0b\u662fUvicorn\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u4f7f\u7528\u65b9\u6cd5&#xff1a;<\/p>\n<h3>1. Uvicorn\u7b80\u4ecb<\/h3>\n<p>Uvicorn\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u3001\u9ad8\u6027\u80fd\u7684ASGI\u670d\u52a1\u5668&#xff0c;\u7531Starlette\u6846\u67b6\u7684\u4f5c\u8005\u7f16\u5199\u3002\u5b83\u57fa\u4e8easyncio\u5e93\u5b9e\u73b0\u5f02\u6b65I\/O\u64cd\u4f5c&#xff0c;\u652f\u6301HTTP\u548cWebSocket\u534f\u8bae\u3002Uvicorn\u4f7f\u7528uvloop\u4f5c\u4e3a\u4e8b\u4ef6\u5faa\u73af\u548chttptools\u8fdb\u884cHTTP\u89e3\u6790&#xff0c;\u56e0\u6b64\u6bd4\u4f20\u7edf\u7684Python\u670d\u52a1\u5668\u66f4\u5feb\u3002<\/p>\n<p>\u4e3b\u8981\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u9ad8\u6027\u80fd&#xff1a;\u57fa\u4e8euvloop\u548chttptools\u6784\u5efa&#xff0c;\u652f\u6301\u9ad8\u541e\u5410\u91cf\u548c\u4f4e\u5ef6\u8fdf<\/li>\n<li>\u652f\u6301HTTP\/1.1\u548cWebSockets<\/li>\n<li>\u70ed\u91cd\u8f7d&#xff1a;\u5f00\u53d1\u6a21\u5f0f\u4e0b\u652f\u6301\u4ee3\u7801\u70ed\u91cd\u8f7d<\/li>\n<li>\u7075\u6d3b\u7684\u914d\u7f6e&#xff1a;\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6216Python\u4ee3\u7801\u81ea\u5b9a\u4e49\u8fd0\u884c\u9009\u9879<\/li>\n<\/ul>\n<h3>2. \u5b89\u88c5Uvicorn<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5Uvicorn&#xff1a;<\/p>\n<p>pip <span class=\"token function\">install<\/span> uvicorn<\/p>\n<p>\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u6027\u80fd&#xff0c;\u53ef\u4ee5\u5b89\u88c5\u5e26Cython\u4f18\u5316\u7684\u7248\u672c&#xff1a;<\/p>\n<p>pip <span class=\"token function\">install<\/span> <span class=\"token string\">&#034;uvicorn[standard]&#034;<\/span><\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f&#xff1a;<\/p>\n<p>uvicorn &#8211;version<\/p>\n<h3>3. \u57fa\u672c\u4f7f\u7528<\/h3>\n<h4>3.1 \u8fd0\u884c\u7b80\u5355\u7684ASGI\u5e94\u7528<\/h4>\n<p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684ASGI\u5e94\u7528\u793a\u4f8b&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">app<\/span><span class=\"token punctuation\">(<\/span>scope<span class=\"token punctuation\">,<\/span> receive<span class=\"token punctuation\">,<\/span> send<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">assert<\/span> scope<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#039;type&#039;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#039;http&#039;<\/span><br \/>\n    response <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">b&#034;Hello, Uvicorn!&#034;<\/span><br \/>\n    <span class=\"token keyword\">await<\/span> send<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token string\">&#039;type&#039;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#039;http.response.start&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token string\">&#039;status&#039;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">200<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token string\">&#039;headers&#039;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">b&#039;content-type&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">b&#039;text\/plain&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">await<\/span> send<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token string\">&#039;type&#039;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#039;http.response.body&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token string\">&#039;body&#039;<\/span><span class=\"token punctuation\">:<\/span> response<span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u4f7f\u7528Uvicorn\u8fd0\u884c\u8be5\u5e94\u7528&#xff1a;<\/p>\n<p>uvicorn app:app &#8211;host <span class=\"token number\">127.0<\/span>.0.1 &#8211;port <span class=\"token number\">8000<\/span><\/p>\n<h4>3.2 \u4e0eFastAPI\u96c6\u6210<\/h4>\n<p>Uvicorn\u5e38\u4e0eFastAPI\u4e00\u8d77\u4f7f\u7528&#xff1a;<\/p>\n<p><span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> FastAPI<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> FastAPI<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;app<span class=\"token punctuation\">.<\/span>get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">read_root<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;message&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;Welcome to Uvicorn with FastAPI!&#034;<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8fd0\u884c\u8be5\u5e94\u7528&#xff1a;<\/p>\n<p>uvicorn main:app &#8211;reload<\/p>\n<p>&#8211;reload\u53c2\u6570\u542f\u7528\u4ee3\u7801\u70ed\u91cd\u8f7d\u529f\u80fd&#xff0c;\u6bcf\u6b21\u4fee\u6539\u4ee3\u7801\u540e\u670d\u52a1\u5668\u5c06\u81ea\u52a8\u91cd\u542f<\/p>\n<h3>4. \u914d\u7f6e\u9009\u9879<\/h3>\n<p>Uvicorn\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u914d\u7f6e\u9009\u9879&#xff1a;<\/p>\n<table>\n<tr>\u53c2\u6570\u63cf\u8ff0\u9ed8\u8ba4\u503c<\/tr>\n<tbody>\n<tr>\n<td>&#8211;host<\/td>\n<td>\u6307\u5b9a\u4e3b\u673a\u5730\u5740<\/td>\n<td>127.0.0.1<\/td>\n<\/tr>\n<tr>\n<td>&#8211;port<\/td>\n<td>\u6307\u5b9a\u7aef\u53e3\u53f7<\/td>\n<td>8000<\/td>\n<\/tr>\n<tr>\n<td>&#8211;workers<\/td>\n<td>\u6307\u5b9a\u5de5\u4f5c\u8fdb\u7a0b\u6570\u91cf<\/td>\n<td>CPU\u6838\u5fc3\u6570\u76841\u500d<\/td>\n<\/tr>\n<tr>\n<td>&#8211;log-level<\/td>\n<td>\u6307\u5b9a\u65e5\u5fd7\u7ea7\u522b<\/td>\n<td>info<\/td>\n<\/tr>\n<tr>\n<td>&#8211;reload<\/td>\n<td>\u5728\u4ee3\u7801\u4fee\u6539\u65f6\u81ea\u52a8\u91cd\u65b0\u52a0\u8f7d\u5e94\u7528\u7a0b\u5e8f<\/td>\n<td>\u65e0<\/td>\n<\/tr>\n<tr>\n<td>&#8211;ssl-keyfile<\/td>\n<td>SSL\u5bc6\u94a5\u6587\u4ef6\u8def\u5f84<\/td>\n<td>\u65e0<\/td>\n<\/tr>\n<tr>\n<td>&#8211;ssl-certfile<\/td>\n<td>SSL\u8bc1\u4e66\u6587\u4ef6\u8def\u5f84<\/td>\n<td>\u65e0<\/td>\n<\/tr>\n<tr>\n<td>&#8211;backlog<\/td>\n<td>\u8bbe\u7f6e\u8fde\u63a5\u8bf7\u6c42\u7684\u6700\u5927\u7b49\u5f85\u961f\u5217\u6570<\/td>\n<td>2048<\/td>\n<\/tr>\n<tr>\n<td>&#8211;limit-max-requests<\/td>\n<td>\u9650\u5236\u5355\u4e2a\u8fdb\u7a0b\u5728\u91cd\u542f\u524d\u53ef\u5904\u7406\u7684\u6700\u5927\u8bf7\u6c42\u6570<\/td>\n<td>1000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>5. \u9ad8\u7ea7\u529f\u80fd<\/h3>\n<h4>5.1 SSL\u652f\u6301<\/h4>\n<p>Uvicorn\u652f\u6301\u901a\u8fc7SSL\u52a0\u5bc6\u6765\u63d0\u4f9b\u5b89\u5168\u7684\u901a\u4fe1&#xff1a;<\/p>\n<p>uvicorn main:app &#8211;ssl-keyfile key.pem &#8211;ssl-certfile cert.pem<\/p>\n<h4>5.2 WebSocket\u652f\u6301<\/h4>\n<p>Uvicorn\u652f\u6301\u5904\u7406WebSocket\u8fde\u63a5&#xff1a;<\/p>\n<p><span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> FastAPI<span class=\"token punctuation\">,<\/span> WebSocket<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> FastAPI<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;app<span class=\"token punctuation\">.<\/span>websocket<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/ws&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">websocket_endpoint<\/span><span class=\"token punctuation\">(<\/span>websocket<span class=\"token punctuation\">:<\/span> WebSocket<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">await<\/span> websocket<span class=\"token punctuation\">.<\/span>accept<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        data <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">await<\/span> websocket<span class=\"token punctuation\">.<\/span>receive_text<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">await<\/span> websocket<span class=\"token punctuation\">.<\/span>send_text<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;Message text was: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>data<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h4>5.3 \u4e2d\u95f4\u4ef6<\/h4>\n<p>Uvicorn\u652f\u6301\u4f7f\u7528\u4e2d\u95f4\u4ef6\u6765\u4fee\u6539\u8bf7\u6c42\u548c\u54cd\u5e94&#xff1a;<\/p>\n<p><span class=\"token keyword\">from<\/span> uvicorn<span class=\"token punctuation\">.<\/span>middleware<span class=\"token punctuation\">.<\/span>proxy_headers <span class=\"token keyword\">import<\/span> ProxyHeadersMiddleware<br \/>\n<span class=\"token keyword\">from<\/span> uvicorn<span class=\"token punctuation\">.<\/span>middleware<span class=\"token punctuation\">.<\/span>debug <span class=\"token keyword\">import<\/span> DebugMiddleware<br \/>\n<span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> FastAPI<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> FastAPI<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>app<span class=\"token punctuation\">.<\/span>add_middleware<span class=\"token punctuation\">(<\/span>ProxyHeadersMiddleware<span class=\"token punctuation\">,<\/span> trusted_hosts<span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;10.0.0.1&#034;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\napp<span class=\"token punctuation\">.<\/span>add_middleware<span class=\"token punctuation\">(<\/span>DebugMiddleware<span class=\"token punctuation\">)<\/span><\/p>\n<h4>5.4 \u5f02\u6b65\u4efb\u52a1<\/h4>\n<p>Uvicorn\u652f\u6301\u5728\u5f02\u6b65Web\u670d\u52a1\u4e2d\u6267\u884c\u5f02\u6b65\u4efb\u52a1&#xff1a;<\/p>\n<p><span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> FastAPI<br \/>\n<span class=\"token keyword\">import<\/span> asyncio<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> FastAPI<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">background_task<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Background task is running&#8230;&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">await<\/span> asyncio<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;app<span class=\"token punctuation\">.<\/span>on_event<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;startup&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">startup_event<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    asyncio<span class=\"token punctuation\">.<\/span>create_task<span class=\"token punctuation\">(<\/span>background_task<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h3>6. \u751f\u4ea7\u73af\u5883\u90e8\u7f72<\/h3>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;\u901a\u5e38\u4f1a\u5c06Uvicorn\u4e0eGunicorn\u7ed3\u5408\u4f7f\u7528&#xff1a;<\/p>\n<p>gunicorn -w <span class=\"token number\">4<\/span> -k uvicorn.workers.UvicornWorker main:app<\/p>\n<p>\u6b64\u547d\u4ee4\u4f1a\u542f\u52a84\u4e2a\u5de5\u4f5c\u8fdb\u7a0b&#xff0c;\u5e76\u4f7f\u7528UvicornWorker\u7ba1\u7406ASGI\u5e94\u7528\u7684\u591a\u8fdb\u7a0b\u73af\u5883\u3002<\/p>\n<h3>7. \u65e5\u5fd7\u914d\u7f6e<\/h3>\n<p>Uvicorn\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65e5\u5fd7\u914d\u7f6e\u9009\u9879&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> logging<br \/>\n<span class=\"token keyword\">import<\/span> uvicorn<\/p>\n<p><span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    logging<span class=\"token punctuation\">.<\/span>basicConfig<span class=\"token punctuation\">(<\/span>level<span class=\"token operator\">&#061;<\/span>logging<span class=\"token punctuation\">.<\/span>INFO<span class=\"token punctuation\">,<\/span> <span class=\"token builtin\">format<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;%(asctime)s &#8211; %(message)s&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    uvicorn<span class=\"token punctuation\">.<\/span>run<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;main:app&#034;<\/span><span class=\"token punctuation\">,<\/span> log_level<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;info&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h3>8. \u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<h4>8.1 \u5f02\u6b65API\u670d\u52a1<\/h4>\n<p><span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> FastAPI<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> FastAPI<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;app<span class=\"token punctuation\">.<\/span>get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/items\/{item_id}&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">read_item<\/span><span class=\"token punctuation\">(<\/span>item_id<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;item_id&#034;<\/span><span class=\"token punctuation\">:<\/span> item_id<span class=\"token punctuation\">}<\/span><\/p>\n<h4>8.2 \u5b9e\u65f6\u804a\u5929\u5e94\u7528<\/h4>\n<p><span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> FastAPI<span class=\"token punctuation\">,<\/span> WebSocket<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> FastAPI<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;app<span class=\"token punctuation\">.<\/span>websocket<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/chat&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">websocket_chat<\/span><span class=\"token punctuation\">(<\/span>websocket<span class=\"token punctuation\">:<\/span> WebSocket<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">await<\/span> websocket<span class=\"token punctuation\">.<\/span>accept<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        data <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">await<\/span> websocket<span class=\"token punctuation\">.<\/span>receive_text<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">await<\/span> websocket<span class=\"token punctuation\">.<\/span>send_text<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;Message: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>data<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h3>9. \u5de5\u4f5c\u539f\u7406<\/h3>\n<p>Uvicorn\u8fd0\u884c\u65f6\u5206\u4e3a\u4e24\u5927\u90e8\u5206&#xff1a;<\/p>\n<li>Uvicorn\u672c\u8eab\u7684\u5faa\u73af&#xff0c;\u7528\u4e8e\u670d\u52a1\u5668\u81ea\u8eab\u72b6\u6001\u7684\u7ef4\u62a4<\/li>\n<li>\u7531Uvicorn\u5efa\u7acb\u7684Asyncio\u670d\u52a1\u5668&#xff0c;\u63d0\u4ea4\u7531h11\u6216httptools\u534f\u8bae\u5e93\u5b9e\u73b0\u7684Protocol<\/li>\n<p>Uvicorn\u652f\u6301\u591a\u79cdHTTP\u534f\u8bae\u5b9e\u73b0&#xff1a;<\/p>\n<ul>\n<li>\u201cauto\u201d: \u81ea\u52a8\u9009\u62e9(\u9ed8\u8ba4)<\/li>\n<li>\u201ch11\u201d: \u57fa\u4e8eh11\u534f\u8bae\u7684\u5b9e\u73b0<\/li>\n<li>\u201chttptools\u201d: \u57fa\u4e8ehttptools\u7684\u5b9e\u73b0(\u6027\u80fd\u66f4\u9ad8)<\/li>\n<\/ul>\n<h3>10. \u4e0eWSGI\u670d\u52a1\u5668\u7684\u533a\u522b<\/h3>\n<table>\n<tr>\u7279\u6027Uvicorn (ASGI)Gunicorn (WSGI)<\/tr>\n<tbody>\n<tr>\n<td>\u63a5\u53e3\u6807\u51c6<\/td>\n<td>ASGI<\/td>\n<td>WSGI<\/td>\n<\/tr>\n<tr>\n<td>\u5f02\u6b65\u652f\u6301<\/td>\n<td>\u5b8c\u5168\u652f\u6301<\/td>\n<td>\u6709\u9650\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd<\/td>\n<td>\u66f4\u9ad8&#xff0c;\u9002\u5408\u9ad8\u5e76\u53d1<\/td>\n<td>\u8f83\u4f4e<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u6846\u67b6<\/td>\n<td>FastAPI, Starlette<\/td>\n<td>Flask, Django<\/td>\n<\/tr>\n<tr>\n<td>\u534f\u8bae\u652f\u6301<\/td>\n<td>HTTP\/1.1, WebSockets<\/td>\n<td>HTTP\/1.1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u603b\u7ed3<\/h3>\n<p>Uvicorn\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u3001\u914d\u7f6e\u7075\u6d3b\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5f02\u6b65Web\u670d\u52a1\u5668&#xff0c;\u975e\u5e38\u9002\u5408\u73b0\u4ee3Python\u5f02\u6b65\u6846\u67b6\u7684\u5f00\u53d1\u548c\u90e8\u7f72\u3002\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u548c\u7ed3\u5408Gunicorn&#xff0c;\u53ef\u4ee5\u5145\u5206\u5229\u7528\u5176\u9ad8\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u6765\u5904\u7406\u5404\u79cdWeb\u8bf7\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb690\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf18\u6b21\u3002Uvicorn\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u3001\u9ad8\u6027\u80fd\u7684ASGI\u670d\u52a1\u5668\uff0c\u7531Starlette\u6846\u67b6\u7684\u4f5c\u8005\u7f16\u5199\u3002\u5b83\u57fa\u4e8easyncio\u5e93\u5b9e\u73b0\u5f02\u6b65I\/O\u64cd\u4f5c\uff0c\u652f\u6301HTTP\u548cWebSocket\u534f\u8bae\u3002Uvicorn\u4f7f\u7528uvloop\u4f5c\u4e3a\u4e8b\u4ef6\u5faa\u73af\u548chttptools\u8fdb\u884cHTTP\u89e3\u6790\uff0c\u56e0\u6b64\u6bd4\u4f20\u7edf\u7684Python\u670d\u52a1\u5668\u66f4\u5feb\u3002\u9ad8\u6027\u80fd\uff1a\u57fa\u4e8euvloop\u548chttptools\u6784\u5efa\uff0c\u652f\u6301\u9ad8\u541e\u5410\u91cf\u548c\u4f4e\u5ef6\u8fdf\u652f\u6301HTTP\/1.1\u548cWebSockets\u70ed\u91cd\u8f7d\uff1a\u5f00\u53d1\u6a21\u5f0f\u4e0b\u652f\u6301\u4ee3\u7801\u70ed\u91cd\u8f7d\u7075\u6d3b\u7684\u914d\u7f6e\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6216Python\u4ee3\u7801\u81ea\u5b9a\u4e49\u8fd0\u884c\u9009\u9879\u3002_uvicorn windows\u91cd\u542f<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,190,43],"topic":[],"class_list":["post-29491","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-190","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/29491.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb690\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf18\u6b21\u3002Uvicorn\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u3001\u9ad8\u6027\u80fd\u7684ASGI\u670d\u52a1\u5668\uff0c\u7531Starlette\u6846\u67b6\u7684\u4f5c\u8005\u7f16\u5199\u3002\u5b83\u57fa\u4e8easyncio\u5e93\u5b9e\u73b0\u5f02\u6b65I\/O\u64cd\u4f5c\uff0c\u652f\u6301HTTP\u548cWebSocket\u534f\u8bae\u3002Uvicorn\u4f7f\u7528uvloop\u4f5c\u4e3a\u4e8b\u4ef6\u5faa\u73af\u548chttptools\u8fdb\u884cHTTP\u89e3\u6790\uff0c\u56e0\u6b64\u6bd4\u4f20\u7edf\u7684Python\u670d\u52a1\u5668\u66f4\u5feb\u3002\u9ad8\u6027\u80fd\uff1a\u57fa\u4e8euvloop\u548chttptools\u6784\u5efa\uff0c\u652f\u6301\u9ad8\u541e\u5410\u91cf\u548c\u4f4e\u5ef6\u8fdf\u652f\u6301HTTP\/1.1\u548cWebSockets\u70ed\u91cd\u8f7d\uff1a\u5f00\u53d1\u6a21\u5f0f\u4e0b\u652f\u6301\u4ee3\u7801\u70ed\u91cd\u8f7d\u7075\u6d3b\u7684\u914d\u7f6e\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6216Python\u4ee3\u7801\u81ea\u5b9a\u4e49\u8fd0\u884c\u9009\u9879\u3002_uvicorn windows\u91cd\u542f\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/29491.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-20T01:55:58+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/29491.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/29491.html\",\"name\":\"Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-20T01:55:58+00:00\",\"dateModified\":\"2025-04-20T01:55:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/29491.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/29491.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/29491.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/29491.html","og_locale":"zh_CN","og_type":"article","og_title":"Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb690\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf18\u6b21\u3002Uvicorn\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u3001\u9ad8\u6027\u80fd\u7684ASGI\u670d\u52a1\u5668\uff0c\u7531Starlette\u6846\u67b6\u7684\u4f5c\u8005\u7f16\u5199\u3002\u5b83\u57fa\u4e8easyncio\u5e93\u5b9e\u73b0\u5f02\u6b65I\/O\u64cd\u4f5c\uff0c\u652f\u6301HTTP\u548cWebSocket\u534f\u8bae\u3002Uvicorn\u4f7f\u7528uvloop\u4f5c\u4e3a\u4e8b\u4ef6\u5faa\u73af\u548chttptools\u8fdb\u884cHTTP\u89e3\u6790\uff0c\u56e0\u6b64\u6bd4\u4f20\u7edf\u7684Python\u670d\u52a1\u5668\u66f4\u5feb\u3002\u9ad8\u6027\u80fd\uff1a\u57fa\u4e8euvloop\u548chttptools\u6784\u5efa\uff0c\u652f\u6301\u9ad8\u541e\u5410\u91cf\u548c\u4f4e\u5ef6\u8fdf\u652f\u6301HTTP\/1.1\u548cWebSockets\u70ed\u91cd\u8f7d\uff1a\u5f00\u53d1\u6a21\u5f0f\u4e0b\u652f\u6301\u4ee3\u7801\u70ed\u91cd\u8f7d\u7075\u6d3b\u7684\u914d\u7f6e\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6216Python\u4ee3\u7801\u81ea\u5b9a\u4e49\u8fd0\u884c\u9009\u9879\u3002_uvicorn windows\u91cd\u542f","og_url":"https:\/\/www.wsisp.com\/helps\/29491.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-20T01:55:58+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/29491.html","url":"https:\/\/www.wsisp.com\/helps\/29491.html","name":"Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-20T01:55:58+00:00","dateModified":"2025-04-20T01:55:58+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/29491.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/29491.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/29491.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python \u9ad8\u6027\u80fd ASGI \u670d\u52a1\u5668 Uvicorn \u5e93\u4f7f\u7528\u6307\u5357"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/29491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=29491"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/29491\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=29491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=29491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=29491"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=29491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}