{"id":1008558,"date":"2024-12-27T11:03:49","date_gmt":"2024-12-27T03:03:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008558.html"},"modified":"2024-12-27T11:03:52","modified_gmt":"2024-12-27T03:03:52","slug":"python-%e5%a6%82%e4%bd%95%e5%bc%80%e5%90%af%e7%ba%bf%e7%a8%8b%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008558.html","title":{"rendered":"python \u5982\u4f55\u5f00\u542f\u7ebf\u7a0b\u6c60"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083824\/bf313a44-5fbb-45f6-9a91-a852615d5e4e.webp\" alt=\"python \u5982\u4f55\u5f00\u542f\u7ebf\u7a0b\u6c60\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u4e2d\u7684<code>ThreadPoolExecutor<\/code>\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u6c60\u3001\u5b9e\u73b0\u5e76\u53d1\u4efb\u52a1\u8c03\u5ea6\u3002<code>ThreadPoolExecutor<\/code>\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u542f\u52a8\u7ebf\u7a0b\u6c60\u5e76\u7ba1\u7406\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u3002\u5728\u521b\u5efa\u7ebf\u7a0b\u6c60\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u7ebf\u7a0b\u7684\u6570\u91cf\uff0c\u4f7f\u7528<code>submit<\/code>\u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\uff0c\u5e76\u901a\u8fc7<code>result<\/code>\u65b9\u6cd5\u83b7\u53d6\u4efb\u52a1\u7684\u8fd4\u56de\u7ed3\u679c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u5728Python\u4e2d\u5f00\u542f\u7ebf\u7a0b\u6c60\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\u548c\u6982\u5ff5\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u7ebf\u7a0b\u6c60\u7684\u57fa\u672c\u6982\u5ff5<br \/>\u4e8c\u3001<code>concurrent.futures.ThreadPoolExecutor<\/code>\u7684\u4f7f\u7528<br \/>\u4e09\u3001\u7ebf\u7a0b\u6c60\u7684\u521b\u5efa\u4e0e\u7ba1\u7406<br \/>\u56db\u3001\u4efb\u52a1\u63d0\u4ea4\u4e0e\u7ed3\u679c\u83b7\u53d6<br \/>\u4e94\u3001\u7ebf\u7a0b\u6c60\u7684\u4f18\u52bf\u548c\u6ce8\u610f\u4e8b\u9879<br \/>\u516d\u3001\u7ebf\u7a0b\u6c60\u7684\u5e94\u7528\u573a\u666f<br \/>\u4e03\u3001\u4e0e\u5176\u4ed6\u5e76\u53d1\u6a21\u578b\u7684\u6bd4\u8f83<br \/>\u516b\u3001\u7ebf\u7a0b\u6c60\u7684\u8c03\u4f18\u4e0e\u6027\u80fd\u4f18\u5316<br \/>\u4e5d\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u7ebf\u7a0b\u6c60\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u7528\u4e8e\u7ba1\u7406\u548c\u590d\u7528\u4e00\u7ec4\u7ebf\u7a0b\u4ee5\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u7ebf\u7a0b\u6c60\u901a\u8fc7\u9650\u5236\u5e76\u53d1\u7ebf\u7a0b\u7684\u6570\u91cf\u6765\u51cf\u5c11\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500\uff0c\u5e76\u901a\u8fc7\u590d\u7528\u7ebf\u7a0b\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u7ebf\u7a0b\u6c60\u7684\u6838\u5fc3\u601d\u60f3\u662f\u901a\u8fc7\u9884\u5148\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\u6765\u5904\u7406\u4efb\u52a1\uff0c\u5f53\u6709\u65b0\u7684\u4efb\u52a1\u5230\u6765\u65f6\uff0c\u7ebf\u7a0b\u6c60\u4ece\u5df2\u6709\u7684\u7ebf\u7a0b\u4e2d\u9009\u62e9\u4e00\u4e2a\u6765\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001<code>concurrent.futures.ThreadPoolExecutor<\/code>\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>concurrent.futures.ThreadPoolExecutor<\/code>\u662fPython\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u7528\u4e8e\u7ba1\u7406\u7ebf\u7a0b\u6c60\u7684\u7c7b\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\u3001\u63d0\u4ea4\u4efb\u52a1\u4ee5\u53ca\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\u3002\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\uff0c\u5e76\u53d1\u5730\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ebf\u7a0b\u6c60\u7684\u521b\u5efa\u4e0e\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u521b\u5efa\u7ebf\u7a0b\u6c60\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165<code>concurrent.futures<\/code>\u6a21\u5757\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u4f8b\u5316<code>ThreadPoolExecutor<\/code>\u5bf9\u8c61\u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\u3002\u5728\u521b\u5efa\u7ebf\u7a0b\u6c60\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u7ebf\u7a0b\u7684\u6570\u91cf\uff0c\u901a\u5e38\u79f0\u4e3a\u201c\u6c60\u7684\u5927\u5c0f\u201d\u3002\u6c60\u7684\u5927\u5c0f\u53ef\u4ee5\u6839\u636e\u4efb\u52a1\u7684\u7279\u6027\u548c\u7cfb\u7edf\u8d44\u6e90\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b3\u4e2a\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    pass  # \u5728\u8fd9\u91cc\u63d0\u4ea4\u4efb\u52a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b3\u4e2a\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\u3002\u5f53\u521b\u5efa\u7ebf\u7a0b\u6c60\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>with<\/code>\u8bed\u53e5\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u7ebf\u7a0b\u6c60\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4efb\u52a1\u63d0\u4ea4\u4e0e\u7ed3\u679c\u83b7\u53d6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u63d0\u4ea4\u4efb\u52a1\u4e3b\u8981\u901a\u8fc7<code>submit<\/code>\u65b9\u6cd5\u3002<code>submit<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff08\u5982\u51fd\u6570\uff09\u548c\u5176\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u5305\u88c5\u4e3a\u4e00\u4e2a<code>Future<\/code>\u5bf9\u8c61\u3002<code>Future<\/code>\u5bf9\u8c61\u8868\u793a\u4e00\u4e2a\u5f02\u6b65\u6267\u884c\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u4efb\u52a1\u7684\u6267\u884c\u72b6\u6001\u548c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def task(n):<\/p>\n<p>    return n * n<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    # \u63d0\u4ea4\u4efb\u52a1<\/p>\n<p>    future1 = executor.submit(task, 2)<\/p>\n<p>    future2 = executor.submit(task, 3)<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c<\/p>\n<p>    result1 = future1.result()<\/p>\n<p>    result2 = future2.result()<\/p>\n<p>    print(f&quot;Result1: {result1}, Result2: {result2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684<code>task<\/code>\u51fd\u6570\uff0c\u5e76\u63d0\u4ea4\u4e86\u4e24\u4e2a\u4efb\u52a1\u3002\u4f7f\u7528<code>result<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u4efb\u52a1\u7684\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ebf\u7a0b\u6c60\u7684\u4f18\u52bf\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u52bf\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u63d0\u9ad8\u6027\u80fd<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u901a\u8fc7\u590d\u7528\u7ebf\u7a0b\u548c\u9650\u5236\u5e76\u53d1\u7ebf\u7a0b\u7684\u6570\u91cf\u6765\u51cf\u5c11\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/li>\n<li><strong>\u7b80\u5316\u5e76\u53d1\u7f16\u7a0b<\/strong>\uff1a<code>ThreadPoolExecutor<\/code>\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<li><strong>\u66f4\u597d\u7684<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/strong>\uff1a\u901a\u8fc7\u9650\u5236\u7ebf\u7a0b\u7684\u6570\u91cf\uff0c\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u7cfb\u7edf\u8d44\u6e90\uff0c\u907f\u514d\u8d44\u6e90\u8fc7\u5ea6\u6d88\u8017\u3002<\/li>\n<\/ol>\n<p><p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\uff1a\u5728\u7ebf\u7a0b\u6c60\u4e2d\u6267\u884c\u7684\u4efb\u52a1\u9700\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u786e\u4fdd\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\u4e0d\u4f1a\u4ea7\u751f\u7ade\u4e89\u6761\u4ef6\u3002<\/li>\n<li><strong>\u4efb\u52a1\u963b\u585e<\/strong>\uff1a\u5982\u679c\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u88ab\u957f\u65f6\u95f4\u963b\u585e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u6c60\u65e0\u6cd5\u5904\u7406\u65b0\u7684\u4efb\u52a1\u3002<\/li>\n<li><strong>\u6c60\u5927\u5c0f\u8bbe\u7f6e<\/strong>\uff1a\u6c60\u7684\u5927\u5c0f\u5e94\u6839\u636e\u4efb\u52a1\u7684\u7279\u6027\u548c\u7cfb\u7edf\u8d44\u6e90\u8fdb\u884c\u8c03\u6574\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u8fc7\u5ea6\u6d88\u8017\u6216\u7ebf\u7a0b\u95f2\u7f6e\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u7ebf\u7a0b\u6c60\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cfI\/O\u64cd\u4f5c\u7684\u4efb\u52a1\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u3002<\/li>\n<li><strong>\u5f02\u6b65\u4efb\u52a1\u5904\u7406<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u4efb\u52a1\uff0c\u5982\u5f02\u6b65\u6570\u636e\u5904\u7406\u3001\u5f02\u6b65\u65e5\u5fd7\u8bb0\u5f55\u7b49\u3002<\/li>\n<li><strong>\u591a\u4efb\u52a1\u5e76\u53d1\u6267\u884c<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u7528\u4e8e\u540c\u65f6\u6267\u884c\u591a\u4e2a\u72ec\u7acb\u7684\u4efb\u52a1\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u4e0e\u5176\u4ed6\u5e76\u53d1\u6a21\u578b\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u9664\u4e86<code>ThreadPoolExecutor<\/code>\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u5e76\u53d1\u6a21\u578b\uff0c\u5982<code>ProcessPoolExecutor<\/code>\u3001<code>asyncio<\/code>\u3001<code>multiprocessing<\/code>\u7b49\u3002\u6bcf\u79cd\u6a21\u578b\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<ul>\n<li><strong><code>ThreadPoolExecutor<\/code><\/strong>\uff1a\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f46\u7531\u4e8eGIL\u7684\u9650\u5236\uff0c\u4e0d\u80fd\u6709\u6548\u5229\u7528\u591a\u6838CPU\u3002<\/li>\n<li><strong><code>ProcessPoolExecutor<\/code><\/strong>\uff1a\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u5229\u7528\u591a\u6838CPU\uff0c\u4f46\u8fdb\u7a0b\u95f4\u901a\u4fe1\u5f00\u9500\u8f83\u5927\u3002<\/li>\n<li><strong><code>asyncio<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u5f02\u6b65I\/O\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\uff0c\u9002\u5408\u4e8e\u9700\u8981\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u3002<\/li>\n<li><strong><code>multiprocessing<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5e76\u884c\u5904\u7406\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u5229\u7528\u591a\u6838CPU\uff0c\u4f46\u7f16\u7a0b\u590d\u6742\u5ea6\u8f83\u9ad8\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u7ebf\u7a0b\u6c60\u7684\u8c03\u4f18\u4e0e\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u8c03\u4f18\u548c\u6027\u80fd\u4f18\u5316\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5408\u9002\u7684\u7ebf\u7a0b\u6c60\u5927\u5c0f<\/strong>\uff1a\u6839\u636e\u4efb\u52a1\u7684\u7279\u6027\u548c\u7cfb\u7edf\u8d44\u6e90\u8bbe\u7f6e\u5408\u9002\u7684\u7ebf\u7a0b\u6c60\u5927\u5c0f\u3002\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8f83\u5927\u7684\u7ebf\u7a0b\u6c60\uff1b\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8f83\u5c0f\u7684\u7ebf\u7a0b\u6c60\u3002<\/li>\n<li><strong>\u4efb\u52a1\u62c6\u5206\u4e0e\u5408\u5e76<\/strong>\uff1a\u5c06\u590d\u6742\u7684\u4efb\u52a1\u62c6\u5206\u4e3a\u591a\u4e2a\u5c0f\u4efb\u52a1\uff0c\u5e76\u4f7f\u7528\u7ebf\u7a0b\u6c60\u5e76\u53d1\u6267\u884c\uff1b\u540c\u65f6\uff0c\u5408\u5e76\u591a\u4e2a\u5c0f\u4efb\u52a1\u4ee5\u51cf\u5c11\u7ebf\u7a0b\u5207\u6362\u7684\u5f00\u9500\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6279\u91cf\u63d0\u4ea4<\/strong>\uff1a\u5982\u679c\u6709\u5927\u91cf\u4efb\u52a1\u9700\u8981\u63d0\u4ea4\uff0c\u53ef\u4ee5\u4f7f\u7528\u6279\u91cf\u63d0\u4ea4\u7684\u65b9\u5f0f\uff0c\u63d0\u9ad8\u4efb\u52a1\u63d0\u4ea4\u7684\u6548\u7387\u3002<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u6c60\u3002\u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5e76\u53d1\u7f16\u7a0b\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u8d44\u6e90\u7ba1\u7406\u80fd\u529b\u3002\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u7ebf\u7a0b\u6c60\u5927\u5c0f\uff0c\u5e76\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>with<\/code>\u8bed\u53e5\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u6c60\uff0c\u786e\u4fdd\u8d44\u6e90\u7684\u81ea\u52a8\u91ca\u653e\u3002<\/li>\n<li>\u6839\u636e\u4efb\u52a1\u7684\u7279\u6027\u548c\u7cfb\u7edf\u8d44\u6e90\u8bbe\u7f6e\u5408\u9002\u7684\u7ebf\u7a0b\u6c60\u5927\u5c0f\u3002<\/li>\n<li>\u786e\u4fdd\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4efb\u52a1\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528<code>submit<\/code>\u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\uff0c\u5e76\u901a\u8fc7<code>Future<\/code>\u5bf9\u8c61\u83b7\u53d6\u4efb\u52a1\u7684\u6267\u884c\u72b6\u6001\u548c\u7ed3\u679c\u3002<\/li>\n<li>\u5728\u7f16\u5199\u5e76\u53d1\u7a0b\u5e8f\u65f6\uff0c\u5145\u5206\u8003\u8651\u4efb\u52a1\u7684\u5e76\u53d1\u6027\u548c\u8d44\u6e90\u7684\u5408\u7406\u5229\u7528\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\uff0c\u4e3a\u5f00\u53d1\u9ad8\u6548\u7684Python\u5e94\u7528\u5960\u5b9a\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u4e2d\u7684<code>ThreadPoolExecutor<\/code>\u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u5b9e\u4f8b\u5316\u4e00\u4e2a<code>ThreadPoolExecutor<\/code>\u5bf9\u8c61\uff0c\u6307\u5b9a\u6700\u5927\u7ebf\u7a0b\u6570\u91cf\u3002\u4f7f\u7528<code>submit()<\/code>\u65b9\u6cd5\u5c06\u4efb\u52a1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u81ea\u52a8\u7ba1\u7406\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u4efb\u52a1\u7684\u8fd4\u56de\u7ed3\u679c\uff1f<\/strong><br \/>\u901a\u8fc7<code>ThreadPoolExecutor<\/code>\u63d0\u4ea4\u7684\u4efb\u52a1\u8fd4\u56de\u4e00\u4e2a<code>Future<\/code>\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>result()<\/code>\u65b9\u6cd5\u83b7\u53d6\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u3002\u5982\u679c\u4efb\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\uff0c\u8be5\u65b9\u6cd5\u4f1a\u91cd\u65b0\u629b\u51fa\u8be5\u5f02\u5e38\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u591a\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as_completed()<\/code>\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5141\u8bb8\u60a8\u5728\u4efb\u52a1\u5b8c\u6210\u65f6\u9010\u4e2a\u5904\u7406\u7ed3\u679c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5f02\u5e38\u548c\u8d85\u65f6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>timeout<\/code>\u53c2\u6570\u6765\u907f\u514d\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u8fc7\u957f\u7684\u95ee\u9898\u3002\u5982\u679c\u4efb\u52a1\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u672a\u5b8c\u6210\uff0c\u5c06\u629b\u51fa<code>concurrent.futures.TimeoutError<\/code>\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u5728\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\u65f6\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002\u5bf9\u4e8e\u9700\u8981\u91cd\u8bd5\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u8003\u8651\u5728\u6355\u83b7\u5f02\u5e38\u540e\u5b9e\u73b0\u91cd\u8bd5\u673a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528concurrent.futures\u6a21\u5757\u4e2d\u7684ThreadPoolExecutor [&hellip;]","protected":false},"author":3,"featured_media":1008563,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008558"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1008558"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008558\/revisions"}],"predecessor-version":[{"id":1008567,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008558\/revisions\/1008567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008563"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}