{"id":204661,"date":"2025-05-29T15:01:22","date_gmt":"2025-05-29T07:01:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204661\/"},"modified":"2025-05-29T15:01:22","modified_gmt":"2025-05-29T07:01:22","slug":"python-%e9%87%87%e9%9b%86%e6%95%b0%e6%8d%ae%e6%97%b6%e5%a6%82%e4%bd%95%e9%99%90%e5%88%b6%e7%ba%bf%e7%a8%8b%e6%95%b0%e9%87%8f%ef%bc%8c%e9%98%b2%e6%ad%a2%e7%a8%8b%e5%ba%8f%e5%b4%a9%e6%ba%83%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204661\/","title":{"rendered":"Python \u91c7\u96c6\u6570\u636e\u65f6\u5982\u4f55\u9650\u5236\u7ebf\u7a0b\u6570\u91cf\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u91c7\u96c6\u6570\u636e\u65f6\u5982\u4f55\u9650\u5236\u7ebf\u7a0b\u6570\u91cf\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\uff1f<\/h1>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/173141122567333d19a6981.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5728\u4f7f\u7528\u7ebf\u7a0b\u8fdb\u884c\u6570\u636e\u91c7\u96c6\u65f6\uff0c\u5982\u679c\u4e0d\u52a0\u4ee5\u63a7\u5236\u7ebf\u7a0b\u7684\u521b\u5efa\u6570\u91cf\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u4e0d\u65ad\u521b\u5efa\u7ebf\u7a0b\uff0c\u6d88\u8017\u5927\u91cf\u5185\u5b58\uff0c\u751a\u81f3\u9020\u6210\u7cfb\u7edf\u5d29\u6e83\u3002\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u9650\u5236\u7ebf\u7a0b\u7684\u521b\u5efa\u6570\u91cf\uff0c\u505a\u5230\u5408\u7406\u5206\u914d\u7ebf\u7a0b\u8d44\u6e90\u3002<\/p>\n<p>\u5728 python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 semaphore \u7c7b\u6765\u9650\u5236\u7ebf\u7a0b\u521b\u5efa\u7684\u6570\u91cf\u3002semaphore \u7c7b\u662f\u4e00\u4e2a\u540c\u6b65\u539f\u8bed\uff0c\u5b83\u53ef\u4ee5\u63a7\u5236\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u7ebf\u7a0b\u6570\u91cf\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 semaphore \u9650\u5236\u7ebf\u7a0b\u521b\u5efa\u6570\u91cf\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>import threading\nimport time\n\n# \u9650\u5236\u7ebf\u7a0b\u7684\u6700\u5927\u6570\u91cf\u4e3a4\u4e2a\nsem = threading.Semaphore(4)\n\ndef gothread():\n    with sem:  # \u9501\u5b9a\u7ebf\u7a0b\u7684\u6700\u5927\u6570\u91cf\n        for i in range(8):\n            print(threading.current_thread().name, i)\n            time.sleep(1)\n\nfor i in range(5):\n    threading.Thread(target=gothread).start()<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 semaphore(4) \u6765\u9650\u5236\u540c\u65f6\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\u6700\u5927\u4e3a4\u4e2a\u3002\u56e0\u6b64\uff0c\u5373\u4f7f\u7a0b\u5e8f\u521b\u5efa\u4e865\u4e2a\u7ebf\u7a0b\uff0c\u4f46\u5b9e\u9645\u4e0a\u6bcf\u6b21\u53ea\u8fd0\u884c4\u4e2a\u7ebf\u7a0b\u3002\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u624d\u4f1a\u88ab\u542f\u52a8\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u7ebf\u7a0b\u521b\u5efa\u7684\u6570\u91cf\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPython \u91c7\u96c6\u6570\u636e\u65f6\u5982\u4f55\u9650\u5236\u7ebf\u7a0b\u6570\u91cf\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u91c7\u96c6\u6570\u636e\u65f6\u5982\u4f55\u9650\u5236&#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-204661","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204661","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=204661"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204661\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}