{"id":531,"date":"2025-07-30T19:24:01","date_gmt":"2025-07-30T11:24:01","guid":{"rendered":"http:\/\/8.154.33.202\/?p=531"},"modified":"2025-08-06T16:52:38","modified_gmt":"2025-08-06T08:52:38","slug":"python%e4%bd%bf%e7%94%a8threadpoolexecutor%e5%ba%93%e8%bf%9b%e8%a1%8c%e5%a4%9a%e7%ba%bf%e7%a8%8b%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"http:\/\/8.154.33.202\/?p=531","title":{"rendered":"python\u4f7f\u7528ThreadPoolExecutor\u3001ProcessPoolExecutor\u8fdb\u884c\u4efb\u52a1\u5e76\u884c"},"content":{"rendered":"\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u8c03\u7528\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from concurrent.futures import ThreadPoolExecutor\n\ndef my_function(arg1, arg2):\n    print(f\"Processing {arg1} and {arg2}\")\n\nwith ThreadPoolExecutor(max_workers=3) as executor:\n    for i in range(5):\n        executor.submit(my_function, i, i*2)\n<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u83b7\u53d6\u8fd4\u56de\u503c\uff0c\u53ef\u901a\u8fc7<code>future.result()<\/code>\u83b7\u53d6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfrom concurrent.futures import ThreadPoolExecutor\nimport time\n\ndef calculate_square(number):\n    time.sleep(1)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    return number * number\n\nwith ThreadPoolExecutor(max_workers=3) as executor:\n    # \u63d0\u4ea4\u4efb\u52a1\u5e76\u4fdd\u5b58future\u5bf9\u8c61\n    future1 = executor.submit(calculate_square, 2)\n    future2 = executor.submit(calculate_square, 3)\n    future3 = executor.submit(calculate_square, 4)\n    \n    # \u83b7\u53d6\u7ed3\u679c\uff08\u4f1a\u963b\u585e\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\uff09\n    print(f\"2\u7684\u5e73\u65b9: {future1.result()}\")\n    print(f\"3\u7684\u5e73\u65b9: {future2.result()}\")\n    print(f\"4\u7684\u5e73\u65b9: {future3.result()}\")\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u591a\u5c11\u7ebf\u7a0b\u6570\u5408\u7406\uff1f\u5bf9\u4e8ecpu\u8ba1\u7b97\u5bc6\u96c6\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528cpu\u6838\u5fc3\u6570\u4f5c\u4e3a\u7ebf\u7a0b\u6570\u3002\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6838\u5fc3\u6570x2\u3002\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u53ef\u4ee5\u83b7\u5f97\u6838\u5fc3\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\ncpu_count = os.cpu_count()  # \u8fd4\u56de\u903b\u8f91\u6838\u5fc3\u6570\uff08\u542b\u8d85\u7ebf\u7a0b\uff09\u200c\nprint(f\"CPU\u6838\u5fc3\u6570: {cpu_count}\")\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u5bf9\u4e8ecpu\u5bc6\u96c6\u578b\u7684\u4efb\u52a1\uff0c\u66f4\u63a8\u8350\u4f7f\u7528\u591a\u8fdb\u7a0b\u7684ProcessPoolExecutor\uff0c\u63a5\u53e3\u548cThreadPoolExecutor\u4e00\u6837\u3002\u4f46\u662f\u8981\u8bf4\u660e\u7684\u662f\uff0c\u5f00\u542f\u8fdb\u7a0b\u3001\u8fdb\u7a0b\u95f4\u901a\u4fe1\u548c\u9500\u6bc1\u8fdb\u7a0b\u8981\u6bd4\u540c\u6837\u7684\u7ebf\u7a0b\u6b65\u9aa4\u8017\u65f6\u66f4\u5927\uff0c\u5982\u679c\u8981\u6267\u884c\u7684\u4efb\u52a1\u4e0d\u662f\u660e\u663e\u7684cpu\u5bc6\u96c6\u578b\uff0c\u90a3\u4e48\u4f7f\u7528ProcessPoolExecutor\u5e76\u4e0d\u4e00\u5b9a\u8981\u5feb\u4e8eThreadPoolExecutor<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528concurrent.futures.ThreadPoolExecutor\u6765\u5b9e\u73b0\u591a\u7ebf &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/8.154.33.202\/?p=531\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cpython\u4f7f\u7528ThreadPoolExecutor\u3001ProcessPoolExecutor\u8fdb\u884c\u4efb\u52a1\u5e76\u884c\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/531"}],"collection":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=531"}],"version-history":[{"count":3,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":534,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/531\/revisions\/534"}],"wp:attachment":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}