{"id":107552,"date":"2026-09-19T20:21:33","date_gmt":"2026-09-19T12:21:33","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/107552.html"},"modified":"2026-09-19T20:21:33","modified_gmt":"2026-09-19T12:21:33","slug":"python-%e5%9f%ba%e7%a1%80%e8%bf%9b%e9%98%b6%ef%bc%88%e4%b8%89%ef%bc%89%ef%bc%9a%e4%bb%8e%e5%87%bd%e6%95%b0%e3%80%81%e7%b1%bb%e5%88%b0-asyncio-%e4%b8%8e-fastapi-%e5%90%8e%e7%ab%af%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/107552.html","title":{"rendered":"Python \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E5%87%BD%E6%95%B0%EF%BC%9A%E5%B0%86%E9%87%8D%E5%A4%8D%E9%80%BB%E8%BE%91%E5%B0%81%E8%A3%85%E8%B5%B7%E6%9D%A5-toc\" style=\"margin-left:40px\">\u4e00\u3001\u51fd\u6570&#xff1a;\u5c06\u91cd\u590d\u903b\u8f91\u5c01\u88c5\u8d77\u6765<\/p>\n<p id=\"1.1%20%E9%BB%98%E8%AE%A4%E5%8F%82%E6%95%B0-toc\" style=\"margin-left:80px\">1.1 \u9ed8\u8ba4\u53c2\u6570<\/p>\n<p id=\"1.2%20%E5%85%B3%E9%94%AE%E5%AD%97%E5%8F%82%E6%95%B0-toc\" style=\"margin-left:80px\">1.2 \u5173\u952e\u5b57\u53c2\u6570<\/p>\n<p id=\"1.3%20%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%20*args%20%E4%B8%8E%20**kwargs-toc\" style=\"margin-left:80px\">1.3 \u53ef\u53d8\u53c2\u6570 *args \u4e0e **kwargs<\/p>\n<p id=\"1.4%20%E5%B0%8F%E6%A1%88%E4%BE%8B%EF%BC%9A%E8%AE%A2%E5%8D%95%E9%87%91%E9%A2%9D%E8%AE%A1%E7%AE%97-toc\" style=\"margin-left:80px\">1.4 \u5c0f\u6848\u4f8b&#xff1a;\u8ba2\u5355\u91d1\u989d\u8ba1\u7b97<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E7%B1%BB%E4%B8%8E%E5%AF%B9%E8%B1%A1%EF%BC%9A%E8%AE%A9%E6%95%B0%E6%8D%AE%E5%92%8C%E8%A1%8C%E4%B8%BA%E6%94%BE%E5%9C%A8%E4%B8%80%E8%B5%B7-toc\" style=\"margin-left:40px\">\u4e8c\u3001\u7c7b\u4e0e\u5bf9\u8c61&#xff1a;\u8ba9\u6570\u636e\u548c\u884c\u4e3a\u653e\u5728\u4e00\u8d77<\/p>\n<p id=\"2.1%20__init__%20%E6%98%AF%E4%BB%80%E4%B9%88-toc\" style=\"margin-left:80px\">2.1 __init__ \u662f\u4ec0\u4e48<\/p>\n<p id=\"2.2%20%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%85%B3%E7%B3%BB-toc\" style=\"margin-left:80px\">2.2 \u7c7b\u548c\u5bf9\u8c61\u7684\u5173\u7cfb<\/p>\n<p id=\"2.3%20%E5%B0%8F%E6%A1%88%E4%BE%8B%EF%BC%9A%E7%AE%80%E5%8D%95%E4%BB%BB%E5%8A%A1%E7%B1%BB-toc\" style=\"margin-left:80px\">2.3 \u5c0f\u6848\u4f8b&#xff1a;\u7b80\u5355\u4efb\u52a1\u7c7b<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%9A%E4%B8%8D%E4%BF%AE%E6%94%B9%E5%8E%9F%E5%87%BD%E6%95%B0%E4%B9%9F%E8%83%BD%E5%A2%9E%E5%BC%BA%E5%8A%9F%E8%83%BD-toc\" style=\"margin-left:40px\">\u4e09\u3001\u88c5\u9970\u5668&#xff1a;\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4e5f\u80fd\u589e\u5f3a\u529f\u80fd<\/p>\n<p id=\"3.1%20%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84%E8%A3%85%E9%A5%B0%E5%99%A8-toc\" style=\"margin-left:80px\">3.1 \u6700\u7b80\u5355\u7684\u88c5\u9970\u5668<\/p>\n<p id=\"3.2%20%E6%94%AF%E6%8C%81%E4%BB%BB%E6%84%8F%E5%8F%82%E6%95%B0%E7%9A%84%E8%A3%85%E9%A5%B0%E5%99%A8-toc\" style=\"margin-left:80px\">3.2 \u652f\u6301\u4efb\u610f\u53c2\u6570\u7684\u88c5\u9970\u5668<\/p>\n<p id=\"3.3%20FastAPI%20%E4%B8%AD%E7%9A%84%E8%A3%85%E9%A5%B0%E5%99%A8-toc\" style=\"margin-left:80px\">3.3 FastAPI \u4e2d\u7684\u88c5\u9970\u5668<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E8%BF%AD%E4%BB%A3%E5%99%A8%E4%B8%8E%E7%94%9F%E6%88%90%E5%99%A8-toc\" style=\"margin-left:40px\">\u56db\u3001\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668<\/p>\n<p id=\"4.1%20%E4%BB%80%E4%B9%88%E6%98%AF%E8%BF%AD%E4%BB%A3-toc\" style=\"margin-left:80px\">4.1 \u4ec0\u4e48\u662f\u8fed\u4ee3<\/p>\n<p id=\"4.2%20%E4%BB%80%E4%B9%88%E6%98%AF%E8%BF%AD%E4%BB%A3%E5%99%A8-toc\" style=\"margin-left:80px\">4.2 \u4ec0\u4e48\u662f\u8fed\u4ee3\u5668<\/p>\n<p id=\"4.3%20%E8%87%AA%E5%AE%9A%E4%B9%89%E8%BF%AD%E4%BB%A3%E5%99%A8-toc\" style=\"margin-left:80px\">4.3 \u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668<\/p>\n<p id=\"4.4%20%E4%BB%80%E4%B9%88%E6%98%AF%E7%94%9F%E6%88%90%E5%99%A8-toc\" style=\"margin-left:80px\">4.4 \u4ec0\u4e48\u662f\u751f\u6210\u5668<\/p>\n<p id=\"4.5%20%E5%B0%8F%E6%A1%88%E4%BE%8B%EF%BC%9A%E5%88%86%E6%89%B9%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE-toc\" style=\"margin-left:80px\">4.5 \u5c0f\u6848\u4f8b&#xff1a;\u5206\u6279\u8bfb\u53d6\u6570\u636e<\/p>\n<p id=\"%E4%BA%94%E3%80%81asyncio%EF%BC%9APython%20%E7%9A%84%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80-toc\" style=\"margin-left:40px\">\u4e94\u3001asyncio&#xff1a;Python \u7684\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840<\/p>\n<p id=\"5.1%20%E5%90%8C%E6%AD%A5%E6%89%A7%E8%A1%8C%E7%9A%84%E9%97%AE%E9%A2%98-toc\" style=\"margin-left:80px\">5.1 \u540c\u6b65\u6267\u884c\u7684\u95ee\u9898<\/p>\n<p id=\"5.2%20%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C-toc\" style=\"margin-left:80px\">5.2 \u5f02\u6b65\u6267\u884c<\/p>\n<p id=\"5.3%20async%20%E4%B8%8E%20await-toc\" style=\"margin-left:80px\">5.3 async \u4e0e await<\/p>\n<p id=\"5.4%20%E4%B8%80%E4%B8%AA%E9%87%8D%E8%A6%81%E8%AF%AF%E5%8C%BA-toc\" style=\"margin-left:80px\">5.4 \u4e00\u4e2a\u91cd\u8981\u8bef\u533a<\/p>\n<p id=\"%E5%85%AD%E3%80%81FastAPI%EF%BC%9A%E4%BB%8E%E9%9B%B6%E5%86%99%E4%B8%80%E4%B8%AA%E4%BB%BB%E5%8A%A1%E7%AE%A1%E7%90%86%E5%90%8E%E7%AB%AF-toc\" style=\"margin-left:40px\">\u516d\u3001FastAPI&#xff1a;\u4ece\u96f6\u5199\u4e00\u4e2a\u4efb\u52a1\u7ba1\u7406\u540e\u7aef<\/p>\n<p id=\"6.1%20%E5%AE%89%E8%A3%85%20FastAPI-toc\" style=\"margin-left:40px\">6.1 \u5b89\u88c5 FastAPI<\/p>\n<p id=\"6.2%20%E7%AC%AC%E4%B8%80%E4%B8%AA%20FastAPI%20%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:40px\">6.2 \u7b2c\u4e00\u4e2a FastAPI \u63a5\u53e3<\/p>\n<p id=\"6.3%20%E8%B7%AF%E5%BE%84%E5%8F%82%E6%95%B0-toc\" style=\"margin-left:40px\">6.3 \u8def\u5f84\u53c2\u6570<\/p>\n<p id=\"6.4%20%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0-toc\" style=\"margin-left:40px\">6.4 \u67e5\u8be2\u53c2\u6570<\/p>\n<p id=\"%E4%B8%83%E3%80%81FastAPI%20%E5%B0%8F%E9%A1%B9%E7%9B%AE%EF%BC%9A%E4%BB%BB%E5%8A%A1%E7%AE%A1%E7%90%86%20API-toc\" style=\"margin-left:40px\">\u4e03\u3001FastAPI \u5c0f\u9879\u76ee&#xff1a;\u4efb\u52a1\u7ba1\u7406 API<\/p>\n<p id=\"7.1%20Pydantic%20%E6%A8%A1%E5%9E%8B%E7%9A%84%E4%BD%9C%E7%94%A8-toc\" style=\"margin-left:80px\">7.1 Pydantic \u6a21\u578b\u7684\u4f5c\u7528<\/p>\n<p id=\"7.2%20%E6%B5%8B%E8%AF%95%E6%96%B0%E5%A2%9E%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:80px\">7.2 \u6d4b\u8bd5\u65b0\u589e\u4efb\u52a1\u63a5\u53e3<\/p>\n<p id=\"7.3%20%E6%B5%8B%E8%AF%95%E4%BF%AE%E6%94%B9%E4%BB%BB%E5%8A%A1%E7%8A%B6%E6%80%81-toc\" style=\"margin-left:80px\">7.3 \u6d4b\u8bd5\u4fee\u6539\u4efb\u52a1\u72b6\u6001<\/p>\n<p id=\"7.4%20HTTPException%20%E7%9A%84%E4%BD%9C%E7%94%A8-toc\" style=\"margin-left:80px\">7.4 HTTPException \u7684\u4f5c\u7528<\/p>\n<p id=\"%E5%85%AB%E3%80%81FastAPI%20%E4%B8%8E%20asyncio%20%E7%9A%84%E5%85%B3%E7%B3%BB-toc\" style=\"margin-left:40px\">\u516b\u3001FastAPI \u4e0e asyncio \u7684\u5173\u7cfb<\/p>\n<p id=\"%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:40px\">\u603b\u7ed3<\/p>\n<p>\u5728\u524d\u4e24\u7bc7\u4e2d&#xff0c;\u6211\u4eec\u5b66\u4e60\u4e86 Python \u5e38\u7528\u5bb9\u5668&#xff0c;\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u96c6\u5408\u548c\u5b57\u5178\u3002 <span style=\"color:#be191c\">\u5bb9\u5668\u89e3\u51b3\u7684\u662f\u201c\u5982\u4f55\u4fdd\u5b58\u4e00\u6279\u6570\u636e\u201d\u7684\u95ee\u9898&#xff1b;\u800c\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u9700\u8981\u5b66\u4e60\u201c\u5982\u4f55\u7ec4\u7ec7\u548c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u201d\u3002<\/span> \u672c\u7bc7\u5c06\u7ee7\u7eed\u5b66\u4e60 Python \u51fd\u6570\u3001\u7c7b\u3001\u88c5\u9970\u5668\u3001\u8fed\u4ee3\u5668\u3001\u751f\u6210\u5668\u3001asyncio&#xff0c;\u5e76\u91cd\u70b9\u5165\u95e8 FastAPI \u540e\u7aef\u5f00\u53d1\u3002<\/p>\n<h3 id=\"%E4%B8%80%E3%80%81%E5%87%BD%E6%95%B0%EF%BC%9A%E5%B0%86%E9%87%8D%E5%A4%8D%E9%80%BB%E8%BE%91%E5%B0%81%E8%A3%85%E8%B5%B7%E6%9D%A5\" style=\"background-color:transparent\">\u4e00\u3001\u51fd\u6570&#xff1a;\u5c06\u91cd\u590d\u903b\u8f91\u5c01\u88c5\u8d77\u6765<\/h3>\n<p>\u51fd\u6570\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u6bb5\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u3002\u4f8b\u5982&#xff0c;\u6211\u4eec\u9700\u8981\u591a\u6b21\u8ba1\u7b97\u5546\u54c1\u8ba2\u5355\u603b\u4ef7&#xff0c;\u5982\u679c\u6bcf\u6b21\u90fd\u624b\u52a8\u5199\u8ba1\u7b97\u903b\u8f91&#xff0c;\u4ee3\u7801\u4f1a\u91cd\u590d\u4e14\u4e0d\u5229\u4e8e\u7ef4\u62a4\u3002<\/p>\n<p>price &#061; 99.9<br \/>\ncount &#061; 2<br \/>\ntotal &#061; price * count<\/p>\n<p>print(total) <\/p>\n<p>\u53ef\u4ee5\u628a\u5b83\u5c01\u88c5\u6210\u51fd\u6570&#xff1a;<\/p>\n<p>def calculate_total(price, count):<br \/>\n    return price * count<\/p>\n<p>result &#061; calculate_total(99.9, 2)<\/p>\n<p>print(result) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>199.8 <\/p>\n<p>\u51fd\u6570\u7531\u4ee5\u4e0b\u90e8\u5206\u7ec4\u6210&#xff1a;<\/p>\n<p>def \u51fd\u6570\u540d(\u53c2\u6570):<br \/>\n    \u51fd\u6570\u4f53<br \/>\n    return \u8fd4\u56de\u503c <\/p>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<ul>\n<li>def&#xff1a;\u5b9a\u4e49\u51fd\u6570\u7684\u5173\u952e\u5b57&#xff1b;<\/li>\n<li>\u51fd\u6570\u540d&#xff1a;\u5efa\u8bae\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf&#xff1b;<\/li>\n<li>\u53c2\u6570&#xff1a;\u51fd\u6570\u9700\u8981\u63a5\u6536\u7684\u6570\u636e&#xff1b;<\/li>\n<li>return&#xff1a;\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002<\/li>\n<\/ul>\n<h4 id=\"1.1%20%E9%BB%98%E8%AE%A4%E5%8F%82%E6%95%B0\">1.1 \u9ed8\u8ba4\u53c2\u6570<\/h4>\n<p>\u6709\u65f6\u51fd\u6570\u7684\u67d0\u4e2a\u53c2\u6570\u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u503c&#xff0c;\u53ef\u4ee5\u7ed9\u5b83\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/p>\n<p>def greet(name, message&#061;&#034;\u4f60\u597d&#034;):<br \/>\n    return f&#034;{message}&#xff0c;{name}&#xff01;&#034;<\/p>\n<p>print(greet(&#034;\u5f20\u4e09&#034;))<br \/>\nprint(greet(&#034;\u674e\u56db&#034;, &#034;\u65e9\u4e0a\u597d&#034;)) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>\u4f60\u597d&#xff0c;\u5f20\u4e09&#xff01;<br \/>\n\u65e9\u4e0a\u597d&#xff0c;\u674e\u56db&#xff01; <\/p>\n<p>\u6ce8\u610f&#xff1a;\u5e26\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\u5e94\u8be5\u653e\u5728\u666e\u901a\u53c2\u6570\u540e\u9762\u3002<\/p>\n<p># \u6b63\u786e<br \/>\ndef create_user(name, age&#061;18):<br \/>\n    pass<br \/>\n# \u9519\u8bef<br \/>\ndef create_user(age&#061;18, name):<br \/>\n    pass <\/p>\n<h4 id=\"1.2%20%E5%85%B3%E9%94%AE%E5%AD%97%E5%8F%82%E6%95%B0\">1.2 \u5173\u952e\u5b57\u53c2\u6570<\/h4>\n<p>\u8c03\u7528\u51fd\u6570\u65f6&#xff0c;\u53ef\u4ee5\u663e\u5f0f\u6307\u5b9a\u53c2\u6570\u540d\u79f0\u3002<\/p>\n<p>def create_user(name, age, city):<br \/>\n    return {<br \/>\n        &#034;name&#034;: name,<br \/>\n        &#034;age&#034;: age,<br \/>\n        &#034;city&#034;: city<br \/>\n    }<\/p>\n<p>user &#061; create_user(<br \/>\n    city&#061;&#034;\u897f\u5b89&#034;,<br \/>\n    name&#061;&#034;\u5c0f\u660e&#034;,<br \/>\n    age&#061;20<br \/>\n)<\/p>\n<p>print(user) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>{&#039;name&#039;: &#039;\u5c0f\u660e&#039;, &#039;age&#039;: 20, &#039;city&#039;: &#039;\u897f\u5b89&#039;} <\/p>\n<p><span style=\"color:#be191c\">\u5173\u952e\u5b57\u53c2\u6570\u7684\u597d\u5904\u662f&#xff1a;\u53c2\u6570\u987a\u5e8f\u53ef\u4ee5\u6539\u53d8&#xff0c;\u4ee3\u7801\u53ef\u8bfb\u6027\u4e5f\u66f4\u597d\u3002<\/span><\/p>\n<h4 id=\"1.3%20%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%20*args%20%E4%B8%8E%20**kwargs\">1.3 \u53ef\u53d8\u53c2\u6570 *args \u4e0e **kwargs<\/h4>\n<p>\u5f53\u51fd\u6570\u63a5\u6536\u7684\u53c2\u6570\u4e2a\u6570\u4e0d\u56fa\u5b9a\u65f6&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u53ef\u53d8\u53c2\u6570\u3002<span style=\"color:#be191c\"><span style=\"background-color:#fefcd8\">*numbers<\/span><span style=\"background-color:#fefcd8\"> \u4f1a\u628a\u4f20\u5165\u7684\u591a\u4e2a\u4f4d\u7f6e\u53c2\u6570\u6536\u96c6\u4e3a<\/span><\/span><span style=\"color:#1c7331\"><span style=\"background-color:#fefcd8\">\u5143\u7ec4<\/span><\/span><span style=\"color:#be191c\"><span style=\"background-color:#fefcd8\">\u3002<\/span><\/span><\/p>\n<p>def calculate_sum(*numbers):<br \/>\n    return sum(numbers)<\/p>\n<p>print(calculate_sum(1, 2, 3))<br \/>\nprint(calculate_sum(10, 20, 30, 40)) <\/p>\n<p><span style=\"color:#be191c\"><span style=\"background-color:#fefcd8\">**kwargs<\/span><span style=\"background-color:#fefcd8\"> \u4f1a\u628a\u591a\u4e2a\u5173\u952e\u5b57\u53c2\u6570\u6536\u96c6\u4e3a<\/span><\/span><span style=\"color:#1c7331\"><span style=\"background-color:#fefcd8\">\u5b57\u5178<\/span><\/span><span style=\"color:#be191c\"><span style=\"background-color:#fefcd8\">\u3002<\/span><\/span><\/p>\n<p>def print_user_info(**kwargs):<br \/>\n    for key, value in kwargs.items():<br \/>\n        print(f&#034;{key}: {value}&#034;)<\/p>\n<p>print_user_info(<br \/>\n    name&#061;&#034;\u5c0f\u660e&#034;,<br \/>\n    age&#061;20,<br \/>\n    city&#061;&#034;\u897f\u5b89&#034;<br \/>\n) <\/p>\n<p>\u8fd9\u548c\u524d\u9762\u5b66\u4e60\u7684\u5bb9\u5668\u77e5\u8bc6\u6b63\u597d\u5bf9\u5e94&#xff1a;<\/p>\n<p>*args   -&gt; \u5143\u7ec4 tuple<br \/>\n**kwargs -&gt; \u5b57\u5178 dict <\/p>\n<h4 id=\"1.4%20%E5%B0%8F%E6%A1%88%E4%BE%8B%EF%BC%9A%E8%AE%A2%E5%8D%95%E9%87%91%E9%A2%9D%E8%AE%A1%E7%AE%97\">1.4 \u5c0f\u6848\u4f8b&#xff1a;\u8ba2\u5355\u91d1\u989d\u8ba1\u7b97<\/h4>\n<p>def calculate_order_total(price, count, discount&#061;1.0):<br \/>\n    &#034;&#034;&#034;\u8ba1\u7b97\u8ba2\u5355\u6700\u7ec8\u91d1\u989d\u3002<\/p>\n<p>    price: \u5546\u54c1\u5355\u4ef7<br \/>\n    count: \u5546\u54c1\u6570\u91cf<br \/>\n    discount: \u6298\u6263&#xff0c;\u9ed8\u8ba4\u4e0d\u6253\u6298<br \/>\n    &#034;&#034;&#034;<br \/>\n    original_total &#061; price * count<br \/>\n    final_total &#061; original_total * discount<\/p>\n<p>    return round(final_total, 2)  #\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<\/p>\n<p>total &#061; calculate_order_total(<br \/>\n    price&#061;299.0,<br \/>\n    count&#061;2,<br \/>\n    discount&#061;0.85<br \/>\n)<\/p>\n<p>print(f&#034;\u8ba2\u5355\u6700\u7ec8\u91d1\u989d&#xff1a;{total} \u5143&#034;) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>\u8ba2\u5355\u6700\u7ec8\u91d1\u989d&#xff1a;508.3 \u5143 <\/p>\n<hr \/>\n<h3 id=\"%E4%BA%8C%E3%80%81%E7%B1%BB%E4%B8%8E%E5%AF%B9%E8%B1%A1%EF%BC%9A%E8%AE%A9%E6%95%B0%E6%8D%AE%E5%92%8C%E8%A1%8C%E4%B8%BA%E6%94%BE%E5%9C%A8%E4%B8%80%E8%B5%B7\">\u4e8c\u3001\u7c7b\u4e0e\u5bf9\u8c61&#xff1a;\u8ba9\u6570\u636e\u548c\u884c\u4e3a\u653e\u5728\u4e00\u8d77<\/h3>\n<p>\u51fd\u6570\u9002\u5408\u5904\u7406\u5355\u4e00\u903b\u8f91\u3002<span style=\"color:#be191c\">\u4f46\u5f53\u4e00\u4e2a\u4e8b\u7269\u65e2\u6709\u6570\u636e\u3001\u53c8\u6709\u884c\u4e3a\u65f6&#xff0c;\u4f7f\u7528\u7c7b\u4f1a\u66f4\u5408\u9002<\/span>\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u5b66\u751f\u62e5\u6709&#xff1a;<\/p>\n<p>\u59d3\u540d<br \/>\n\u5e74\u9f84<br \/>\n\u5206\u6570 <\/p>\n<p>\u540c\u65f6\u8fd8\u53ef\u4ee5&#xff1a;<\/p>\n<p>\u81ea\u6211\u4ecb\u7ecd<br \/>\n\u4fee\u6539\u5206\u6570<br \/>\n\u5224\u65ad\u662f\u5426\u53ca\u683c <\/p>\n<p>\u8fd9\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u7c7b\u3002<\/p>\n<p>class Student:<br \/>\n    def __init__(self, name, age, score):<br \/>\n        self.name &#061; name<br \/>\n        self.age &#061; age<br \/>\n        self.score &#061; score<\/p>\n<p>    def introduce(self):<br \/>\n        print(f&#034;\u5927\u5bb6\u597d&#xff0c;\u6211\u53eb {self.name}&#xff0c;\u4eca\u5e74 {self.age} \u5c81\u3002&#034;)<\/p>\n<p>    def is_passed(self):<br \/>\n        return self.score &gt;&#061; 60<\/p>\n<p>    def update_score(self, new_score):<br \/>\n        self.score &#061; new_score <\/p>\n<p>\u521b\u5efa\u5bf9\u8c61&#xff1a;<\/p>\n<p>student &#061; Student(&#034;\u5c0f\u660e&#034;, 20, 88)<\/p>\n<p>student.introduce()<\/p>\n<p>print(student.is_passed())<\/p>\n<p>student.update_score(95)<\/p>\n<p>print(student.score) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>\u5927\u5bb6\u597d&#xff0c;\u6211\u53eb \u5c0f\u660e&#xff0c;\u4eca\u5e74 20 \u5c81\u3002<br \/>\nTrue<br \/>\n95 <\/p>\n<h4 id=\"2.1%20__init__%20%E6%98%AF%E4%BB%80%E4%B9%88\">2.1 __init__ \u662f\u4ec0\u4e48<\/h4>\n<p><span style=\"color:#be191c\">__init__ \u662f\u521d\u59cb\u5316\u65b9\u6cd5\u3002<\/span>\u5f53\u6211\u4eec\u521b\u5efa\u5bf9\u8c61\u65f6&#xff1a;<\/p>\n<p>student &#061; Student(&#034;\u5c0f\u660e&#034;, 20, 88) <\/p>\n<p>Python \u4f1a\u81ea\u52a8\u8c03\u7528&#xff1a;<\/p>\n<p>__init__(self, &#034;\u5c0f\u660e&#034;, 20, 88) <\/p>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<p>self.name &#061; name <\/p>\n<p>\u8868\u793a\u5c06\u4f20\u5165\u7684 name \u4fdd\u5b58\u5230\u5f53\u524d\u5bf9\u8c61\u4e2d\u3002self \u53ef\u4ee5\u7406\u89e3\u4e3a\u201c\u5f53\u524d\u521b\u5efa\u51fa\u6765\u7684\u5bf9\u8c61\u672c\u8eab\u201d\u3002<\/p>\n<h4 id=\"2.2%20%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%85%B3%E7%B3%BB\">2.2 \u7c7b\u548c\u5bf9\u8c61\u7684\u5173\u7cfb<\/h4>\n<p>Student \u7c7b<br \/>\n   \u2193<br \/>\n\u521b\u5efa\u5bf9\u8c61<br \/>\n   \u2193<br \/>\nstudent1\u3001student2\u3001student3 <\/p>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p>student1 &#061; Student(&#034;\u5c0f\u660e&#034;, 20, 88)<br \/>\nstudent2 &#061; Student(&#034;\u5c0f\u7ea2&#034;, 21, 56)<\/p>\n<p>print(student1.name)<br \/>\nprint(student2.name)<\/p>\n<p>print(student1.is_passed())<br \/>\nprint(student2.is_passed()) <\/p>\n<p><span style=\"color:#be191c\">\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u81ea\u5df1\u7684\u5c5e\u6027\u6570\u636e\u3002<\/span><\/p>\n<h4 id=\"2.3%20%E5%B0%8F%E6%A1%88%E4%BE%8B%EF%BC%9A%E7%AE%80%E5%8D%95%E4%BB%BB%E5%8A%A1%E7%B1%BB\">2.3 \u5c0f\u6848\u4f8b&#xff1a;\u7b80\u5355\u4efb\u52a1\u7c7b<\/h4>\n<p>\u540e\u9762\u4f7f\u7528 FastAPI \u7f16\u5199\u540e\u7aef\u65f6&#xff0c;\u4e5f\u4f1a\u9891\u7e41\u5904\u7406\u201c\u4efb\u52a1\u201d\u201c\u7528\u6237\u201d\u201c\u8ba2\u5355\u201d\u7b49\u5bf9\u8c61\u3002<\/p>\n<p>class Task:<br \/>\n    def __init__(self, task_id, title, completed&#061;False):<br \/>\n        self.task_id &#061; task_id<br \/>\n        self.title &#061; title<br \/>\n        self.completed &#061; completed<\/p>\n<p>    def finish(self):<br \/>\n        self.completed &#061; True<\/p>\n<p>    def to_dict(self):<br \/>\n        return {<br \/>\n            &#034;id&#034;: self.task_id,<br \/>\n            &#034;title&#034;: self.title,<br \/>\n            &#034;completed&#034;: self.completed<br \/>\n        }<\/p>\n<p>task &#061; Task(1, &#034;\u5b66\u4e60 FastAPI&#034;)<\/p>\n<p>print(task.to_dict())<\/p>\n<p>task.finish()<\/p>\n<p>print(task.to_dict()) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>{&#039;id&#039;: 1, &#039;title&#039;: &#039;\u5b66\u4e60 FastAPI&#039;, &#039;completed&#039;: False}<br \/>\n{&#039;id&#039;: 1, &#039;title&#039;: &#039;\u5b66\u4e60 FastAPI&#039;, &#039;completed&#039;: True} <\/p>\n<h3 id=\"%E4%B8%89%E3%80%81%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%9A%E4%B8%8D%E4%BF%AE%E6%94%B9%E5%8E%9F%E5%87%BD%E6%95%B0%E4%B9%9F%E8%83%BD%E5%A2%9E%E5%BC%BA%E5%8A%9F%E8%83%BD\" style=\"background-color:transparent\">\u4e09\u3001\u88c5\u9970\u5668&#xff1a;\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4e5f\u80fd\u589e\u5f3a\u529f\u80fd<\/h3>\n<p><span style=\"color:#be191c\">\u88c5\u9970\u5668\u662f Python \u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u3002\u5b83\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u524d\u63d0\u4e0b&#xff0c;\u4e3a\u51fd\u6570\u589e\u52a0\u989d\u5916\u80fd\u529b\u3002<\/span><\/p>\n<p>\u5e38\u89c1\u4f7f\u7528\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\u8bb0\u5f55\u65e5\u5fd7&#xff1b;<\/li>\n<li>\u7edf\u8ba1\u51fd\u6570\u6267\u884c\u65f6\u95f4&#xff1b;<\/li>\n<li>\u767b\u5f55\u6821\u9a8c&#xff1b;<\/li>\n<li>\u6743\u9650\u6821\u9a8c&#xff1b;<\/li>\n<li>\u8bf7\u6c42\u53c2\u6570\u6821\u9a8c&#xff1b;<\/li>\n<li>\u7f13\u5b58\u5904\u7406&#xff1b;<\/li>\n<li>Web \u63a5\u53e3\u8def\u7531\u6ce8\u518c\u3002<\/li>\n<\/ul>\n<h4 id=\"3.1%20%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84%E8%A3%85%E9%A5%B0%E5%99%A8\">3.1 \u6700\u7b80\u5355\u7684\u88c5\u9970\u5668<\/h4>\n<p>def log_decorator(func):<br \/>\n    def wrapper():<br \/>\n        print(&#034;\u51fd\u6570\u5f00\u59cb\u6267\u884c&#034;)<br \/>\n        func()<br \/>\n        print(&#034;\u51fd\u6570\u6267\u884c\u7ed3\u675f&#034;)<\/p>\n<p>    return wrapper<\/p>\n<p>&#064;log_decorator<br \/>\ndef say_hello():<br \/>\n    print(&#034;\u4f60\u597d&#xff0c;Python&#xff01;&#034;)<\/p>\n<p>say_hello() <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>\u51fd\u6570\u5f00\u59cb\u6267\u884c<br \/>\n\u4f60\u597d&#xff0c;Python&#xff01;<br \/>\n\u51fd\u6570\u6267\u884c\u7ed3\u675f <\/p>\n<p>\u8fd9\u4e00\u53e5&#xff1a;<\/p>\n<p>&#064;log_decorator <\/p>\n<p>\u672c\u8d28\u4e0a\u7b49\u4ef7\u4e8e&#xff1a;<\/p>\n<p>say_hello &#061; log_decorator(say_hello) <\/p>\n<h4 id=\"3.2%20%E6%94%AF%E6%8C%81%E4%BB%BB%E6%84%8F%E5%8F%82%E6%95%B0%E7%9A%84%E8%A3%85%E9%A5%B0%E5%99%A8\">3.2 \u652f\u6301\u4efb\u610f\u53c2\u6570\u7684\u88c5\u9970\u5668<\/h4>\n<p>\u5b9e\u9645\u51fd\u6570\u5f80\u5f80\u6709\u53c2\u6570&#xff0c;\u56e0\u6b64\u88c5\u9970\u5668\u901a\u5e38\u5199\u6210\u4e0b\u9762\u8fd9\u6837&#xff1a;<\/p>\n<p>from functools import wraps<\/p>\n<p>def log_decorator(func):<br \/>\n    &#064;wraps(func)<br \/>\n    def wrapper(*args, **kwargs):<br \/>\n        print(f&#034;\u6b63\u5728\u8c03\u7528\u51fd\u6570&#xff1a;{func.__name__}&#034;)<\/p>\n<p>        result &#061; func(*args, **kwargs)<\/p>\n<p>        print(f&#034;\u51fd\u6570\u6267\u884c\u5b8c\u6210&#xff0c;\u7ed3\u679c&#xff1a;{result}&#034;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper <\/p>\n<p>\u4f7f\u7528&#xff1a;<\/p>\n<p>&#064;log_decorator<br \/>\ndef add(a, b):<br \/>\n    return a &#043; b<\/p>\n<p>print(add(3, 5)) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>\u6b63\u5728\u8c03\u7528\u51fd\u6570&#xff1a;add<br \/>\n\u51fd\u6570\u6267\u884c\u5b8c\u6210&#xff0c;\u7ed3\u679c&#xff1a;8<br \/>\n8 <\/p>\n<p><span style=\"color:#be191c\">&#064;wraps(func) \u7684\u4f5c\u7528\u662f\u4fdd\u7559\u539f\u51fd\u6570\u7684\u540d\u79f0\u3001\u6ce8\u91ca\u7b49\u5143\u4fe1\u606f&#xff0c;\u662f\u7f16\u5199\u88c5\u9970\u5668\u65f6\u7684\u63a8\u8350\u5199\u6cd5\u3002<\/span><\/p>\n<h4 id=\"3.3%20FastAPI%20%E4%B8%AD%E7%9A%84%E8%A3%85%E9%A5%B0%E5%99%A8\" style=\"background-color:transparent\">3.3 FastAPI \u4e2d\u7684\u88c5\u9970\u5668<\/h4>\n<p>\u540e\u9762\u4f1a\u770b\u5230&#xff0c;FastAPI \u6700\u5e38\u89c1\u7684\u5199\u6cd5\u5c31\u662f\u88c5\u9970\u5668&#xff1a;<\/p>\n<p>&#064;app.get(&#034;\/tasks&#034;)<br \/>\nasync def get_tasks():<br \/>\n    return [] <\/p>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<p>&#064;app.get(&#034;\/tasks&#034;) <\/p>\n<p>\u5c31\u662f\u4e00\u4e2a\u88c5\u9970\u5668\u3002\u5b83\u7684\u4f5c\u7528\u662f\u544a\u8bc9 FastAPI&#xff1a;<\/p>\n<p>\u5f53\u5ba2\u6237\u7aef\u53d1\u9001 GET \/tasks \u8bf7\u6c42\u65f6&#xff0c;<br \/>\n\u8bf7\u6267\u884c get_tasks \u51fd\u6570\u3002 <\/p>\n<p>\u6240\u4ee5\u7406\u89e3\u88c5\u9970\u5668&#xff0c;\u5bf9\u5b66\u4e60 FastAPI \u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h3 id=\"%E5%9B%9B%E3%80%81%E8%BF%AD%E4%BB%A3%E5%99%A8%E4%B8%8E%E7%94%9F%E6%88%90%E5%99%A8\">\u56db\u3001\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668<\/h3>\n<h4 id=\"4.1%20%E4%BB%80%E4%B9%88%E6%98%AF%E8%BF%AD%E4%BB%A3\">4.1 \u4ec0\u4e48\u662f\u8fed\u4ee3<\/h4>\n<p>\u904d\u5386\u5217\u8868\u65f6&#xff0c;\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u8fc7 for \u5faa\u73af&#xff1a;<\/p>\n<p>numbers &#061; [10, 20, 30]<\/p>\n<p>for number in numbers:<br \/>\n    print(number) <\/p>\n<p>\u8fd9\u91cc\u7684\u5217\u8868\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<p>\u5e38\u89c1\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>list&#xff1b;<\/li>\n<li>tuple&#xff1b;<\/li>\n<li>str&#xff1b;<\/li>\n<li>set&#xff1b;<\/li>\n<li>dict&#xff1b;<\/li>\n<li>\u6587\u4ef6\u5bf9\u8c61&#xff1b;<\/li>\n<li>\u751f\u6210\u5668\u3002<\/li>\n<\/ul>\n<h4 id=\"4.2%20%E4%BB%80%E4%B9%88%E6%98%AF%E8%BF%AD%E4%BB%A3%E5%99%A8\">4.2 \u4ec0\u4e48\u662f\u8fed\u4ee3\u5668<\/h4>\n<p><span style=\"color:#be191c\"><span style=\"background-color:#fefcd8\">\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u53ef\u4ee5\u4e0d\u65ad\u901a\u8fc7 <\/span><span style=\"background-color:#fefcd8\">next()<\/span><span style=\"background-color:#fefcd8\"> \u83b7\u53d6\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u5bf9\u8c61\u3002<\/span><\/span><\/p>\n<p>numbers &#061; [10, 20, 30]<\/p>\n<p>iterator &#061; iter(numbers)<\/p>\n<p>print(next(iterator))<br \/>\nprint(next(iterator))<br \/>\nprint(next(iterator)) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>10<br \/>\n20<br \/>\n30 <\/p>\n<p>\u5f53\u6ca1\u6709\u66f4\u591a\u5143\u7d20\u65f6&#xff0c;\u518d\u8c03\u7528 next() \u4f1a\u629b\u51fa\u5f02\u5e38&#xff1a;<\/p>\n<p>StopIteration <\/p>\n<p>for \u5faa\u73af\u672c\u8d28\u4e0a\u4e5f\u662f\u4e0d\u65ad\u8c03\u7528 next()&#xff0c;\u53ea\u662f Python \u81ea\u52a8\u5e2e\u6211\u4eec\u5904\u7406\u4e86\u505c\u6b62\u5f02\u5e38\u3002<\/p>\n<h4 id=\"4.3%20%E8%87%AA%E5%AE%9A%E4%B9%89%E8%BF%AD%E4%BB%A3%E5%99%A8\">4.3 \u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668<\/h4>\n<p>class CountDown:<br \/>\n    def __init__(self, start):<br \/>\n        self.current &#061; start<\/p>\n<p>    def __iter__(self):<br \/>\n        return self<\/p>\n<p>    def __next__(self):<br \/>\n        if self.current &lt;&#061; 0:<br \/>\n            raise StopIteration<\/p>\n<p>        value &#061; self.current<br \/>\n        self.current -&#061; 1<\/p>\n<p>        return value <\/p>\n<p>\u4f7f\u7528&#xff1a;<\/p>\n<p>for number in CountDown(3):<br \/>\n    print(number) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>3<br \/>\n2<br \/>\n1 <\/p>\n<h4 id=\"4.4%20%E4%BB%80%E4%B9%88%E6%98%AF%E7%94%9F%E6%88%90%E5%99%A8\" style=\"background-color:transparent\">4.4 \u4ec0\u4e48\u662f\u751f\u6210\u5668<\/h4>\n<p><span style=\"color:#be191c\"><span style=\"background-color:#fefcd8\">\u751f\u6210\u5668\u662f\u66f4\u7b80\u5355\u7684\u8fed\u4ee3\u5668\u5199\u6cd5\u3002\u5b83\u4f7f\u7528 <\/span><span style=\"background-color:#fefcd8\">yield<\/span><span style=\"background-color:#fefcd8\"> \u8fd4\u56de\u6570\u636e\u3002<\/span><\/span><\/p>\n<p>def count_down(start):<br \/>\n    while start &gt; 0:<br \/>\n        yield start<br \/>\n        start -&#061; 1 <\/p>\n<p>\u8c03\u7528&#xff1a;<\/p>\n<p>generator &#061; count_down(3)<\/p>\n<p>print(next(generator))<br \/>\nprint(next(generator))<br \/>\nprint(next(generator)) <\/p>\n<p>\u6216\u8005\u76f4\u63a5\u4f7f\u7528 for&#xff1a;<\/p>\n<p>for number in count_down(3):<br \/>\n    print(number) <\/p>\n<p><span style=\"color:#be191c\">\u751f\u6210\u5668\u6700\u5927\u7684\u4f18\u52bf\u662f\u8282\u7701\u5185\u5b58\u3002<\/span>\u4f8b\u5982&#xff0c;\u5982\u679c\u9700\u8981\u5904\u7406\u4e00\u5343\u4e07\u6761\u6570\u636e&#xff1a;<\/p>\n<p>numbers &#061; [i for i in range(10_000_000)] <\/p>\n<p>\u4f1a\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u6570\u636e\u5e76\u5360\u7528\u5185\u5b58\u3002\u800c\u751f\u6210\u5668&#xff1a;<\/p>\n<p>numbers &#061; (i for i in range(10_000_000)) <\/p>\n<p><span style=\"color:#be191c\">\u53ea\u6709\u771f\u6b63\u904d\u5386\u5230\u67d0\u4e2a\u5143\u7d20\u65f6&#xff0c;\u624d\u751f\u6210\u90a3\u4e2a\u5143\u7d20\u3002<\/span><\/p>\n<h4 id=\"4.5%20%E5%B0%8F%E6%A1%88%E4%BE%8B%EF%BC%9A%E5%88%86%E6%89%B9%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE\" style=\"background-color:transparent\">4.5 \u5c0f\u6848\u4f8b&#xff1a;\u5206\u6279\u8bfb\u53d6\u6570\u636e<\/h4>\n<p>def batch_data(data, batch_size):<br \/>\n    for index in range(0, len(data), batch_size):<br \/>\n        yield data[index:index &#043; batch_size]<\/p>\n<p>users &#061; [<br \/>\n    &#034;\u7528\u62371&#034;, &#034;\u7528\u62372&#034;, &#034;\u7528\u62373&#034;,<br \/>\n    &#034;\u7528\u62374&#034;, &#034;\u7528\u62375&#034;, &#034;\u7528\u62376&#034;,<br \/>\n    &#034;\u7528\u62377&#034;<br \/>\n]<\/p>\n<p>for batch in batch_data(users, 3):<br \/>\n    print(batch) <\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>[&#039;\u7528\u62371&#039;, &#039;\u7528\u62372&#039;, &#039;\u7528\u62373&#039;]<br \/>\n[&#039;\u7528\u62374&#039;, &#039;\u7528\u62375&#039;, &#039;\u7528\u62376&#039;]<br \/>\n[&#039;\u7528\u62377&#039;] <\/p>\n<p>\u8fd9\u79cd\u201c\u5206\u6279\u5904\u7406\u201d\u7684\u601d\u8def&#xff0c;\u5728\u722c\u866b\u3001\u6570\u636e\u5e93\u6279\u91cf\u5199\u5165\u3001\u6587\u4ef6\u5904\u7406\u3001\u6d88\u606f\u961f\u5217\u6d88\u8d39\u7b49\u573a\u666f\u4e2d\u5f88\u5e38\u89c1\u3002<\/p>\n<h3 id=\"%E4%BA%94%E3%80%81asyncio%EF%BC%9APython%20%E7%9A%84%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80\" style=\"background-color:transparent\">\u4e94\u3001asyncio&#xff1a;Python \u7684\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840<\/h3>\n<p><span style=\"color:#be191c\">\u5b66\u4e60 FastAPI \u524d&#xff0c;\u5fc5\u987b\u5148\u7406\u89e3 asyncio \u7684\u57fa\u7840\u3002<\/span><\/p>\n<h4 id=\"5.1%20%E5%90%8C%E6%AD%A5%E6%89%A7%E8%A1%8C%E7%9A%84%E9%97%AE%E9%A2%98\">5.1 \u540c\u6b65\u6267\u884c\u7684\u95ee\u9898<\/h4>\n<p>\u5047\u8bbe\u6709\u4e24\u4e2a\u4efb\u52a1&#xff1a;<\/p>\n<p>import time<\/p>\n<p>def download_file():<br \/>\n    print(&#034;\u5f00\u59cb\u4e0b\u8f7d\u6587\u4ef6&#034;)<br \/>\n    time.sleep(3)<br \/>\n    print(&#034;\u6587\u4ef6\u4e0b\u8f7d\u5b8c\u6210&#034;)<\/p>\n<p>def send_message():<br \/>\n    print(&#034;\u5f00\u59cb\u53d1\u9001\u6d88\u606f&#034;)<br \/>\n    time.sleep(2)<br \/>\n    print(&#034;\u6d88\u606f\u53d1\u9001\u5b8c\u6210&#034;)<\/p>\n<p>download_file()<br \/>\nsend_message() <\/p>\n<p>\u603b\u8017\u65f6\u7ea6\u4e3a&#xff1a;<\/p>\n<p>3 \u79d2 &#043; 2 \u79d2 &#061; 5 \u79d2 <\/p>\n<p>\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u4efb\u52a1\u6ca1\u6709\u7ed3\u675f&#xff0c;\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u4e0d\u80fd\u5f00\u59cb\u3002<\/p>\n<h4 id=\"5.2%20%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C\">5.2 \u5f02\u6b65\u6267\u884c<\/h4>\n<p>import asyncio<\/p>\n<p>async def download_file():<br \/>\n    print(&#034;\u5f00\u59cb\u4e0b\u8f7d\u6587\u4ef6&#034;)<br \/>\n    await asyncio.sleep(3)<br \/>\n    print(&#034;\u6587\u4ef6\u4e0b\u8f7d\u5b8c\u6210&#034;)<\/p>\n<p>async def send_message():<br \/>\n    print(&#034;\u5f00\u59cb\u53d1\u9001\u6d88\u606f&#034;)<br \/>\n    await asyncio.sleep(2)<br \/>\n    print(&#034;\u6d88\u606f\u53d1\u9001\u5b8c\u6210&#034;)<\/p>\n<p>async def main():<br \/>\n    await asyncio.gather(<br \/>\n        download_file(),<br \/>\n        send_message()<br \/>\n    )<\/p>\n<p>asyncio.run(main()) <\/p>\n<p>\u603b\u8017\u65f6\u7ea6\u4e3a&#xff1a;<\/p>\n<p>3 \u79d2 <\/p>\n<p>\u56e0\u4e3a\u4e24\u4e2a\u4efb\u52a1\u5728\u7b49\u5f85 I\/O \u65f6\u53ef\u4ee5\u4ea4\u66ff\u6267\u884c\u3002<\/p>\n<h4 id=\"5.3%20async%20%E4%B8%8E%20await\">5.3 async \u4e0e await<\/h4>\n<p>async def task():<br \/>\n    await asyncio.sleep(1) <\/p>\n<p>\u542b\u4e49&#xff1a;<\/p>\n<ul>\n<li>async def&#xff1a;\u5b9a\u4e49\u534f\u7a0b\u51fd\u6570&#xff1b;<\/li>\n<li>await&#xff1a;\u7b49\u5f85\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210&#xff0c;\u540c\u65f6\u8ba9\u51fa\u6267\u884c\u6743&#xff1b;<\/li>\n<li>asyncio.run()&#xff1a;\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u5e76\u6267\u884c\u534f\u7a0b&#xff1b;<\/li>\n<li>asyncio.gather()&#xff1a;\u5e76\u53d1\u7b49\u5f85\u591a\u4e2a\u534f\u7a0b\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<h4 id=\"5.4%20%E4%B8%80%E4%B8%AA%E9%87%8D%E8%A6%81%E8%AF%AF%E5%8C%BA\">5.4 \u4e00\u4e2a\u91cd\u8981\u8bef\u533a<\/h4>\n<p>\u5f02\u6b65\u4e0d\u662f\u201c\u8ba9\u6240\u6709\u4ee3\u7801\u90fd\u53d8\u5feb\u201d\u3002\u5982\u679c\u51fd\u6570\u662f CPU \u5bc6\u96c6\u578b\u4efb\u52a1&#xff0c;\u4f8b\u5982&#xff1a;<\/p>\n<p>def calculate():<br \/>\n    total &#061; 0<\/p>\n<p>    for i in range(100_000_000):<br \/>\n        total &#043;&#061; i<\/p>\n<p>    return total <\/p>\n<p>\u5373\u4f7f\u5199\u6210 async def&#xff0c;\u4e5f\u4e0d\u4f1a\u81ea\u52a8\u52a0\u901f\u3002<\/p>\n<p>\u5f02\u6b65\u66f4\u9002\u5408 I\/O \u5bc6\u96c6\u578b\u573a\u666f&#xff0c;\u4f8b\u5982&#xff1a;<\/p>\n<ul>\n<li>\u8bf7\u6c42\u7b2c\u4e09\u65b9\u63a5\u53e3&#xff1b;<\/li>\n<li>\u67e5\u8be2\u6570\u636e\u5e93&#xff1b;<\/li>\n<li>\u8bfb\u5199\u6587\u4ef6&#xff1b;<\/li>\n<li>\u7b49\u5f85 Redis&#xff1b;<\/li>\n<li>\u7b49\u5f85\u6d88\u606f\u961f\u5217&#xff1b;<\/li>\n<li>\u7b49\u5f85\u7f51\u7edc\u54cd\u5e94\u3002<\/li>\n<\/ul>\n<p>\u5728\u5f02\u6b65\u51fd\u6570\u4e2d&#xff0c;\u4e0d\u8981\u4f7f\u7528\u963b\u585e\u5f0f\u7684&#xff1a;<\/p>\n<p>time.sleep(3) <\/p>\n<p>\u800c\u5e94\u8be5\u4f7f\u7528&#xff1a;<\/p>\n<p>await asyncio.sleep(3) <\/p>\n<h3 id=\"%E5%85%AD%E3%80%81FastAPI%EF%BC%9A%E4%BB%8E%E9%9B%B6%E5%86%99%E4%B8%80%E4%B8%AA%E4%BB%BB%E5%8A%A1%E7%AE%A1%E7%90%86%E5%90%8E%E7%AB%AF\" style=\"background-color:transparent\">\u516d\u3001FastAPI&#xff1a;\u4ece\u96f6\u5199\u4e00\u4e2a\u4efb\u52a1\u7ba1\u7406\u540e\u7aef<\/h3>\n<p><span style=\"color:#be191c\">FastAPI \u662f\u4e00\u4e2a\u73b0\u4ee3 Python Web \u6846\u67b6&#xff0c;\u9002\u5408\u5f00\u53d1<\/span>&#xff1a;<\/p>\n<ul>\n<li>RESTful API&#xff1b;<\/li>\n<li>\u524d\u540e\u7aef\u5206\u79bb\u9879\u76ee&#xff1b;<\/li>\n<li>\u7ba1\u7406\u540e\u53f0\u63a5\u53e3&#xff1b;<\/li>\n<li>\u5fae\u670d\u52a1&#xff1b;<\/li>\n<li>AI \u5e94\u7528\u540e\u7aef&#xff1b;<\/li>\n<li>\u6587\u4ef6\u4e0a\u4f20\u670d\u52a1&#xff1b;<\/li>\n<li>\u6570\u636e\u5206\u6790\u63a5\u53e3&#xff1b;<\/li>\n<li>\u6a21\u578b\u63a8\u7406\u63a5\u53e3\u3002<\/li>\n<\/ul>\n<p>FastAPI \u7684\u51e0\u4e2a\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u57fa\u4e8e Python \u7c7b\u578b\u6ce8\u89e3&#xff1b;<\/li>\n<li>\u81ea\u52a8\u6821\u9a8c\u8bf7\u6c42\u53c2\u6570&#xff1b;<\/li>\n<li>\u81ea\u52a8\u751f\u6210 Swagger \u6587\u6863&#xff1b;<\/li>\n<li>\u539f\u751f\u652f\u6301\u5f02\u6b65&#xff1b;<\/li>\n<li>\u5f00\u53d1\u4f53\u9a8c\u53cb\u597d&#xff1b;<\/li>\n<li>\u9002\u5408\u6784\u5efa\u9ad8\u6027\u80fd API\u3002<\/li>\n<\/ul>\n<h3 id=\"6.1%20%E5%AE%89%E8%A3%85%20FastAPI\">6.1 \u5b89\u88c5 FastAPI<\/h3>\n<p>\u5efa\u8bae\u5148\u521b\u5efa\u865a\u62df\u73af\u5883&#xff1a;<\/p>\n<p>python -m venv .venv <\/p>\n<p>Windows \u6fc0\u6d3b\u865a\u62df\u73af\u5883&#xff1a;<\/p>\n<p>.venv\\\\Scripts\\\\activate <\/p>\n<p>\u5b89\u88c5 FastAPI&#xff1a;<\/p>\n<p>python -m pip install &#034;fastapi[standard]&#034; <\/p>\n<p>\u9879\u76ee\u76ee\u5f55\u53ef\u4ee5\u5148\u4fdd\u6301\u7b80\u5355&#xff1a;<\/p>\n<p>python_fastapi_demo\/<br \/>\n\u251c\u2500\u2500 main.py<br \/>\n\u2514\u2500\u2500 requirements.txt <\/p>\n<p>requirements.txt \u5185\u5bb9&#xff1a;<\/p>\n<p>fastapi[standard] <\/p>\n<h3 id=\"6.2%20%E7%AC%AC%E4%B8%80%E4%B8%AA%20FastAPI%20%E6%8E%A5%E5%8F%A3\">6.2 \u7b2c\u4e00\u4e2a FastAPI \u63a5\u53e3<\/h3>\n<p>\u521b\u5efa main.py&#xff1a;<\/p>\n<p>from fastapi import FastAPI<\/p>\n<p>app &#061; FastAPI(<br \/>\n    title&#061;&#034;\u4efb\u52a1\u7ba1\u7406 API&#034;,<br \/>\n    description&#061;&#034;\u7528\u4e8e\u5b66\u4e60 FastAPI \u7684\u7b80\u5355\u540e\u7aef\u9879\u76ee&#034;,<br \/>\n    version&#061;&#034;1.0.0&#034;<br \/>\n)<\/p>\n<p>&#064;app.get(&#034;\/&#034;)<br \/>\nasync def read_root():<br \/>\n    return {<br \/>\n        &#034;message&#034;: &#034;FastAPI \u670d\u52a1\u542f\u52a8\u6210\u529f&#034;<br \/>\n    } <\/p>\n<p>\u542f\u52a8\u670d\u52a1&#xff1a;<\/p>\n<p>fastapi dev main.py <\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4f20\u7edf\u7684 Uvicorn \u547d\u4ee4&#xff1a;<\/p>\n<p>uvicorn main:app &#8211;reload <\/p>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<p>main     -&gt; main.py \u6587\u4ef6<br \/>\napp      -&gt; FastAPI() \u521b\u5efa\u7684\u5bf9\u8c61<br \/>\n&#8211;reload -&gt; \u4fee\u6539\u4ee3\u7801\u540e\u81ea\u52a8\u91cd\u542f\u670d\u52a1 <\/p>\n<p>\u542f\u52a8\u540e\u8bbf\u95ee&#xff1a;<\/p>\n<p>http:\/\/127.0.0.1:8000\/ <\/p>\n<p>\u8fd4\u56de&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;message&#034;: &#034;FastAPI \u670d\u52a1\u542f\u52a8\u6210\u529f&#034;<br \/>\n} <\/p>\n<p>FastAPI \u8fd8\u4f1a\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863&#xff1a;<\/p>\n<p>http:\/\/127.0.0.1:8000\/docs <\/p>\n<p>\u8fd9\u662f Swagger UI \u9875\u9762&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u4e2d\u6d4b\u8bd5\u63a5\u53e3\u3002FastAPI \u7684\u5b98\u65b9\u6587\u6863\u4e5f\u63d0\u4f9b\u4e86\u4f7f\u7528 Python \u57fa\u7840\u955c\u50cf\u6784\u5efa\u5bb9\u5668\u5316\u670d\u52a1\u7684\u6807\u51c6\u76ee\u5f55\u4e0e\u542f\u52a8\u65b9\u5f0f&#xff0c;\u540e\u7eed\u53ef\u4ee5\u628a\u672c\u7bc7\u7684\u5c0f\u9879\u76ee\u76f4\u63a5\u5bb9\u5668\u5316\u90e8\u7f72\u3002FastAPI \u5b98\u65b9\u90e8\u7f72\u6587\u6863<\/p>\n<h3 id=\"6.3%20%E8%B7%AF%E5%BE%84%E5%8F%82%E6%95%B0\">6.3 \u8def\u5f84\u53c2\u6570<\/h3>\n<p><span style=\"color:#be191c\">\u8def\u5f84\u53c2\u6570\u662f URL \u8def\u5f84\u4e2d\u7684\u52a8\u6001\u90e8\u5206\u3002<\/span><\/p>\n<p>from fastapi import FastAPI<\/p>\n<p>app &#061; FastAPI()<\/p>\n<p>&#064;app.get(&#034;\/tasks\/{task_id}&#034;)<br \/>\nasync def get_task(task_id: int):<br \/>\n    return {<br \/>\n        &#034;task_id&#034;: task_id,<br \/>\n        &#034;message&#034;: &#034;\u67e5\u8be2\u4efb\u52a1\u6210\u529f&#034;<br \/>\n    } <\/p>\n<p>\u8bbf\u95ee&#xff1a;<\/p>\n<p>http:\/\/127.0.0.1:8000\/tasks\/1 <\/p>\n<p>\u8fd4\u56de&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;task_id&#034;: 1,<br \/>\n  &#034;message&#034;: &#034;\u67e5\u8be2\u4efb\u52a1\u6210\u529f&#034;<br \/>\n} <\/p>\n<p>\u8fd9\u91cc&#xff1a;<\/p>\n<p>task_id: int <\/p>\n<p><span style=\"color:#be191c\"><span style=\"background-color:#fefcd8\">\u4e0d\u4ec5\u662f\u7c7b\u578b\u63d0\u793a&#xff0c;\u4e5f\u662f\u8bf7\u6c42\u53c2\u6570\u6821\u9a8c\u3002<\/span><\/span>\u5982\u679c\u8bbf\u95ee&#xff1a;<\/p>\n<p>\/tasks\/abc <\/p>\n<p>FastAPI \u4f1a\u81ea\u52a8\u8fd4\u56de\u53c2\u6570\u6821\u9a8c\u9519\u8bef&#xff0c;\u56e0\u4e3a abc \u4e0d\u80fd\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<h3 id=\"6.4%20%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0\">6.4 \u67e5\u8be2\u53c2\u6570<\/h3>\n<p>\u67e5\u8be2\u53c2\u6570\u901a\u5e38\u653e\u5728 URL \u7684 ? \u540e\u9762\u3002<\/p>\n<p>&#064;app.get(&#034;\/tasks&#034;)<br \/>\nasync def get_tasks(<br \/>\n    keyword: str | None &#061; None,<br \/>\n    completed: bool | None &#061; None<br \/>\n):<br \/>\n    return {<br \/>\n        &#034;keyword&#034;: keyword,<br \/>\n        &#034;completed&#034;: completed<br \/>\n    } <\/p>\n<p>\u8bbf\u95ee&#xff1a;<\/p>\n<p>http:\/\/127.0.0.1:8000\/tasks?keyword&#061;FastAPI&#038;completed&#061;false <\/p>\n<p>\u8fd4\u56de&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;keyword&#034;: &#034;FastAPI&#034;,<br \/>\n  &#034;completed&#034;: false<br \/>\n} <\/p>\n<p>\u8def\u5f84\u53c2\u6570\u548c\u67e5\u8be2\u53c2\u6570\u7684\u533a\u522b&#xff1a;<\/p>\n<table>\n<tr>\u7c7b\u578b\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>\u8def\u5f84\u53c2\u6570<\/td>\n<td>\/tasks\/1<\/td>\n<\/tr>\n<tr>\n<td>\u67e5\u8be2\u53c2\u6570<\/td>\n<td>\/tasks?completed&#061;true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3 id=\"%E4%B8%83%E3%80%81FastAPI%20%E5%B0%8F%E9%A1%B9%E7%9B%AE%EF%BC%9A%E4%BB%BB%E5%8A%A1%E7%AE%A1%E7%90%86%20API\" style=\"background-color:transparent\">\u4e03\u3001FastAPI \u5c0f\u9879\u76ee&#xff1a;\u4efb\u52a1\u7ba1\u7406 API<\/h3>\n<p>\u4e0b\u9762\u4f7f\u7528\u5185\u5b58\u5217\u8868\u6a21\u62df\u6570\u636e\u5e93&#xff0c;\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u4efb\u52a1\u7ba1\u7406\u7cfb\u7edf\u3002<\/p>\n<p>\u652f\u6301\u529f\u80fd&#xff1a;<\/p>\n<ul>\n<li>\u67e5\u8be2\u5168\u90e8\u4efb\u52a1&#xff1b;<\/li>\n<li>\u6839\u636e ID \u67e5\u8be2\u4efb\u52a1&#xff1b;<\/li>\n<li>\u65b0\u589e\u4efb\u52a1&#xff1b;<\/li>\n<li>\u4fee\u6539\u4efb\u52a1\u72b6\u6001&#xff1b;<\/li>\n<li>\u5220\u9664\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p>\u5b8c\u6574 main.py&#xff1a;<\/p>\n<p>from fastapi import FastAPI, HTTPException, status<br \/>\nfrom pydantic import BaseModel, Field<\/p>\n<p>app &#061; FastAPI(<br \/>\n    title&#061;&#034;\u4efb\u52a1\u7ba1\u7406 API&#034;,<br \/>\n    description&#061;&#034;FastAPI \u5165\u95e8\u5b9e\u6218\u9879\u76ee&#034;,<br \/>\n    version&#061;&#034;1.0.0&#034;<br \/>\n)<\/p>\n<p>tasks &#061; [<br \/>\n    {<br \/>\n        &#034;id&#034;: 1,<br \/>\n        &#034;title&#034;: &#034;\u5b66\u4e60 Python \u51fd\u6570&#034;,<br \/>\n        &#034;completed&#034;: True<br \/>\n    },<br \/>\n    {<br \/>\n        &#034;id&#034;: 2,<br \/>\n        &#034;title&#034;: &#034;\u5b66\u4e60 FastAPI&#034;,<br \/>\n        &#034;completed&#034;: False<br \/>\n    }<br \/>\n]<\/p>\n<p>class TaskCreate(BaseModel):<br \/>\n    title: str &#061; Field(<br \/>\n        min_length&#061;1,<br \/>\n        max_length&#061;100,<br \/>\n        description&#061;&#034;\u4efb\u52a1\u6807\u9898&#034;<br \/>\n    )<\/p>\n<p>class TaskUpdate(BaseModel):<br \/>\n    completed: bool &#061; Field(<br \/>\n        description&#061;&#034;\u4efb\u52a1\u662f\u5426\u5b8c\u6210&#034;<br \/>\n    )<\/p>\n<p>&#064;app.get(&#034;\/&#034;)<br \/>\nasync def read_root():<br \/>\n    return {<br \/>\n        &#034;message&#034;: &#034;\u4efb\u52a1\u7ba1\u7406 API \u670d\u52a1\u8fd0\u884c\u4e2d&#034;<br \/>\n    }<\/p>\n<p>&#064;app.get(&#034;\/tasks&#034;)<br \/>\nasync def get_tasks(completed: bool | None &#061; None):<br \/>\n    &#034;&#034;&#034;\u67e5\u8be2\u4efb\u52a1\u5217\u8868\u3002&#034;&#034;&#034;<\/p>\n<p>    if completed is None:<br \/>\n        return tasks<\/p>\n<p>    return [<br \/>\n        task for task in tasks<br \/>\n        if task[&#034;completed&#034;] &#061;&#061; completed<br \/>\n    ]<\/p>\n<p>&#064;app.get(&#034;\/tasks\/{task_id}&#034;)<br \/>\nasync def get_task(task_id: int):<br \/>\n    &#034;&#034;&#034;\u6839\u636e\u4efb\u52a1 ID \u67e5\u8be2\u4efb\u52a1\u3002&#034;&#034;&#034;<\/p>\n<p>    for task in tasks:<br \/>\n        if task[&#034;id&#034;] &#061;&#061; task_id:<br \/>\n            return task<\/p>\n<p>    raise HTTPException(<br \/>\n        status_code&#061;status.HTTP_404_NOT_FOUND,<br \/>\n        detail&#061;&#034;\u4efb\u52a1\u4e0d\u5b58\u5728&#034;<br \/>\n    )<\/p>\n<p>&#064;app.post(<br \/>\n    &#034;\/tasks&#034;,<br \/>\n    status_code&#061;status.HTTP_201_CREATED<br \/>\n)<br \/>\nasync def create_task(task: TaskCreate):<br \/>\n    &#034;&#034;&#034;\u65b0\u589e\u4efb\u52a1\u3002&#034;&#034;&#034;<\/p>\n<p>    new_task &#061; {<br \/>\n        &#034;id&#034;: len(tasks) &#043; 1,<br \/>\n        &#034;title&#034;: task.title,<br \/>\n        &#034;completed&#034;: False<br \/>\n    }<\/p>\n<p>    tasks.append(new_task)<\/p>\n<p>    return new_task<\/p>\n<p>&#064;app.patch(&#034;\/tasks\/{task_id}&#034;)<br \/>\nasync def update_task(<br \/>\n    task_id: int,<br \/>\n    task_data: TaskUpdate<br \/>\n):<br \/>\n    &#034;&#034;&#034;\u4fee\u6539\u4efb\u52a1\u5b8c\u6210\u72b6\u6001\u3002&#034;&#034;&#034;<\/p>\n<p>    for task in tasks:<br \/>\n        if task[&#034;id&#034;] &#061;&#061; task_id:<br \/>\n            task[&#034;completed&#034;] &#061; task_data.completed<br \/>\n            return task<\/p>\n<p>    raise HTTPException(<br \/>\n        status_code&#061;status.HTTP_404_NOT_FOUND,<br \/>\n        detail&#061;&#034;\u4efb\u52a1\u4e0d\u5b58\u5728&#034;<br \/>\n    )<\/p>\n<p>&#064;app.delete(&#034;\/tasks\/{task_id}&#034;)<br \/>\nasync def delete_task(task_id: int):<br \/>\n    &#034;&#034;&#034;\u5220\u9664\u4efb\u52a1\u3002&#034;&#034;&#034;<\/p>\n<p>    for index, task in enumerate(tasks):<br \/>\n        if task[&#034;id&#034;] &#061;&#061; task_id:<br \/>\n            deleted_task &#061; tasks.pop(index)<\/p>\n<p>            return {<br \/>\n                &#034;message&#034;: &#034;\u4efb\u52a1\u5220\u9664\u6210\u529f&#034;,<br \/>\n                &#034;task&#034;: deleted_task<br \/>\n            }<\/p>\n<p>    raise HTTPException(<br \/>\n        status_code&#061;status.HTTP_404_NOT_FOUND,<br \/>\n        detail&#061;&#034;\u4efb\u52a1\u4e0d\u5b58\u5728&#034;<br \/>\n    ) <\/p>\n<h4 id=\"7.1%20Pydantic%20%E6%A8%A1%E5%9E%8B%E7%9A%84%E4%BD%9C%E7%94%A8\" style=\"background-color:transparent\">7.1 Pydantic \u6a21\u578b\u7684\u4f5c\u7528<\/h4>\n<p>class TaskCreate(BaseModel):<br \/>\n    title: str &#061; Field(min_length&#061;1, max_length&#061;100) <\/p>\n<p>\u8fd9\u4e2a\u7c7b\u7528\u4e8e\u63a5\u6536\u65b0\u589e\u4efb\u52a1\u65f6\u7684 JSON \u6570\u636e\u3002\u5f53\u5ba2\u6237\u7aef\u53d1\u9001&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;title&#034;: &#034;\u5b66\u4e60 asyncio&#034;<br \/>\n} <\/p>\n<p>FastAPI \u4f1a\u81ea\u52a8&#xff1a;<\/p>\n<li>\u8bfb\u53d6\u8bf7\u6c42 JSON&#xff1b;<\/li>\n<li>\u68c0\u67e5\u662f\u5426\u5b58\u5728 title&#xff1b;<\/li>\n<li>\u68c0\u67e5 title \u662f\u5426\u4e3a\u5b57\u7b26\u4e32&#xff1b;<\/li>\n<li>\u68c0\u67e5\u957f\u5ea6\u662f\u5426\u7b26\u5408\u8981\u6c42&#xff1b;<\/li>\n<li>\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a TaskCreate \u5bf9\u8c61\u3002<\/li>\n<p>\u5982\u679c\u5ba2\u6237\u7aef\u53d1\u9001&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;title&#034;: &#034;&#034;<br \/>\n} <\/p>\n<p>FastAPI \u4f1a\u81ea\u52a8\u8fd4\u56de\u53c2\u6570\u6821\u9a8c\u9519\u8bef&#xff0c;\u4e0d\u9700\u8981\u624b\u52a8\u5199\u5927\u91cf if \u5224\u65ad\u3002<\/p>\n<h4 id=\"7.2%20%E6%B5%8B%E8%AF%95%E6%96%B0%E5%A2%9E%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3\">7.2 \u6d4b\u8bd5\u65b0\u589e\u4efb\u52a1\u63a5\u53e3<\/h4>\n<p>\u8bf7\u6c42\u5730\u5740&#xff1a;<\/p>\n<p>POST http:\/\/127.0.0.1:8000\/tasks <\/p>\n<p>\u8bf7\u6c42\u4f53&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;title&#034;: &#034;\u5b66\u4e60 FastAPI \u8bf7\u6c42\u4f53&#034;<br \/>\n} <\/p>\n<p>\u8fd4\u56de&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;id&#034;: 3,<br \/>\n  &#034;title&#034;: &#034;\u5b66\u4e60 FastAPI \u8bf7\u6c42\u4f53&#034;,<br \/>\n  &#034;completed&#034;: false<br \/>\n} <\/p>\n<h4 id=\"7.3%20%E6%B5%8B%E8%AF%95%E4%BF%AE%E6%94%B9%E4%BB%BB%E5%8A%A1%E7%8A%B6%E6%80%81\">7.3 \u6d4b\u8bd5\u4fee\u6539\u4efb\u52a1\u72b6\u6001<\/h4>\n<p>\u8bf7\u6c42\u5730\u5740&#xff1a;<\/p>\n<p>PATCH http:\/\/127.0.0.1:8000\/tasks\/2 <\/p>\n<p>\u8bf7\u6c42\u4f53&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;completed&#034;: true<br \/>\n} <\/p>\n<p>\u8fd4\u56de&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;id&#034;: 2,<br \/>\n  &#034;title&#034;: &#034;\u5b66\u4e60 FastAPI&#034;,<br \/>\n  &#034;completed&#034;: true<br \/>\n} <\/p>\n<h4 id=\"7.4%20HTTPException%20%E7%9A%84%E4%BD%9C%E7%94%A8\" style=\"background-color:transparent\">7.4 HTTPException \u7684\u4f5c\u7528<\/h4>\n<p>\u5f53\u4efb\u52a1\u4e0d\u5b58\u5728\u65f6&#xff1a;<\/p>\n<p>raise HTTPException(<br \/>\n    status_code&#061;404,<br \/>\n    detail&#061;&#034;\u4efb\u52a1\u4e0d\u5b58\u5728&#034;<br \/>\n) <\/p>\n<p>\u5ba2\u6237\u7aef\u4f1a\u6536\u5230&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;detail&#034;: &#034;\u4efb\u52a1\u4e0d\u5b58\u5728&#034;<br \/>\n} <\/p>\n<p>\u5e38\u89c1\u72b6\u6001\u7801&#xff1a;<\/p>\n<table>\n<tr>\u72b6\u6001\u7801\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>200<\/td>\n<td>\u8bf7\u6c42\u6210\u529f<\/td>\n<\/tr>\n<tr>\n<td>201<\/td>\n<td>\u521b\u5efa\u6210\u529f<\/td>\n<\/tr>\n<tr>\n<td>400<\/td>\n<td>\u8bf7\u6c42\u53c2\u6570\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>401<\/td>\n<td>\u672a\u767b\u5f55\u6216\u8ba4\u8bc1\u5931\u8d25<\/td>\n<\/tr>\n<tr>\n<td>403<\/td>\n<td>\u6ca1\u6709\u6743\u9650<\/td>\n<\/tr>\n<tr>\n<td>404<\/td>\n<td>\u8d44\u6e90\u4e0d\u5b58\u5728<\/td>\n<\/tr>\n<tr>\n<td>500<\/td>\n<td>\u670d\u52a1\u7aef\u5f02\u5e38<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3 id=\"%E5%85%AB%E3%80%81FastAPI%20%E4%B8%8E%20asyncio%20%E7%9A%84%E5%85%B3%E7%B3%BB\" style=\"background-color:transparent\">\u516b\u3001FastAPI \u4e0e asyncio \u7684\u5173\u7cfb<\/h3>\n<p>\u4f60\u4f1a\u53d1\u73b0&#xff0c;FastAPI \u63a5\u53e3\u7ecf\u5e38\u5199\u6210&#xff1a;<\/p>\n<p>&#064;app.get(&#034;\/tasks&#034;)<br \/>\nasync def get_tasks():<br \/>\n    return tasks <\/p>\n<p>\u8fd9\u91cc\u7684 async def \u8868\u793a\u8be5\u63a5\u53e3\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u65b9\u5f0f\u6267\u884c\u3002\u4f8b\u5982&#xff0c;\u6a21\u62df\u4e00\u4e2a\u9700\u8981\u7b49\u5f85\u6570\u636e\u5e93\u67e5\u8be2\u7684\u63a5\u53e3&#xff1a;<\/p>\n<p>import asyncio<\/p>\n<p>&#064;app.get(&#034;\/slow-tasks&#034;)<br \/>\nasync def get_slow_tasks():<br \/>\n    await asyncio.sleep(2)<\/p>\n<p>    return {<br \/>\n        &#034;message&#034;: &#034;\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2\u5b8c\u6210&#034;,<br \/>\n        &#034;data&#034;: tasks<br \/>\n    } <\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528&#xff1a;<\/p>\n<p>await asyncio.sleep(2) <\/p>\n<p>\u8868\u793a\u5f53\u524d\u8bf7\u6c42\u7b49\u5f85\u65f6&#xff0c;\u4e0d\u4f1a\u4e00\u76f4\u963b\u585e\u6574\u4e2a\u670d\u52a1\u3002FastAPI \u53ef\u4ee5\u5728\u8fd9\u6bb5\u7b49\u5f85\u65f6\u95f4\u91cc\u5904\u7406\u5176\u4ed6\u8bf7\u6c42\u3002<\/p>\n<p>\u4f46\u8981\u6ce8\u610f&#xff1a;<\/p>\n<p>import time<\/p>\n<p>&#064;app.get(&#034;\/bad-example&#034;)<br \/>\nasync def bad_example():<br \/>\n    time.sleep(2)<\/p>\n<p>    return {&#034;message&#034;: &#034;\u4e0d\u63a8\u8350\u8fd9\u6837\u5199&#034;} <\/p>\n<p>\u867d\u7136\u63a5\u53e3\u5b9a\u4e49\u6210\u4e86 async def&#xff0c;\u4f46 time.sleep() \u4ecd\u7136\u662f\u963b\u585e\u64cd\u4f5c&#xff0c;\u4f1a\u5f71\u54cd\u5f02\u6b65\u6027\u80fd\u3002<\/p>\n<p>\u6b63\u786e\u601d\u8def&#xff1a;<\/p>\n<table>\n<tr>\u573a\u666f\u63a8\u8350\u5199\u6cd5<\/tr>\n<tbody>\n<tr>\n<td>\u5f02\u6b65 HTTP \u8bf7\u6c42<\/td>\n<td>\u4f7f\u7528 httpx.AsyncClient<\/td>\n<\/tr>\n<tr>\n<td>\u5f02\u6b65\u6570\u636e\u5e93<\/td>\n<td>\u4f7f\u7528\u5f02\u6b65\u6570\u636e\u5e93\u9a71\u52a8<\/td>\n<\/tr>\n<tr>\n<td>\u5f02\u6b65 Redis<\/td>\n<td>\u4f7f\u7528\u5f02\u6b65 Redis \u5ba2\u6237\u7aef<\/td>\n<\/tr>\n<tr>\n<td>\u6a21\u62df\u5f02\u6b65\u7b49\u5f85<\/td>\n<td>await asyncio.sleep()<\/td>\n<\/tr>\n<tr>\n<td>CPU \u5bc6\u96c6\u578b\u8ba1\u7b97<\/td>\n<td>\u8003\u8651\u7ebf\u7a0b\u6c60\u3001\u8fdb\u7a0b\u6c60\u3001\u4efb\u52a1\u961f\u5217<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u56e0\u6b64\u53ef\u4ee5\u8bb0\u4f4f\u4e00\u53e5\u8bdd&#xff1a;<\/p>\n<p>async def \u672c\u8eab\u4e0d\u4f1a\u8ba9\u4ee3\u7801\u53d8\u5feb&#xff1b;\u771f\u6b63\u51b3\u5b9a\u662f\u5426\u5f02\u6b65\u7684\u662f\u5185\u90e8\u8c03\u7528\u7684\u64cd\u4f5c\u662f\u5426\u652f\u6301 await\u3002<\/p>\n<hr \/>\n<h3 id=\"%E6%80%BB%E7%BB%93\" style=\"background-color:transparent\">\u603b\u7ed3<\/h3>\n<p>\u672c\u7bc7\u4ece Python \u5bb9\u5668\u7ee7\u7eed\u5411\u540e\u5b66\u4e60\u4e86 Python \u540e\u7aef\u5f00\u53d1\u7684\u91cd\u8981\u57fa\u7840\u3002<\/p>\n<ul>\n<li>\u51fd\u6570&#xff1a;\u5c01\u88c5\u91cd\u590d\u903b\u8f91&#xff1b;<\/li>\n<li>\u7c7b\u4e0e\u5bf9\u8c61&#xff1a;\u7ec4\u7ec7\u6570\u636e\u548c\u884c\u4e3a&#xff1b;<\/li>\n<li>\u88c5\u9970\u5668&#xff1a;\u4e3a\u51fd\u6570\u589e\u52a0\u989d\u5916\u80fd\u529b&#xff1b;<\/li>\n<li>\u8fed\u4ee3\u5668&#xff1a;\u6309\u987a\u5e8f\u83b7\u53d6\u6570\u636e&#xff1b;<\/li>\n<li>\u751f\u6210\u5668&#xff1a;\u8282\u7701\u5185\u5b58\u5730\u751f\u6210\u5927\u91cf\u6570\u636e&#xff1b;<\/li>\n<li>asyncio&#xff1a;\u5904\u7406 I\/O \u5bc6\u96c6\u578b\u5f02\u6b65\u4efb\u52a1&#xff1b;<\/li>\n<li>FastAPI&#xff1a;\u5feb\u901f\u6784\u5efa\u73b0\u4ee3 Web API&#xff1b;<\/li>\n<li>Pydantic&#xff1a;\u81ea\u52a8\u6821\u9a8c\u8bf7\u6c42\u53c2\u6570&#xff1b;<\/li>\n<li>HTTPException&#xff1a;\u89c4\u8303\u8fd4\u56de\u63a5\u53e3\u5f02\u5e38&#xff1b;<\/li>\n<li>Swagger&#xff1a;\u901a\u8fc7 \/docs \u81ea\u52a8\u6d4b\u8bd5\u63a5\u53e3\u3002<\/li>\n<\/ul>\n<p>\u672c\u7bc7\u7684\u4efb\u52a1\u7ba1\u7406 API \u76ee\u524d\u4f7f\u7528\u7684\u662f\u5185\u5b58\u5217\u8868\u4fdd\u5b58\u6570\u636e&#xff0c;\u670d\u52a1\u91cd\u542f\u540e\u6570\u636e\u4f1a\u4e22\u5931\u3002<\/p>\n<p>\u4e0b\u4e00\u7bc7\u53ef\u4ee5\u7ee7\u7eed\u5c06\u4efb\u52a1\u7ba1\u7406 API \u5347\u7ea7\u4e3a&#xff1a;<\/p>\n<p>FastAPI<br \/>\n&#043; SQLAlchemy<br \/>\n&#043; MySQL \/ SQLite<br \/>\n&#043; \u7528\u6237\u6ce8\u518c\u767b\u5f55<br \/>\n&#043; JWT \u6743\u9650\u8ba4\u8bc1<br \/>\n&#043; Docker \u5bb9\u5668\u5316\u90e8\u7f72<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55<br \/>\n\u4e00\u3001\u51fd\u6570&#xff1a;\u5c06\u91cd\u590d\u903b\u8f91\u5c01\u88c5\u8d77\u6765<br \/>\n1.1 \u9ed8\u8ba4\u53c2\u6570<br \/>\n1.2 \u5173\u952e\u5b57\u53c2\u6570<br \/>\n1.3 \u53ef\u53d8\u53c2\u6570 *args \u4e0e **kwargs<br \/>\n1.4 \u5c0f\u6848\u4f8b&#xff1a;\u8ba2\u5355\u91d1\u989d\u8ba1\u7b97<br \/>\n\u4e8c\u3001\u7c7b\u4e0e\u5bf9\u8c61&#xff1a;\u8ba9\u6570\u636e\u548c\u884c\u4e3a\u653e\u5728\u4e00\u8d77<br \/>\n2.1 __init__ \u662f\u4ec0\u4e48<br \/>\n2.2 \u7c7b\u548c\u5bf9\u8c61\u7684\u5173\u7cfb<br \/>\n2.3 \u5c0f\u6848\u4f8b&#xff1a;\u7b80\u5355\u4efb\u52a1\u7c7b<br \/>\n\u4e09\u3001\u88c5\u9970\u5668&#xff1a;\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4e5f\u80fd\u589e\u5f3a\u529f\u80fd<br \/>\n3.1 \u6700\u7b80\u5355\u7684\u88c5\u9970\u5668<br \/>\n3.2 \u652f\u6301\u4efb\u610f\u53c2\u6570\u7684\u88c5\u9970\u5668<br \/>\n3.3 Fas<\/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],"topic":[],"class_list":["post-107552","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218 - \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\/107552.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u4e00\u3001\u51fd\u6570&#xff1a;\u5c06\u91cd\u590d\u903b\u8f91\u5c01\u88c5\u8d77\u6765 1.1 \u9ed8\u8ba4\u53c2\u6570 1.2 \u5173\u952e\u5b57\u53c2\u6570 1.3 \u53ef\u53d8\u53c2\u6570 *args \u4e0e **kwargs 1.4 \u5c0f\u6848\u4f8b&#xff1a;\u8ba2\u5355\u91d1\u989d\u8ba1\u7b97 \u4e8c\u3001\u7c7b\u4e0e\u5bf9\u8c61&#xff1a;\u8ba9\u6570\u636e\u548c\u884c\u4e3a\u653e\u5728\u4e00\u8d77 2.1 __init__ \u662f\u4ec0\u4e48 2.2 \u7c7b\u548c\u5bf9\u8c61\u7684\u5173\u7cfb 2.3 \u5c0f\u6848\u4f8b&#xff1a;\u7b80\u5355\u4efb\u52a1\u7c7b \u4e09\u3001\u88c5\u9970\u5668&#xff1a;\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4e5f\u80fd\u589e\u5f3a\u529f\u80fd 3.1 \u6700\u7b80\u5355\u7684\u88c5\u9970\u5668 3.2 \u652f\u6301\u4efb\u610f\u53c2\u6570\u7684\u88c5\u9970\u5668 3.3 Fas\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/107552.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-19T12:21:33+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=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/107552.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/107552.html\",\"name\":\"Python \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-19T12:21:33+00:00\",\"dateModified\":\"2026-09-19T12:21:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/107552.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/107552.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/107552.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218\"}]},{\"@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 \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218 - \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\/107552.html","og_locale":"zh_CN","og_type":"article","og_title":"Python \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u4e00\u3001\u51fd\u6570&#xff1a;\u5c06\u91cd\u590d\u903b\u8f91\u5c01\u88c5\u8d77\u6765 1.1 \u9ed8\u8ba4\u53c2\u6570 1.2 \u5173\u952e\u5b57\u53c2\u6570 1.3 \u53ef\u53d8\u53c2\u6570 *args \u4e0e **kwargs 1.4 \u5c0f\u6848\u4f8b&#xff1a;\u8ba2\u5355\u91d1\u989d\u8ba1\u7b97 \u4e8c\u3001\u7c7b\u4e0e\u5bf9\u8c61&#xff1a;\u8ba9\u6570\u636e\u548c\u884c\u4e3a\u653e\u5728\u4e00\u8d77 2.1 __init__ \u662f\u4ec0\u4e48 2.2 \u7c7b\u548c\u5bf9\u8c61\u7684\u5173\u7cfb 2.3 \u5c0f\u6848\u4f8b&#xff1a;\u7b80\u5355\u4efb\u52a1\u7c7b \u4e09\u3001\u88c5\u9970\u5668&#xff1a;\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4e5f\u80fd\u589e\u5f3a\u529f\u80fd 3.1 \u6700\u7b80\u5355\u7684\u88c5\u9970\u5668 3.2 \u652f\u6301\u4efb\u610f\u53c2\u6570\u7684\u88c5\u9970\u5668 3.3 Fas","og_url":"https:\/\/www.wsisp.com\/helps\/107552.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-19T12:21:33+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/107552.html","url":"https:\/\/www.wsisp.com\/helps\/107552.html","name":"Python \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-19T12:21:33+00:00","dateModified":"2026-09-19T12:21:33+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/107552.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/107552.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/107552.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python \u57fa\u7840\u8fdb\u9636\uff08\u4e09\uff09\uff1a\u4ece\u51fd\u6570\u3001\u7c7b\u5230 asyncio \u4e0e FastAPI \u540e\u7aef\u5f00\u53d1\u5b9e\u6218"}]},{"@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\/107552","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=107552"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/107552\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=107552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=107552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=107552"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=107552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}