{"id":204793,"date":"2025-05-29T10:02:04","date_gmt":"2025-05-29T02:02:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204793\/"},"modified":"2025-05-29T10:02:04","modified_gmt":"2025-05-29T02:02:04","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-threadpoolexecutor-%e5%b9%b6%e8%a1%8c%e6%89%a7%e8%a1%8c%e5%ad%97%e5%85%b8%e5%88%97%e8%a1%a8%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204793\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 ThreadPoolExecutor \u5e76\u884c\u6267\u884c\u5b57\u5178\u5217\u8868\u51fd\u6570\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 ThreadPoolExecutor \u5e76\u884c\u6267\u884c\u5b57\u5178\u5217\u8868\u51fd\u6570\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 ThreadPoolExecutor \u5e76\u884c\u6267\u884c\u5b57\u5178\u5217\u8868\u51fd\u6570\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\/20241116\/173174440667385296b2dda.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u901a\u8fc7\u591a\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u5b57\u5178\u5217\u8868\u51fd\u6570<\/strong><\/p>\n<p>\u5728\u7f16\u5199\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 threadpoolexecutor \u7ebf\u7a0b\u6c60\u6765\u9ad8\u6548\u5730\u5206\u914d\u548c\u7ba1\u7406\u5de5\u4f5c\u7ebf\u7a0b\u3002\u672c\u4f8b\u4e2d\uff0c\u60a8\u7684\u76ee\u6807\u662f\u4f7f\u7528\u591a\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u5178\u53c2\u6570\u5217\u8868\u3002<\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60\uff1a<\/strong>\u4f7f\u7528 threadpoolexecutor \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u6307\u5b9a\u8981\u4f7f\u7528\u7684\u6700\u5927\u5de5\u4f5c\u7ebf\u7a0b\u6570\u3002<\/li>\n<li><strong>\u5faa\u73af\u904d\u5386\u5b57\u5178\u5217\u8868\uff1a<\/strong>\u5bf9\u4e8e\u5b57\u5178\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u5178\uff0c\u4f7f\u7528 submit \u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a future \u5bf9\u8c61\uff0c\u5b83\u5141\u8bb8\u60a8\u8ddf\u8e2a\u4efb\u52a1\u7684\u72b6\u6001\u3002<\/li>\n<li><strong>\u4f20\u9012\u5b57\u5178\u53c2\u6570\uff1a<\/strong>\u4f7f\u7528 ** \u8bed\u6cd5\u5c06\u5b57\u5178\u89e3\u5305\u4e3a\u5355\u72ec\u7684\u51fd\u6570\u53c2\u6570\uff0c\u4ee5\u4fbf\u51fd\u6570\u53ef\u4ee5\u6b63\u786e\u5730\u8bbf\u95ee\u5b83\u4eec\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u7684 python \u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 threadpoolexecutor \u6765\u5e76\u884c\u6267\u884c\u5b57\u5178\u5217\u8868\u51fd\u6570\uff1a<\/p>\n<pre>from concurrent.futures import ThreadPoolExecutor\nimport threading\n\nmy_list = [\n    {'ip': '192.168.1.2', 'password': '123456', 'user_name': '654321'},\n    {'ip': '192.168.1.3', 'password': '123456', 'user_name': '654321'},\n    {'ip': '192.168.1.4', 'password': '123456', 'user_name': '654321'},\n    {'ip': '192.168.1.5', 'password': '123456', 'user_name': '654321'},\n    {'ip': '192.168.1.6', 'password': '123456', 'user_name': '654321'}\n]\n\ndef dosome(ip, password, user_name):\n    tname = threading.current_thread().getName()\n    time.sleep(1)\n    print(f'{tname} {ip}')\n\ntpe = ThreadPoolExecutor(max_workers=3)\n\nfor m in my_list:\n    tpe.submit(dosome, **m)<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cthreadpoolexecutor \u88ab\u914d\u7f6e\u4e3a\u4f7f\u7528 3 \u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u3002\u5bf9\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u5178\uff0csubmit \u65b9\u6cd5\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u5e76\u6267\u884c dosome \u51fd\u6570\uff0c\u4f20\u9012\u5b57\u5178\u4e2d\u7684\u53c2\u6570\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4f7f\u7528 ThreadPoolExecutor \u5e76\u884c\u6267\u884c\u5b57\u5178\u5217\u8868\u51fd\u6570\uff1f \u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 ThreadPoolE&#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-204793","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204793","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=204793"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204793\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}