{"id":204907,"date":"2025-05-29T15:58:15","date_gmt":"2025-05-29T07:58:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204907\/"},"modified":"2025-05-29T15:58:15","modified_gmt":"2025-05-29T07:58:15","slug":"python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e9%94%81%e4%bf%9d%e8%af%81%e5%85%b1%e4%ba%ab%e5%8f%98%e9%87%8f%e7%9a%84%e5%8e%9f%e5%ad%90%e6%93%8d%e4%bd%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204907\/","title":{"rendered":"Python\u591a\u8fdb\u7a0b\u4e2d\u5982\u4f55\u4f7f\u7528\u9501\u4fdd\u8bc1\u5171\u4eab\u53d8\u91cf\u7684\u539f\u5b50\u64cd\u4f5c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u591a\u8fdb\u7a0b\u4e2d\u5982\u4f55\u4f7f\u7528\u9501\u4fdd\u8bc1\u5171\u4eab\u53d8\u91cf\u7684\u539f\u5b50\u64cd\u4f5c\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731983106673bf702a71bd.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5206\u4eab\u591a\u8fdb\u7a0b\u64cd\u4f5c\u5171\u4eab\u53d8\u91cf\u7684\u539f\u5b50\u64cd\u4f5c<\/strong><\/p>\n<p>\u5728 python \u591a\u8fdb\u7a0b\u5904\u7406\u4e2d\uff0c\u534f\u8c03\u591a\u4e2a\u8fdb\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u53d8\u91cf\u8981\u4fdd\u6301\u539f\u5b50\u6027\u5341\u5206\u5173\u952e\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 concurrent.futures \u6a21\u5757\u4e2d\u7684 lock \u5bf9\u8c61\u3002<\/p>\n<p><strong>1. \u521b\u5efa manager \u548c lock<\/strong><\/p>\n<p>\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a manager \u5bf9\u8c61\uff0c\u5b83\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a lock \u5bf9\u8c61\uff0c\u5b83\u5c06\u7528\u4e8e\u4fdd\u62a4\u5bf9\u5171\u4eab\u53d8\u91cf\u7684\u8bbf\u95ee\u3002<\/p>\n<p><strong>2. \u4f7f\u7528\u9501\u8fdb\u884c\u539f\u5b50\u64cd\u4f5c<\/strong><\/p>\n<p>\u5728\u8fdb\u7a0b\u4e2d\u64cd\u4f5c\u5171\u4eab\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 with \u8bed\u53e5\u5c06 lock \u4f5c\u4e3a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002\u8fd9\u5c06\u786e\u4fdd\u6bcf\u6b21\u5171\u4eab\u53d8\u91cf\u88ab\u66f4\u6539\u65f6\u53ea\u5141\u8bb8\u4e00\u4e2a\u8fdb\u7a0b\u8bbf\u95ee\u5b83\uff0c\u4ece\u800c\u4fdd\u6301\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4fee\u6b63\u540e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>from concurrent.futures import ProcessPoolExecutor\nimport ctypes\nfrom multiprocessing import Manager, Lock\nimport os\n\n# \u521b\u5efa Manager \u548c Lock\nmanager = Manager()\nm = manager.Value(ctypes.c_int, 0)\nlock = manager.Lock()\n\ndef calc_number(x: int, y: int, _m, total_tasks: int, _lock):\n    # \u6a21\u62df\u8017\u65f6\u4efb\u52a1\u51fd\u6570\n    # \u6a21\u62df\u8017\u65f6\u8ba1\u7b97\n    res = x ** y\n    \n    # \u7528\u9501\u6765\u4fdd\u8bc1\u539f\u5b50\u64cd\u4f5c\n    with _lock:\n        _m.value += 1\n        current_value = _m.value\n    \n    # \u5f53\u603b\u4efb\u52a1\u6570\u91cf\u548c_m.value\u76f8\u7b49\u7684\u65f6\u5019, \u901a\u77e5\u7b2c\u4e09\u65b9\u4efb\u52a1\u5168\u90e8\u505a\u5b8c\u4e86\n    if current_value == total_tasks:\n        print(True)\n    \n    print(f\"m_value: {current_value}, p_id: {os.getpid()}, res: {res}\")\n\ndef main():\n    # \u4efb\u52a1\u53c2\u6570\n    t1 = (100, 200, 300, 400, 500, 600, 700, 800)\n    t2 = (80, 70, 60, 50, 40, 30, 20, 10)\n\n    len_t = len(t1)\n\n    # \u591a\u8fdb\u7a0b\u6267\u884c\u4efb\u52a1\n    with ProcessPoolExecutor(max_workers=len_t) as executor:\n        for x, y in zip(t1, t2):\n            executor.submit(calc_number, x, y, m, len_t, lock)\n\nif __name__ == \"__main__\":\n    main()<\/pre>\n<p>\u8fd9\u5c06\u786e\u4fdd\u591a\u4e2a\u8fdb\u7a0b\u6bcf\u6b21\u53ea\u4fee\u6539\u5171\u4eab\u53d8\u91cf m_value\uff0c\u4ece\u800c\u5b9e\u73b0\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPython\u591a\u8fdb\u7a0b\u4e2d\u5982\u4f55\u4f7f\u7528\u9501\u4fdd\u8bc1\u5171\u4eab\u53d8\u91cf\u7684\u539f\u5b50\u64cd\u4f5c\uff1f\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","protected":false},"excerpt":{"rendered":"<p>Python\u591a\u8fdb\u7a0b\u4e2d\u5982\u4f55\u4f7f\u7528\u9501\u4fdd&#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-204907","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204907","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=204907"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204907\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}