{"id":204471,"date":"2025-05-29T10:57:00","date_gmt":"2025-05-29T02:57:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204471\/"},"modified":"2025-05-29T10:57:00","modified_gmt":"2025-05-29T02:57:00","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e7%ba%bf%e7%a8%8b%e6%b1%a0%e5%92%8c-semaphore-%e9%81%bf%e5%85%8d%e7%ba%bf%e7%a8%8b%e5%88%9b%e5%bb%ba%e5%af%bc%e8%87%b4%e7%9a%84%e5%86%85%e5%ad%98%e6%b3%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204471\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u7ebf\u7a0b\u6c60\u548c Semaphore \u907f\u514d\u7ebf\u7a0b\u521b\u5efa\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u7ebf\u7a0b\u6c60\u548c Semaphore \u907f\u514d\u7ebf\u7a0b\u521b\u5efa\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\uff1f<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u5982\u4f55\u4f7f\u7528 Python \u7ebf\u7a0b\u6c60\u548c Semaphore \u907f\u514d\u7ebf\u7a0b\u521b\u5efa\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731059140672dddc4429cc.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 python \u7ebf\u7a0b\u6c60\u4e2d\u63a7\u5236\u7ebf\u7a0b\u521b\u5efa\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n<p>\u5728\u60a8\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff0c\u7531\u4e8e\u6bcf\u6b21\u6267\u884c\u90fd\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\uff0c\u5bfc\u81f4\u7ebf\u7a0b\u6570\u91cf\u65e0\u9650\u589e\u52a0\uff0c\u6700\u7ec8\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6765\u7ba1\u7406\u7ebf\u7a0b\u521b\u5efa\u3002<\/p>\n<p><strong>\u7ebf\u7a0b\u6c60<\/strong><\/p>\n<p>\u7ebf\u7a0b\u6c60\u662f\u4e00\u4e2a\u9884\u5148\u5206\u914d\u7684\u7ebf\u7a0b\u7ec4\uff0c\u53ef\u4ee5\u5728\u4efb\u52a1\u5230\u6765\u65f6\u91cd\u590d\u5229\u7528\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u5730\u521b\u5efa\u65b0\u7ebf\u7a0b\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>semaphore<\/strong><\/p>\n<p>semaphore \u662f\u4e00\u4e2a\u7528\u4e8e\u534f\u8c03\u5e76\u53d1\u8bbf\u95ee\u7684\u9501\u5bf9\u8c61\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u9650\u5236\u540c\u65f6\u6267\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\u3002<\/p>\n<p><strong>\u4f18\u5316\u540e\u7684\u4ee3\u7801<\/strong><\/p>\n<p>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u548c semaphore\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u60a8\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>import threading\nimport time\n\n# \u6700\u5927\u540c\u65f6\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\nMAX_THREADS = 4\n\n# \u521b\u5efa\u7ebf\u7a0b\u6c60\nthread_pool = ThreadPool(MAX_THREADS)\n\n# Semaphore \u7528\u4e8e\u63a7\u5236\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u6570\u91cf\nsem = threading.Semaphore(MAX_THREADS)\n\ndef processHTML(html_code):\n    # ...\n\ndef threadDown():\n    with sem:  # \u9650\u5236\u540c\u65f6\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\n        # ...\n\ndef runStart():\n    while initial:\n        # \u4ece initial \u961f\u5217\u4e2d\u83b7\u53d6\u4e00\u4e2a\u5173\u952e\u8bcd\n        keyword = initial.pop(0)\n\n        # \u521b\u5efa\u7ebf\u7a0b\u4efb\u52a1\u5e76\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\n        thread_pool.submit(create_url, keyword)\n\nif __name__ == \"__main__\":\n    runStart()<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f18\u5316\u540e\u7684\u7248\u672c\u4e2d\uff0c\u6bcf\u6b21\u53ea\u80fd\u521b\u5efa\u548c\u8fd0\u884c<strong>\u6700\u591a 4 \u4e2a\u7ebf\u7a0b<\/strong>\u3002semaphore \u786e\u4fdd\u8fd9\u4e00\u70b9\uff0c\u5373\u4f7f initial \u961f\u5217\u4e2d\u6709\u66f4\u591a\u7684\u4efb\u52a1\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u7ebf\u7a0b\u6c60\u548c Semaphore \u907f\u514d\u7ebf\u7a0b\u521b\u5efa\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u7ebf\u7a0b\u6c60\u548c&#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-204471","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204471","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=204471"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204471\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}