{"id":204247,"date":"2025-05-29T10:52:46","date_gmt":"2025-05-29T02:52:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204247\/"},"modified":"2025-05-29T10:52:46","modified_gmt":"2025-05-29T02:52:46","slug":"%e5%bc%82%e6%ad%a5%e4%ba%8b%e4%bb%b6%e5%92%8c%e6%9d%a1%e4%bb%b6%ef%bc%9apython-%e4%ba%a4%e9%80%9a%e4%bf%a1%e5%8f%b7%e7%9a%84%e7%a7%98%e5%af%86%e7%94%9f%e6%b4%bb","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204247\/","title":{"rendered":"\u5f02\u6b65\u4e8b\u4ef6\u548c\u6761\u4ef6\uff1aPython \u4ea4\u901a\u4fe1\u53f7\u7684\u79d8\u5bc6\u751f\u6d3b"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5f02\u6b65\u4e8b\u4ef6\u548c\u6761\u4ef6\uff1aPython \u4ea4\u901a\u4fe1\u53f7\u7684\u79d8\u5bc6\u751f\u6d3b<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5f02\u6b65\u4e8b\u4ef6\u548c\u6761\u4ef6\uff1aPython \u4ea4\u901a\u4fe1\u53f7\u7684\u79d8\u5bc6\u751f\u6d3b\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241102\/173051877967259efb52537.jpg\" class=\"aligncenter\"><\/p>\n<p>\u6709\u6ca1\u6709\u60f3\u8fc7\u4f60\u7684 python \u534f\u7a0b\u5982\u4f55\u80fd\u591f\u5728\u4e0d\u9020\u6210\u4ea4\u901a\u62e5\u5835\u7684\u60c5\u51b5\u4e0b\u5f88\u597d\u5730\u534f\u540c\u5de5\u4f5c\uff1f\u8ba9\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u5f02\u6b65\u4e8b\u4ef6\u548c\u6761\u4ef6\u7684\u5f02\u60f3\u5929\u5f00\u7684\u4e16\u754c &#8211; \u9632\u6b62\u5f02\u6b65\u4ee3\u7801\u53d8\u6210\u9a6c\u620f\u56e2\u7684\u65e0\u540d\u82f1\u96c4\uff08\u9664\u975e\u60a8\u613f\u610f\uff09\u3002<\/p>\n<p><strong>\u6d3b\u52a8\uff1a\u5927\u5bb6\u90fd\u5728\u7b49\u5f85\u7684\u7eff\u706f<\/strong><br \/> \u5c06 asyncio.event \u89c6\u4e3a\u4ee3\u7801\u4e2d\u7684\u7ea2\u7eff\u706f\u3002\u534f\u7a0b\u6392\u961f\u5e76\u8010\u5fc3\uff08\u6216\u4e0d\u90a3\u4e48\u8010\u5fc3\uff09\u7b49\u5f85\u706f\u53d8\u7eff\uff0c\u7136\u540e\u518d\u7f29\u5c0f\u3002<\/p>\n<p>\u60f3\u8c61\u4e00\u4e0b\u4f60\u548c\u4e00\u7fa4\u4eba\uff08\u534f\u7a0b\uff09\u5728\u4eba\u884c\u6a2a\u9053\u4e0a\u3002\u884c\u4eba\u4fe1\u53f7\u706f\u662f\u7ea2\u8272\u7684\uff08\u4e8b\u4ef6\u672a\u8bbe\u7f6e\uff09\uff0c\u6240\u4ee5\u6bcf\u4e2a\u4eba\u90fd\u5728\u7b49\u5f85\u3002\u5f53\u5b83\u53d8\u6210\u7eff\u8272\u7684\u90a3\u4e00\u523b\uff08event.set()\uff09\uff0c\u6574\u4e2a\u5c0f\u7ec4\u4e00\u81f4\u524d\u8fdb\u3002\u6ca1\u6709\u4eba\u613f\u610f\u6210\u4e3a\u90a3\u4e2a\u56e0\u4e3a\u95ef\u7ea2\u706f\u800c\u8eb2\u907f\u6c7d\u8f66\u7684\u4eba\u3002<\/p>\n<pre>\n\nimport asyncio\n\nasync def pedestrian(event):\n    print(\"pedestrian is waiting for the signal.\")\n    await event.wait()\n    print(\"pedestrian crosses the street.\")\n\nasync def traffic_light(event):\n    print(\"traffic light is red.\")\n    await asyncio.sleep(2)\n    event.set()\n    print(\"traffic light turns green.\")\n\nasync def main():\n    event = asyncio.event()\n    await asyncio.gather(pedestrian(event), traffic_light(event))\n\nasyncio.run(main())\n\n\n<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>\n\npedestrian is waiting for the signal.\ntraffic light is red.\ntraffic light turns green.\npedestrian crosses the street.\n\n\n<\/pre>\n<p><strong>\u6761\u4ef6\uff1a\u4ff1\u4e50\u90e8 vip \u901a\u884c\u8bc1<\/strong><br \/> asyncio.condition \u5c31\u50cf\u9ad8\u7ea7\u4ff1\u4e50\u90e8\u7684\u4fdd\u9556\u3002\u4f60\u4e0d\u4ec5\u9700\u8981\u4ff1\u4e50\u90e8\u5f00\u653e\uff08\u6709\u6761\u4ef6\uff09\uff0c\u8fd8\u9700\u8981\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\uff08await condition.wait()\uff09\u3002<\/p>\n<p>\u5c1d\u8bd5\u8fdb\u5165\u65f6\u5c1a\u591c\u603b\u4f1a\u7684\u7167\u7247\u3002\u4ff1\u4e50\u90e8\uff08\u6761\u4ef6\uff09\u5bb9\u91cf\u6709\u9650\uff0c\u53ea\u6709\u5f53\u5176\u4ed6\u4eba\u79bb\u5f00\uff08\u6ee1\u8db3\u6761\u4ef6\uff09\u65f6\uff0c\u4fdd\u9556\u624d\u5141\u8bb8\u60a8\u8fdb\u5165\u3002\u60a8\u53ef\u80fd\u4f1a\u5728\u5916\u9762\u7b49\u5f85\uff0c\u7ec3\u4e60\u60a8\u6700\u597d\u7684\u821e\u8e48\u52a8\u4f5c\uff08\u6216\u5c34\u5c2c\u5730\u67e5\u770b\u624b\u673a\uff09\uff0c\u76f4\u5230\u83b7\u5f97\u8ba4\u53ef\u3002<\/p>\n<pre>\n\nimport asyncio\n\nasync def clubber(condition, name):\n    async with condition:\n        print(f\"{name} wants to enter the club.\")\n        await condition.wait()\n        print(f\"{name} enters the club.\")\n\nasync def bouncer(condition):\n    await asyncio.sleep(2)\n    async with condition:\n        print(\"bouncer signals someone can enter.\")\n        condition.notify()\n\nasync def main():\n    condition = asyncio.condition()\n    await asyncio.gather(\n        clubber(condition, \"alice\"),\n        clubber(condition, \"bob\"),\n        bouncer(condition)\n    )\n\nasyncio.run(main())\n\n\n<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>\n\nalice wants to enter the club.\nbob wants to enter the club.\nbouncer signals someone can enter.\nalice enters the club.\n\n\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u53ea\u6709\u4e00\u540d\u4ff1\u4e50\u90e8\u6210\u5458\u53ef\u4ee5\u8fdb\u5165\u4ff1\u4e50\u90e8\uff0c\u56e0\u4e3a\u4fdd\u9556\u53ea\u5411\u4e00\u4e2a\u4eba\u53d1\u51fa\u4fe1\u53f7 (condition.notify())\u3002\u53e6\u4e00\u540d\u4ff1\u4e50\u90e8\u6210\u5458\u4ecd\u5728\u65e0\u9650\u671f\u5730\u7b49\u5f85\u3002\u5982\u679c\u4f60\u60f3\u8ba9\u6bcf\u4e2a\u4eba\u90fd\u8fdb\u6765\uff08\u805a\u4f1a\u65f6\u95f4\uff01\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528condition.notify_all()\u3002<\/p>\n<pre>\n\nimport asyncio\n\nasync def clubber(condition, name):\n    async with condition:\n        print(f\"{name} wants to enter the club.\")\n        await condition.wait()\n        print(f\"{name} enters the club.\")\n\nasync def bouncer(condition):\n    await asyncio.sleep(2)\n    async with condition:\n        print(\"bouncer signals everyone can enter.\")\n        condition.notify_all()\n\nasync def main():\n    condition = asyncio.condition()\n    await asyncio.gather(\n        clubber(condition, \"alice\"),\n        clubber(condition, \"bob\"),\n        bouncer(condition)\n    )\n\nasyncio.run(main())\n\n\n<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>\n\nAlice wants to enter the club.\nBob wants to enter the club.\nBouncer signals everyone can enter.\nAlice enters the club.\nBob enters the club.\n\n\n<\/pre>\n<p><strong>\u805a\u4f1a\u7ed3\u675f<\/strong><br \/> \u73b0\u5728\uff0c\u60a8\u5e94\u8be5\u5bf9 asyncio <strong>events<\/strong> \u548c <strong>conditions<\/strong> \u7684\u5de5\u4f5c\u539f\u7406\u6709\u4e86\u66f4\u6e05\u6670\u7684\u4e86\u89e3\u3002\u4ed6\u4eec\u662f\u5e55\u540e\u534f\u8c03\u8005\uff0c\u786e\u4fdd\u4e00\u5207\u987a\u5229\u8fdb\u884c\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u4e0b\u6b21\u5f53\u4f60\u7684\u5f02\u6b65\u4ee3\u7801\u4e0d\u518d\u50cf\u9ad8\u901f\u516c\u8def\u4e0a\u7684\u649e\u8f66\u4e8b\u6545\u65f6\uff0c\u4f60\u5c31\u77e5\u9053\u8be5\u611f\u8c22\u8c01\u4e86\uff01<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5f02\u6b65\u4e8b\u4ef6\u548c\u6761\u4ef6\uff1aPython \u4ea4\u901a\u4fe1\u53f7\u7684\u79d8\u5bc6\u751f\u6d3b\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u6b65\u4e8b\u4ef6\u548c\u6761\u4ef6\uff1aPython \u4ea4&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204247","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204247"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204247\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}