{"id":106459,"date":"2026-09-17T13:16:39","date_gmt":"2026-09-17T05:16:39","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/106459.html"},"modified":"2026-09-17T13:16:39","modified_gmt":"2026-09-17T05:16:39","slug":"python%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/106459.html","title":{"rendered":"Python\u5f02\u6b65\u7f16\u7a0b"},"content":{"rendered":"<p>\u5b66\u4e60 FastAPI \u548c Agent \u65f6&#xff0c;\u7ecf\u5e38\u4f1a\u9047\u5230 async\u3001await \u548c\u4e8b\u4ef6\u5faa\u73af\u3002\u7406\u89e3\u5b83\u4eec\u7684\u6267\u884c\u8fc7\u7a0b&#xff0c;\u624d\u80fd\u5224\u65ad\u54ea\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e76\u53d1&#xff0c;\u54ea\u4e9b\u4ee3\u7801\u4f1a\u963b\u585e\u7a0b\u5e8f\u3002<\/p>\n<p>\u4e0b\u9762\u5148\u533a\u5206\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u534f\u7a0b&#xff0c;\u518d\u770b\u5f02\u6b65\u8c03\u7528\u5728 FastAPI \u548c Agent \u4e2d\u7684\u5b9e\u9645\u7528\u6cd5\u3002<\/p>\n<h3>\u4e00\u3001\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u534f\u7a0b<\/h3>\n<p>\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u8d44\u6e90\u7684\u57fa\u672c\u5355\u4f4d\u3002\u4e00\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u5176\u4ed6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u8fdb\u7a0b\u4e4b\u95f4\u9ed8\u8ba4\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5bf9\u65b9\u7684\u5185\u5b58&#xff0c;\u56e0\u6b64\u9694\u79bb\u6027\u8f83\u5f3a&#xff0c;\u4f46\u521b\u5efa\u548c\u5207\u6362\u7684\u6210\u672c\u4e5f\u6bd4\u8f83\u9ad8\u3002<\/p>\n<p>\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u4e2d\u7684\u6267\u884c\u5355\u5143\u3002\u540c\u4e00\u4e2a\u8fdb\u7a0b\u91cc\u7684\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u5730\u5740\u7a7a\u95f4\u548c\u5927\u90e8\u5206\u8d44\u6e90&#xff0c;\u7ebf\u7a0b\u4e4b\u95f4\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u540c\u4e00\u4efd\u6570\u636e&#xff0c;\u6240\u4ee5\u901a\u4fe1\u6bd4\u8f83\u65b9\u4fbf&#xff1b;\u4f46\u5171\u4eab\u6570\u636e\u4e5f\u5e26\u6765\u4e86\u7ade\u6001\u3001\u9501\u548c\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u7ebf\u7a0b\u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6&#xff0c;\u7ebf\u7a0b\u5207\u6362\u53d1\u751f\u5728\u5185\u6838\u63a7\u5236\u4e0b\u3002<\/p>\n<p>\u534f\u7a0b\u901a\u5e38\u8fd0\u884c\u5728\u7ebf\u7a0b\u4e4b\u4e0a\u3002\u591a\u4e2a\u534f\u7a0b\u53ef\u4ee5\u5171\u7528\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u5b83\u4eec\u4e0d\u4f1a\u7531\u64cd\u4f5c\u7cfb\u7edf\u5f3a\u5236\u5207\u6362&#xff0c;\u800c\u662f\u5728\u4ee3\u7801\u6267\u884c\u5230 await \u7b49\u4f4d\u7f6e\u65f6\u4e3b\u52a8\u4ea4\u51fa\u6267\u884c\u6743\u3002\u534f\u7a0b\u7684\u5207\u6362\u6210\u672c\u8f83\u4f4e&#xff0c;\u4f46\u524d\u63d0\u662f\u534f\u7a0b\u4e0d\u80fd\u957f\u65f6\u95f4\u6267\u884c\u963b\u585e\u4ee3\u7801&#xff0c;\u5426\u5219\u540c\u4e00\u7ebf\u7a0b\u4e0a\u7684\u5176\u4ed6\u534f\u7a0b\u90fd\u4f1a\u88ab\u5361\u4f4f\u3002<\/p>\n<p>\u53ef\u4ee5\u628a\u4e09\u8005\u7b80\u5355\u7406\u89e3\u4e3a&#xff1a;\u8fdb\u7a0b\u8d1f\u8d23\u8d44\u6e90\u9694\u79bb&#xff0c;\u7ebf\u7a0b\u8d1f\u8d23\u6267\u884c&#xff0c;\u534f\u7a0b\u8d1f\u8d23\u5728\u540c\u4e00\u7ebf\u7a0b\u5185\u7ec4\u7ec7\u53ef\u6682\u505c\u7684\u4efb\u52a1\u3002\u5b83\u4eec\u5e76\u4e0d\u662f\u4e92\u76f8\u66ff\u4ee3\u7684\u5173\u7cfb&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u7ebf\u7a0b&#xff0c;\u4e00\u4e2a\u7ebf\u7a0b\u4e5f\u53ef\u4ee5\u8fd0\u884c\u591a\u4e2a\u534f\u7a0b\u3002<\/p>\n<p>\u672c\u6587\u5173\u6ce8\u7684\u662f\u534f\u7a0b\u3002\u8981\u8ba9\u591a\u4e2a\u534f\u7a0b\u6309\u7167\u7b49\u5f85\u72b6\u6001\u8f6e\u6d41\u6267\u884c&#xff0c;\u8fd8\u9700\u8981\u4e00\u4e2a\u8d1f\u8d23\u8c03\u5ea6\u5b83\u4eec\u7684\u673a\u5236&#xff0c;\u8fd9\u5c31\u662f\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<h3>\u4e8c\u3001\u534f\u7a0b\u4e0e\u4e8b\u4ef6\u5faa\u73af<\/h3>\n<p>\u534f\u7a0b\u53ef\u4ee5\u770b\u6210\u4e00\u79cd\u80fd\u591f\u6682\u505c\u548c\u6062\u590d\u7684\u51fd\u6570\u3002\u666e\u901a\u51fd\u6570\u4ece\u7b2c\u4e00\u884c\u6267\u884c\u5230\u6700\u540e\u4e00\u884c&#xff0c;\u4e2d\u9014\u4e0d\u80fd\u4e3b\u52a8\u4ea4\u51fa\u6267\u884c\u6743&#xff1b;\u534f\u7a0b\u6267\u884c\u5230 await \u65f6&#xff0c;\u53ef\u4ee5\u6682\u65f6\u505c\u4e0b\u6765&#xff0c;\u628a\u6267\u884c\u673a\u4f1a\u4ea4\u7ed9\u5176\u4ed6\u4efb\u52a1&#xff0c;\u7b49\u5f85\u7684\u64cd\u4f5c\u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u3002<\/p>\n<p>\u4e8b\u4ef6\u5faa\u73af\u662f\u8d1f\u8d23\u8c03\u5ea6\u534f\u7a0b\u7684\u7ba1\u7406\u8005\u3002\u5b83\u4e0d\u65ad\u68c0\u67e5\u54ea\u4e9b\u4efb\u52a1\u5df2\u7ecf\u51c6\u5907\u597d\u3001\u54ea\u4e9b\u4efb\u52a1\u6b63\u5728\u7b49\u5f85 I\/O&#xff0c;\u7136\u540e\u8fd0\u884c\u5f53\u524d\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u7684\u4efb\u52a1\u3002\u4e8b\u4ef6\u5faa\u73af\u901a\u5e38\u8fd0\u884c\u5728\u7ebf\u7a0b\u4e2d&#xff0c;\u5f02\u6b65\u7a0b\u5e8f\u6b63\u662f\u901a\u8fc7\u8fd9\u79cd\u201c\u7b49\u5f85\u65f6\u5207\u6362\u4efb\u52a1\u201d\u7684\u65b9\u5f0f\u63d0\u9ad8\u7ebf\u7a0b\u5229\u7528\u7387\u3002<\/p>\n<p>\u4e8b\u4ef6\u5faa\u73af\u9700\u8981\u4e00\u4e2a\u5165\u53e3\u624d\u80fd\u542f\u52a8&#xff0c;asyncio.run \u5c31\u662f\u5e38\u7528\u7684\u5165\u53e3\u3002\u540e\u9762\u7684\u793a\u4f8b\u4f1a\u4ece\u540c\u6b65\u8c03\u7528\u5f00\u59cb&#xff0c;\u5148\u89c2\u5bdf\u7b49\u5f85\u65f6\u95f4&#xff0c;\u518d\u6539\u6210\u534f\u7a0b\u3002<\/p>\n<h3>\u4e09\u3001\u540c\u6b65\u8c03\u7528<\/h3>\n<p>\u5148\u7528\u4e24\u4e2a\u8017\u65f6\u76f8\u8fd1\u7684\u51fd\u6570\u6a21\u62df\u5916\u90e8\u5de5\u5177&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> time<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">load_user<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\ntime<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token string\">&#034;user loaded&#034;<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">load_orders<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\ntime<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token string\">&#034;orders loaded&#034;<\/span><\/p>\n<p>start_time<span class=\"token operator\">&#061;<\/span>time<span class=\"token punctuation\">.<\/span>perf_counter<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\nuser<span class=\"token operator\">&#061;<\/span>load_user<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\norders<span class=\"token operator\">&#061;<\/span>load_orders<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\nelapsed<span class=\"token operator\">&#061;<\/span>time<span class=\"token punctuation\">.<\/span>perf_counter<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&#8211;<\/span>start_time<\/p>\n<p><span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>user<span class=\"token punctuation\">,<\/span>orders<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;elapsed: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>elapsed<span class=\"token punctuation\">:<\/span><span class=\"token format-spec\">.2f<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">s&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u4e24\u4e2a\u51fd\u6570\u4e4b\u95f4\u6ca1\u6709\u6570\u636e\u4f9d\u8d56&#xff0c;\u7406\u8bba\u4e0a\u53ef\u4ee5\u540c\u65f6\u7b49\u5f85&#xff0c;\u4f46\u540c\u6b65\u5199\u6cd5\u53ea\u80fd\u5148\u6267\u884c load_user&#xff0c;\u5b83\u8fd4\u56de\u4ee5\u540e\u624d\u4f1a\u6267\u884c load_orders\u3002\u56e0\u6b64\u603b\u8017\u65f6\u5927\u7ea6\u662f\u4e24\u6b21\u7b49\u5f85\u65f6\u95f4\u4e4b\u548c\u3002<\/p>\n<p>\u8fd9\u91cc\u7684 sleep \u53ea\u662f\u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\u3002\u771f\u5b9e\u573a\u666f\u4e2d&#xff0c;\u6570\u636e\u5e93\u67e5\u8be2\u3001HTTP \u8bf7\u6c42\u548c\u6a21\u578b\u8c03\u7528\u90fd\u53ef\u80fd\u5728\u7b49\u5f85\u6570\u636e\u8fd4\u56de\u3002\u7b49\u5f85\u671f\u95f4&#xff0c;CPU \u5e76\u6ca1\u6709\u6301\u7eed\u8ba1\u7b97&#xff0c;\u4f46\u5f53\u524d\u7ebf\u7a0b\u88ab\u540c\u6b65\u8c03\u7528\u5360\u4f4f\u4e86\u3002<\/p>\n<h3>\u56db\u3001\u5f02\u6b65\u51fd\u6570<\/h3>\n<p>\u628a\u51fd\u6570\u6539\u6210\u5f02\u6b65\u51fd\u6570&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> asyncio<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">load_user<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/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\">2<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token string\">&#034;user loaded&#034;<\/span><\/p>\n<p>\u8c03\u7528 load_user() \u65f6&#xff0c;\u5e76\u4e0d\u4f1a\u7acb\u5373\u5f97\u5230\u5b57\u7b26\u4e32&#xff0c;\u800c\u662f\u5f97\u5230\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61&#xff1a;<\/p>\n<p>result<span class=\"token operator\">&#061;<\/span>load_user<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u8fd9\u91cc\u6253\u5370\u51fa\u7684\u4e0d\u662f\u51fd\u6570\u8fd4\u56de\u503c&#xff0c;\u800c\u662f\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\u3002\u5b83\u53ea\u662f\u5bf9\u201c\u4ee5\u540e\u5982\u4f55\u6267\u884c\u8fd9\u6bb5\u5f02\u6b65\u4ee3\u7801\u201d\u7684\u63cf\u8ff0&#xff1b;\u5982\u679c\u65e2\u4e0d await&#xff0c;\u4e5f\u4e0d\u521b\u5efa Task&#xff0c;\u51fd\u6570\u4f53\u5c31\u4e0d\u4f1a\u6267\u884c&#xff0c;\u7a0b\u5e8f\u7ed3\u675f\u65f6\u8fd8\u53ef\u80fd\u51fa\u73b0\u201c\u534f\u7a0b\u4ece\u672a\u7b49\u5f85\u201d\u7684\u8b66\u544a\u3002\u53ea\u6709\u628a\u5b83\u4ea4\u7ed9\u4e8b\u4ef6\u5faa\u73af&#xff0c;\u4ee3\u7801\u624d\u4f1a\u771f\u6b63\u8fd0\u884c\u3002\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 asyncio.run&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\nresult<span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">await<\/span> load_user<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">)<\/span><\/p>\n<p>asyncio<span class=\"token punctuation\">.<\/span>run<span class=\"token punctuation\">(<\/span>main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>await \u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u6b21\u4e3b\u52a8\u8ba9\u51fa\u6267\u884c\u6743\u7684\u64cd\u4f5c\u3002\u6267\u884c\u5230 await asyncio.sleep(2) \u65f6&#xff0c;\u5f53\u524d\u534f\u7a0b\u6682\u65f6\u6302\u8d77&#xff0c;\u4e8b\u4ef6\u5faa\u73af\u53ef\u4ee5\u53bb\u8fd0\u884c\u5176\u4ed6\u5df2\u7ecf\u51c6\u5907\u597d\u7684\u4efb\u52a1\u3002\u7b49\u5f85\u7ed3\u675f\u540e&#xff0c;\u4e8b\u4ef6\u5faa\u73af\u518d\u6062\u590d\u8fd9\u4e2a\u534f\u7a0b\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f&#xff0c;await \u7b49\u5f85\u7684\u662f\u4e00\u4e2a\u53ef\u7b49\u5f85\u5bf9\u8c61&#xff0c;\u5e38\u89c1\u7684\u5305\u62ec\u534f\u7a0b\u5bf9\u8c61\u3001Task \u548c Future\u3002async def \u53ea\u662f\u5b9a\u4e49\u4e86\u534f\u7a0b\u51fd\u6570&#xff0c;\u5e76\u4e0d\u4ee3\u8868\u51fd\u6570\u8c03\u7528\u4f1a\u81ea\u52a8\u5e76\u53d1\u3002<\/p>\n<h3>\u4e94\u3001\u4e32\u884c\u4e0e\u5e76\u53d1<\/h3>\n<p>\u4e0b\u9762\u4e24\u79cd\u5199\u6cd5\u770b\u8d77\u6765\u5f88\u63a5\u8fd1&#xff0c;\u6267\u884c\u6548\u679c\u5374\u4e0d\u540c\u3002<\/p>\n<p>\u7b2c\u4e00\u79cd\u4ecd\u7136\u662f\u4e32\u884c\u7b49\u5f85&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\nuser<span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">await<\/span> load_user<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\norders<span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">await<\/span> load_orders<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>user<span class=\"token punctuation\">,<\/span>orders<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u7b2c\u4e00\u4e2a await \u8fd4\u56de\u4ee5\u540e&#xff0c;\u7b2c\u4e8c\u4e2a\u534f\u7a0b\u624d\u5f00\u59cb\u6267\u884c\u3002\u51fd\u6570\u867d\u7136\u662f\u5f02\u6b65\u7684&#xff0c;\u4f46\u4e24\u4e2a\u64cd\u4f5c\u6ca1\u6709\u5e76\u53d1\u8d77\u6765\u3002<\/p>\n<p>\u5982\u679c\u4e24\u4e2a\u5de5\u5177\u4e4b\u95f4\u6ca1\u6709\u4f9d\u8d56&#xff0c;\u53ef\u4ee5\u5148\u521b\u5efa\u4efb\u52a1&#xff0c;\u518d\u7edf\u4e00\u7b49\u5f85&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\nuser_task<span class=\"token operator\">&#061;<\/span>asyncio<span class=\"token punctuation\">.<\/span>create_task<span class=\"token punctuation\">(<\/span>load_user<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\norders_task<span class=\"token operator\">&#061;<\/span>asyncio<span class=\"token punctuation\">.<\/span>create_task<span class=\"token punctuation\">(<\/span>load_orders<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\nuser<span class=\"token punctuation\">,<\/span>orders<span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">await<\/span> asyncio<span class=\"token punctuation\">.<\/span>gather<span class=\"token punctuation\">(<\/span>user_task<span class=\"token punctuation\">,<\/span>orders_task<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>user<span class=\"token punctuation\">,<\/span>orders<span class=\"token punctuation\">)<\/span><\/p>\n<p>start_time<span class=\"token operator\">&#061;<\/span>time<span class=\"token punctuation\">.<\/span>perf_counter<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\nasyncio<span class=\"token punctuation\">.<\/span>run<span class=\"token punctuation\">(<\/span>main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\nelapsed<span class=\"token operator\">&#061;<\/span>time<span class=\"token punctuation\">.<\/span>perf_counter<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&#8211;<\/span>start_time<br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;elapsed: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>elapsed<span class=\"token punctuation\">:<\/span><span class=\"token format-spec\">.2f<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">s&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>create_task \u4f1a\u628a\u534f\u7a0b\u767b\u8bb0\u4e3a\u4e8b\u4ef6\u5faa\u73af\u53ef\u4ee5\u8c03\u5ea6\u7684\u4efb\u52a1&#xff0c;gather \u5219\u8d1f\u8d23\u7b49\u5f85\u8fd9\u4e9b\u4efb\u52a1\u5168\u90e8\u7ed3\u675f\u5e76\u6536\u96c6\u7ed3\u679c\u3002\u8fd4\u56de\u503c\u7684\u987a\u5e8f\u4e0e\u4f20\u5165\u4efb\u52a1\u7684\u987a\u5e8f\u4e00\u81f4&#xff0c;\u4e0d\u4f1a\u56e0\u4e3a\u67d0\u4e2a\u4efb\u52a1\u5148\u5b8c\u6210\u5c31\u6539\u53d8\u4f4d\u7f6e\u3002\u4e24\u4e2a\u4efb\u52a1\u90fd\u5728\u7b49\u5f85\u65f6&#xff0c;\u7ebf\u7a0b\u53ef\u4ee5\u4ea4\u66ff\u63a8\u8fdb\u5b83\u4eec&#xff0c;\u56e0\u6b64\u603b\u8017\u65f6\u901a\u5e38\u63a5\u8fd1\u5176\u4e2d\u6700\u957f\u7684\u4e00\u6b21\u7b49\u5f85&#xff0c;\u800c\u4e0d\u662f\u4e24\u6b21\u7b49\u5f85\u4e4b\u548c\u3002<\/p>\n<p>\u8fd9\u4e0d\u662f\u591a\u7ebf\u7a0b\u3002\u8fd9\u91cc\u4ecd\u7136\u53ef\u4ee5\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u4efb\u52a1\u5728\u7b49\u5f85 I\/O \u65f6\u4ea4\u66ff\u6267\u884c\u3002\u5982\u679c\u4efb\u52a1\u672c\u8eab\u662f\u5927\u91cf CPU \u8ba1\u7b97&#xff0c;\u4e8b\u4ef6\u5faa\u73af\u4e0d\u4f1a\u56e0\u4e3a async \u81ea\u52a8\u83b7\u5f97\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<h3>\u516d\u3001FastAPI \u548c Agent<\/h3>\n<p>FastAPI \u4e2d\u4f7f\u7528 async def \u58f0\u660e\u7684\u8def\u7531\u4f1a\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u6267\u884c&#xff1b;\u4f7f\u7528\u666e\u901a def \u58f0\u660e\u7684\u8def\u7531&#xff0c;\u5219\u7531\u6846\u67b6\u653e\u5230\u7ebf\u7a0b\u6c60\u4e2d\u6267\u884c\u3002\u8def\u7531\u51fd\u6570\u91cc\u5982\u679c\u8981\u7b49\u5f85\u5f02\u6b65 I\/O&#xff0c;\u5e94\u4f7f\u7528 async def&#xff0c;\u5e76\u4e14\u4e0d\u80fd\u5728\u5df2\u7ecf\u8fd0\u884c\u7684\u4e8b\u4ef6\u5faa\u73af\u4e2d\u518d\u6b21\u8c03\u7528 asyncio.run\u3002<\/p>\n<p><span class=\"token keyword\">import<\/span> asyncio<\/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;\/summary&#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\">summary<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\nuser_task<span class=\"token operator\">&#061;<\/span>asyncio<span class=\"token punctuation\">.<\/span>create_task<span class=\"token punctuation\">(<\/span>load_user<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\norders_task<span class=\"token operator\">&#061;<\/span>asyncio<span class=\"token punctuation\">.<\/span>create_task<span class=\"token punctuation\">(<\/span>load_orders<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\nuser<span class=\"token punctuation\">,<\/span>orders<span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">await<\/span> asyncio<span class=\"token punctuation\">.<\/span>gather<span class=\"token punctuation\">(<\/span>user_task<span class=\"token punctuation\">,<\/span>orders_task<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;user&#034;<\/span><span class=\"token punctuation\">:<\/span>user<span class=\"token punctuation\">,<\/span><span class=\"token string\">&#034;orders&#034;<\/span><span class=\"token punctuation\">:<\/span>orders<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e00\u6b21 Agent \u8bf7\u6c42\u4e5f\u53ef\u4ee5\u91c7\u7528\u540c\u6837\u7684\u7ed3\u6784\u3002\u6bd4\u5982\u5148\u540c\u65f6\u67e5\u8be2\u77e5\u8bc6\u5e93\u548c\u7528\u6237\u914d\u7f6e&#xff0c;\u518d\u628a\u4e24\u4efd\u7ed3\u679c\u4ea4\u7ed9\u540e\u7eed\u6a21\u578b\u8c03\u7528&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">call_tool<\/span><span class=\"token punctuation\">(<\/span>tool_name<span class=\"token punctuation\">)<\/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\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>tool_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> result&#034;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">run_agent<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\nknowledge_task<span class=\"token operator\">&#061;<\/span>asyncio<span class=\"token punctuation\">.<\/span>create_task<span class=\"token punctuation\">(<\/span>call_tool<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;knowledge&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\nprofile_task<span class=\"token operator\">&#061;<\/span>asyncio<span class=\"token punctuation\">.<\/span>create_task<span class=\"token punctuation\">(<\/span>call_tool<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;profile&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\nknowledge<span class=\"token punctuation\">,<\/span>profile<span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">await<\/span> asyncio<span class=\"token punctuation\">.<\/span>gather<span class=\"token punctuation\">(<\/span><br \/>\nknowledge_task<span class=\"token punctuation\">,<\/span><br \/>\nprofile_task<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>knowledge<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">; <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>profile<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><\/p>\n<p>\u8fd9\u91cc\u80fd\u5426\u5e76\u53d1&#xff0c;\u53d6\u51b3\u4e8e\u4e24\u4e2a\u5de5\u5177\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u6570\u636e\u4f9d\u8d56\u3002\u5982\u679c\u7b2c\u4e8c\u4e2a\u5de5\u5177\u5fc5\u987b\u4f7f\u7528\u7b2c\u4e00\u4e2a\u5de5\u5177\u7684\u8fd4\u56de\u503c&#xff0c;\u5c31\u4e0d\u80fd\u4e3a\u4e86\u8ffd\u6c42\u5e76\u53d1\u5f3a\u884c\u62c6\u5f00\u3002\u5e76\u53d1\u4f18\u5316\u7684\u524d\u63d0\u662f\u5148\u786e\u8ba4\u4efb\u52a1\u4e4b\u95f4\u53ef\u4ee5\u72ec\u7acb\u6267\u884c\u3002<\/p>\n<h3>\u4e03\u3001\u963b\u585e\u8c03\u7528<\/h3>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u867d\u7136\u4f7f\u7528\u4e86 async def&#xff0c;\u4f46\u4ecd\u7136\u4f1a\u963b\u585e\u4e8b\u4ef6\u5faa\u73af&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">bad_tool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\ntime<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token string\">&#034;done&#034;<\/span><\/p>\n<p>time.sleep \u4e0d\u4f1a\u628a\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u4e8b\u4ef6\u5faa\u73af\u3002\u53ea\u8981\u5b83\u8fd0\u884c&#xff0c;\u5f53\u524d\u7ebf\u7a0b\u5c31\u4f1a\u505c\u5728\u90a3\u91cc&#xff0c;\u5176\u4ed6\u534f\u7a0b\u4e5f\u65e0\u6cd5\u5f97\u5230\u6267\u884c\u673a\u4f1a\u3002<\/p>\n<p>\u5f02\u6b65\u4ee3\u7801\u4e2d\u5e94\u4f18\u5148\u4f7f\u7528\u5f02\u6b65\u5e93&#xff0c;\u4f8b\u5982\u7528 asyncio.sleep \u6a21\u62df\u7b49\u5f85&#xff1b;\u5982\u679c\u53ea\u80fd\u8c03\u7528\u540c\u6b65\u51fd\u6570&#xff0c;\u53ef\u4ee5\u628a\u5b83\u653e\u5230\u7ebf\u7a0b\u4e2d\u6267\u884c&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">call_blocking_tool<\/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 keyword\">await<\/span> asyncio<span class=\"token punctuation\">.<\/span>to_thread<span class=\"token punctuation\">(<\/span>load_user<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u8fd9\u5e76\u4e0d\u4f1a\u628a\u540c\u6b65\u51fd\u6570\u53d8\u6210\u771f\u6b63\u7684\u5f02\u6b65\u51fd\u6570&#xff0c;\u800c\u662f\u907f\u514d\u5b83\u76f4\u63a5\u5360\u4f4f\u4e8b\u4ef6\u5faa\u73af\u7ebf\u7a0b\u3002\u6570\u636e\u5e93\u9a71\u52a8\u3001HTTP \u5ba2\u6237\u7aef\u7b49\u7ec4\u4ef6\u5e94\u4f18\u5148\u9009\u62e9\u4e0e\u5f02\u6b65\u6a21\u578b\u5339\u914d\u7684\u5b9e\u73b0&#xff0c;\u5426\u5219\u63a5\u53e3\u51fd\u6570\u5199\u6210 async def \u4e5f\u4e0d\u80fd\u5e26\u6765\u9884\u671f\u6548\u679c\u3002<\/p>\n<h3>\u516b\u3001\u8d85\u65f6\u3001\u5f02\u5e38\u548c\u53d6\u6d88<\/h3>\n<p>\u5de5\u5177\u8c03\u7528\u4e0d\u80fd\u53ea\u8003\u8651\u6b63\u5e38\u8fd4\u56de\u3002\u4e00\u4e2a\u5916\u90e8\u670d\u52a1\u53ef\u80fd\u8fdf\u8fdf\u4e0d\u54cd\u5e94&#xff0c;\u4e5f\u53ef\u80fd\u5728\u5e76\u53d1\u4efb\u52a1\u4e2d\u9014\u5931\u8d25\u3002\u53ef\u4ee5\u4e3a\u5355\u6b21\u5de5\u5177\u8c03\u7528\u8bbe\u7f6e\u8d85\u65f6&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">call_with_timeout<\/span><span class=\"token punctuation\">(<\/span>tool_name<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">try<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">async<\/span> <span class=\"token keyword\">with<\/span> asyncio<span class=\"token punctuation\">.<\/span>timeout<span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token keyword\">await<\/span> call_tool<span class=\"token punctuation\">(<\/span>tool_name<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">except<\/span> TimeoutError<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>tool_name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> timeout&#034;<\/span><\/span><\/p>\n<p>\u591a\u4e2a\u4efb\u52a1\u4e00\u8d77\u7b49\u5f85\u65f6&#xff0c;\u5f02\u5e38\u5904\u7406\u7b56\u7565\u9700\u8981\u63d0\u524d\u786e\u5b9a\u3002asyncio.gather \u9ed8\u8ba4\u4f1a\u628a\u5f02\u5e38\u4f20\u7ed9\u8c03\u7528\u65b9&#xff1b;\u5982\u679c\u5e0c\u671b\u6536\u96c6\u6bcf\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c\u548c\u5f02\u5e38&#xff0c;\u53ef\u4ee5\u4f7f\u7528 return_exceptions&#061;True&#xff1a;<\/p>\n<p>results<span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">await<\/span> asyncio<span class=\"token punctuation\">.<\/span>gather<span class=\"token punctuation\">(<\/span><br \/>\ncall_tool<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;knowledge&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\ncall_tool<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;profile&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\nreturn_exceptions<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">for<\/span> result <span class=\"token keyword\">in<\/span> results<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token builtin\">isinstance<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">,<\/span>BaseException<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;tool failed: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>result<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">else<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>result<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u53d6\u6d88\u4e5f\u662f\u5f02\u6b65\u7a0b\u5e8f\u7684\u6b63\u5e38\u8def\u5f84\u3002\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u3001\u8bf7\u6c42\u8d85\u65f6\u6216\u4e0a\u5c42\u4efb\u52a1\u7ed3\u675f\u65f6&#xff0c;\u6b63\u5728\u7b49\u5f85\u7684\u534f\u7a0b\u53ef\u80fd\u6536\u5230 asyncio.CancelledError\u3002\u5982\u679c\u6355\u83b7\u5b83&#xff0c;\u901a\u5e38\u5e94\u5148\u5b8c\u6210\u6e05\u7406&#xff0c;\u518d\u7ee7\u7eed\u629b\u51fa&#xff0c;\u800c\u4e0d\u662f\u6084\u6084\u541e\u6389\u53d6\u6d88\u4fe1\u53f7&#xff1a;<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">long_tool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">try<\/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\">10<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token string\">&#034;done&#034;<\/span><br \/>\n<span class=\"token keyword\">except<\/span> asyncio<span class=\"token punctuation\">.<\/span>CancelledError<span class=\"token punctuation\">:<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;tool cancelled&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">raise<\/span><\/p>\n<p>\u5728 Agent \u573a\u666f\u4e2d&#xff0c;\u8d85\u65f6\u3001\u5f02\u5e38\u548c\u53d6\u6d88\u90fd\u5e94\u8f6c\u6362\u6210\u660e\u786e\u7684\u8c03\u7528\u7ed3\u679c\u6216\u4e0a\u5c42\u9519\u8bef&#xff0c;\u4e0d\u80fd\u8ba9\u67d0\u4e00\u4e2a\u5de5\u5177\u65e0\u9650\u7b49\u5f85&#xff0c;\u62d6\u4f4f\u6574\u4e2a\u8bf7\u6c42\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>Python \u5f02\u6b65\u7f16\u7a0b\u7684\u6838\u5fc3&#xff0c;\u662f\u8ba9\u534f\u7a0b\u5728\u7b49\u5f85 I\/O \u65f6\u628a\u6267\u884c\u6743\u4ea4\u7ed9\u4e8b\u4ef6\u5faa\u73af&#xff0c;\u518d\u7531\u4e8b\u4ef6\u5faa\u73af\u8c03\u5ea6\u5176\u4ed6\u4efb\u52a1\u3002async def \u5b9a\u4e49\u534f\u7a0b&#xff0c;await \u6682\u505c\u5f53\u524d\u534f\u7a0b&#xff0c;create_task \u548c asyncio.gather \u53ef\u4ee5\u5e76\u53d1\u7b49\u5f85\u76f8\u4e92\u72ec\u7acb\u7684\u64cd\u4f5c&#xff1b;\u4f46 time.sleep \u7b49\u963b\u585e\u8c03\u7528\u4ecd\u4f1a\u5361\u4f4f\u4e8b\u4ef6\u5faa\u73af\u3002FastAPI \u548c Agent \u4e2d\u4f7f\u7528\u5f02\u6b65\u65f6&#xff0c;\u9664\u4e86\u5224\u65ad\u4efb\u52a1\u80fd\u5426\u5e76\u53d1&#xff0c;\u8fd8\u8981\u5904\u7406\u8d85\u65f6\u3001\u5f02\u5e38\u3001\u53d6\u6d88\u548c\u8d44\u6e90\u6e05\u7406\u3002<\/p>\n<p>\u672c\u4eba\u80fd\u529b\u6709\u9650&#xff0c;\u6587\u7ae0\u5982\u6709\u9519\u8bef\u6216\u9057\u6f0f\u4e4b\u5904&#xff0c;\u6b22\u8fce\u6307\u6b63\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60 FastAPI \u548c Agent \u65f6&#xff0c;\u7ecf\u5e38\u4f1a\u9047\u5230 async\u3001await \u548c\u4e8b\u4ef6\u5faa\u73af\u3002\u7406\u89e3\u5b83\u4eec\u7684\u6267\u884c\u8fc7\u7a0b&#xff0c;\u624d\u80fd\u5224\u65ad\u54ea\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e76\u53d1&#xff0c;\u54ea\u4e9b\u4ee3\u7801\u4f1a\u963b\u585e\u7a0b\u5e8f\u3002<br \/>\n\u4e0b\u9762\u5148\u533a\u5206\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u534f\u7a0b&#xff0c;\u518d\u770b\u5f02\u6b65\u8c03\u7528\u5728 FastAPI \u548c Agent \u4e2d\u7684\u5b9e\u9645\u7528\u6cd5\u3002<br \/>\n\u4e00\u3001\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u534f\u7a0b<br \/>\n\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u8d44\u6e90\u7684\u57fa\u672c\u5355\u4f4d\u3002\u4e00\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u5176\u4ed6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u8fdb\u7a0b\u4e4b\u95f4\u9ed8\u8ba4\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5bf9\u65b9\u7684\u5185\u5b58&#xff0c;<\/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":[72,81],"topic":[],"class_list":["post-106459","post","type-post","status-publish","format-standard","hentry","category-server","tag-fastapi","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u5f02\u6b65\u7f16\u7a0b - \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\/106459.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u5f02\u6b65\u7f16\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5b66\u4e60 FastAPI \u548c Agent \u65f6&#xff0c;\u7ecf\u5e38\u4f1a\u9047\u5230 async\u3001await \u548c\u4e8b\u4ef6\u5faa\u73af\u3002\u7406\u89e3\u5b83\u4eec\u7684\u6267\u884c\u8fc7\u7a0b&#xff0c;\u624d\u80fd\u5224\u65ad\u54ea\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e76\u53d1&#xff0c;\u54ea\u4e9b\u4ee3\u7801\u4f1a\u963b\u585e\u7a0b\u5e8f\u3002 \u4e0b\u9762\u5148\u533a\u5206\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u534f\u7a0b&#xff0c;\u518d\u770b\u5f02\u6b65\u8c03\u7528\u5728 FastAPI \u548c Agent \u4e2d\u7684\u5b9e\u9645\u7528\u6cd5\u3002 \u4e00\u3001\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u534f\u7a0b \u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u8d44\u6e90\u7684\u57fa\u672c\u5355\u4f4d\u3002\u4e00\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u5176\u4ed6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u8fdb\u7a0b\u4e4b\u95f4\u9ed8\u8ba4\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5bf9\u65b9\u7684\u5185\u5b58&#xff0c;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/106459.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-17T05:16:39+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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106459.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/106459.html\",\"name\":\"Python\u5f02\u6b65\u7f16\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-17T05:16:39+00:00\",\"dateModified\":\"2026-09-17T05:16:39+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/106459.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/106459.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106459.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u5f02\u6b65\u7f16\u7a0b\"}]},{\"@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\u5f02\u6b65\u7f16\u7a0b - \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\/106459.html","og_locale":"zh_CN","og_type":"article","og_title":"Python\u5f02\u6b65\u7f16\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5b66\u4e60 FastAPI \u548c Agent \u65f6&#xff0c;\u7ecf\u5e38\u4f1a\u9047\u5230 async\u3001await \u548c\u4e8b\u4ef6\u5faa\u73af\u3002\u7406\u89e3\u5b83\u4eec\u7684\u6267\u884c\u8fc7\u7a0b&#xff0c;\u624d\u80fd\u5224\u65ad\u54ea\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e76\u53d1&#xff0c;\u54ea\u4e9b\u4ee3\u7801\u4f1a\u963b\u585e\u7a0b\u5e8f\u3002 \u4e0b\u9762\u5148\u533a\u5206\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u534f\u7a0b&#xff0c;\u518d\u770b\u5f02\u6b65\u8c03\u7528\u5728 FastAPI \u548c Agent \u4e2d\u7684\u5b9e\u9645\u7528\u6cd5\u3002 \u4e00\u3001\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u534f\u7a0b \u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u8d44\u6e90\u7684\u57fa\u672c\u5355\u4f4d\u3002\u4e00\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u5176\u4ed6\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u8fdb\u7a0b\u4e4b\u95f4\u9ed8\u8ba4\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5bf9\u65b9\u7684\u5185\u5b58&#xff0c;","og_url":"https:\/\/www.wsisp.com\/helps\/106459.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-17T05:16:39+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/106459.html","url":"https:\/\/www.wsisp.com\/helps\/106459.html","name":"Python\u5f02\u6b65\u7f16\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-17T05:16:39+00:00","dateModified":"2026-09-17T05:16:39+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/106459.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/106459.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/106459.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python\u5f02\u6b65\u7f16\u7a0b"}]},{"@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\/106459","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=106459"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/106459\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=106459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=106459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=106459"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=106459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}