{"id":204883,"date":"2025-05-29T11:03:49","date_gmt":"2025-05-29T03:03:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204883\/"},"modified":"2025-05-29T11:03:49","modified_gmt":"2025-05-29T03:03:49","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%a4%84%e7%90%86%e5%88%97%e8%a1%a8%e4%b8%ad%e5%ad%97%e5%85%b8%e5%8f%82%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204883\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u591a\u7ebf\u7a0b\u5904\u7406\u5217\u8868\u4e2d\u5b57\u5178\u53c2\u6570\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u591a\u7ebf\u7a0b\u5904\u7406\u5217\u8868\u4e2d\u5b57\u5178\u53c2\u6570\uff1f<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5982\u4f55\u4f7f\u7528 Python \u591a\u7ebf\u7a0b\u5904\u7406\u5217\u8868\u4e2d\u5b57\u5178\u53c2\u6570\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731923148673b0ccc4f278.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u5904\u7406\u5217\u8868\u4e2d\u5b57\u5178\u53c2\u6570<\/strong><\/p>\n<p>\u9488\u5bf9\u9700\u8981\u4f7f\u7528\u591a\u7ebf\u7a0b\u4f20\u9012\u5b57\u5178\u53c2\u6570\u6267\u884c\u51fd\u6570\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528python\u63d0\u4f9b\u7684threadpoolexecutor\u7ebf\u7a0b\u6c60\u9ad8\u6548\u5730\u5b8c\u6210\u6b64\u4efb\u52a1\u3002<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165 n\u00e9cessaire \u5e93\uff1a<\/strong><\/p>\n<pre>import time\nfrom concurrent.futures import threadpoolexecutor\nimport threading<\/pre>\n<\/li>\n<li><strong>\u5b9a\u4e49\u6267\u884c\u51fd\u6570\uff1a<\/strong><br \/>\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a dosome \u7684\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u542b\u8981\u9488\u5bf9\u5b57\u5178\u53c2\u6570\u6267\u884c\u7684\u5b9e\u9645\u903b\u8f91\u3002<\/li>\n<li><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60\uff1a<\/strong><br \/>\u4f7f\u7528 threadpoolexecutor(max_workers=thread_count) \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u5176\u4e2d thread_count \u4e3a\u5e0c\u671b\u521b\u5efa\u7684\u7ebf\u7a0b\u6570\u91cf\u3002<\/li>\n<li><strong>\u63d0\u4ea4\u4efb\u52a1\uff1a<\/strong><br \/>\u5bf9\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u5178\uff0c\u4f7f\u7528 executor.submit(dosome, **m) \u5c06\u4e00\u4e2a\u4efb\u52a1\u63d0\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\uff0c\u5176\u4e2d **m \u6269\u5c55\u4e86\u5b57\u5178\u53c2\u6570\u4ee5\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u3002<\/li>\n<li><strong>\u542f\u52a8\u7ebf\u7a0b\u6c60\uff1a<\/strong><br \/>\u7ebf\u7a0b\u6c60\u5c06\u81ea\u52a8\u5f00\u59cb\u6267\u884c\u63d0\u4ea4\u7684\u4efb\u52a1\u3002<\/li>\n<\/ol>\n<pre>my_list = [{'ip': '192.168.1.2', 'password': '123456', 'user_name': '654321'}, ...]\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>\u6b64\u4ee3\u7801\u5c06\u521b\u5efa3\u4e2a\u7ebf\u7a0b\uff0c\u5e76\u5c06\u5b57\u5178\u53c2\u6570\u4e2d\u7684 ip \u5730\u5740\u6253\u5370\u4e3a\u7ebf\u7a0b\u540d\u79f0\u3002\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>Thread-4 192.168.1.2\nThread-2 192.168.1.3\nThread-1 192.168.1.4\nThread-4 192.168.1.5\nThread-2 192.168.1.6<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u591a\u7ebf\u7a0b\u5904\u7406\u5217\u8868\u4e2d\u5b57\u5178\u53c2\u6570\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 \u591a\u7ebf\u7a0b\u5904&#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-204883","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204883","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=204883"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204883\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}