{"id":1020810,"date":"2024-12-27T13:15:48","date_gmt":"2024-12-27T05:15:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1020810.html"},"modified":"2024-12-27T13:15:53","modified_gmt":"2024-12-27T05:15:53","slug":"python-%e5%a6%82%e4%bd%95%e5%85%b3%e9%97%ad%e7%ba%bf%e7%a8%8b%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1020810.html","title":{"rendered":"python \u5982\u4f55\u5173\u95ed\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\/25163155\/62b29f19-151b-487b-a4e0-2a3ebe8164bc.webp\" alt=\"python \u5982\u4f55\u5173\u95ed\u7ebf\u7a0b\u6c60\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5173\u95ed\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u901a\u8fc7<strong>\u8c03\u7528\u7ebf\u7a0b\u6c60\u7684shutdown()\u65b9\u6cd5\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7ba1\u7406\u7ebf\u7a0b\u6c60\u7684\u751f\u547d\u5468\u671f\u3001\u786e\u4fdd\u6240\u6709\u4efb\u52a1\u90fd\u5df2\u7ecf\u5b8c\u6210<\/strong>\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5173\u95ed\u7ebf\u7a0b\u6c60\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u91ca\u653e\u8d44\u6e90\u5e76\u786e\u4fdd\u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa\u3002\u4ee5\u4e0b\u662f\u5bf9\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p><strong>\u8c03\u7528\u7ebf\u7a0b\u6c60\u7684shutdown()\u65b9\u6cd5<\/strong>\uff1aThreadPoolExecutor\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e2ashutdown()\u65b9\u6cd5\uff0c\u7528\u4e8e\u5173\u95ed\u7ebf\u7a0b\u6c60\u3002\u8c03\u7528\u8be5\u65b9\u6cd5\u540e\uff0c\u7ebf\u7a0b\u6c60\u5c06\u4e0d\u518d\u63a5\u53d7\u65b0\u7684\u4efb\u52a1\uff0c\u5f85\u6c60\u4e2d\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u540e\uff0c\u7ebf\u7a0b\u6c60\u5c06\u88ab\u5173\u95ed\u3002\u8c03\u7528shutdown()\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u662f\u5426\u7b49\u5f85\u6c60\u4e2d\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u3002\u4f20\u5165\u53c2\u6570w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t=True\u65f6\uff0cshutdown()\u4f1a\u963b\u585e\u8c03\u7528\uff0c\u76f4\u5230\u6240\u6709\u4efb\u52a1\u5b8c\u6210\uff1b\u4f20\u5165\u53c2\u6570wait=False\u65f6\uff0cshutdown()\u5c06\u7acb\u5373\u8fd4\u56de\uff0c\u4efb\u52a1\u4ecd\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u4e0d\u540c\u7684\u5173\u95ed\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7ebf\u7a0b\u6c60\u7684\u57fa\u7840\u77e5\u8bc6<\/h2>\n<\/p>\n<p><h3>1.1 \u7ebf\u7a0b\u6c60\u7684\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u7ebf\u7a0b\u7ba1\u7406\u6280\u672f\uff0c\u65e8\u5728\u89e3\u51b3\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u5e26\u6765\u7684\u6027\u80fd\u5f00\u9500\u3002\u901a\u8fc7\u91cd\u7528\u7ebf\u7a0b\uff0c\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002Python\u4e2d\u7684ThreadPoolExecutor\u662fconcurrent.futures\u6a21\u5757\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u548c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u7ebf\u7a0b\u6c60\u7684\u5de5\u4f5c\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u901a\u8fc7\u9884\u5148\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\uff0c\u5e76\u5c06\u5176\u653e\u5165\u6c60\u4e2d\u8fdb\u884c\u7ba1\u7406\u3002\u7a0b\u5e8f\u9700\u8981\u6267\u884c\u4efb\u52a1\u65f6\uff0c\u5c06\u4efb\u52a1\u63d0\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\uff0c\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7a7a\u95f2\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u6267\u884c\u8fd9\u4e9b\u4efb\u52a1\u3002\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7ebf\u7a0b\u4e0d\u88ab\u9500\u6bc1\uff0c\u800c\u662f\u8fd4\u56de\u6c60\u4e2d\u7b49\u5f85\u65b0\u7684\u4efb\u52a1\u3002\u8fd9\u79cd\u673a\u5236\u51cf\u5c11\u4e86\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u5f00\u9500\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5173\u95ed\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>2.1 \u8c03\u7528shutdown()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>ThreadPoolExecutor\u63d0\u4f9b\u4e86shutdown()\u65b9\u6cd5\u6765\u5173\u95ed\u7ebf\u7a0b\u6c60\u3002\u8c03\u7528shutdown()\u540e\uff0c\u7ebf\u7a0b\u6c60\u5c06\u4e0d\u518d\u63a5\u53d7\u65b0\u4efb\u52a1\uff0c\u4f46\u4f1a\u7ee7\u7eed\u6267\u884c\u5df2\u7ecf\u63d0\u4ea4\u7684\u4efb\u52a1\u3002shutdown()\u6709\u4e00\u4e2a\u53ef\u9009\u53c2\u6570wait\uff0c\u9ed8\u8ba4\u4e3aTrue\uff0c\u8868\u793a\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u540e\u518d\u5173\u95ed\u7ebf\u7a0b\u6c60\u3002\u8bbe\u7f6e\u4e3aFalse\u65f6\uff0cshutdown()\u4f1a\u7acb\u5373\u8fd4\u56de\uff0c\u4f46\u6c60\u4e2d\u7684\u4efb\u52a1\u4ecd\u4f1a\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def task(n):<\/p>\n<p>    print(f&quot;Task {n} is running&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=5)<\/p>\n<h2><strong>\u63d0\u4ea4\u4efb\u52a1<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    executor.submit(task, i)<\/p>\n<h2><strong>\u5173\u95ed\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor.shutdown(wait=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ba1\u7406\u5176\u751f\u547d\u5468\u671f\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u7ebf\u7a0b\u6c60\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u3002\u5728with\u8bed\u53e5\u5757\u4e2d\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u81ea\u52a8\u8c03\u7528shutdown()\u65b9\u6cd5\uff0c\u65e0\u9700\u663e\u5f0f\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def task(n):<\/p>\n<p>    print(f&quot;Task {n} is running&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    for i in range(10):<\/p>\n<p>        executor.submit(task, i)<\/p>\n<h2><strong>\u7ebf\u7a0b\u6c60\u5728\u6b64\u5904\u81ea\u52a8\u5173\u95ed<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3 \u786e\u4fdd\u6240\u6709\u4efb\u52a1\u5b8c\u6210<\/h3>\n<\/p>\n<p><p>\u5728\u5173\u95ed\u7ebf\u7a0b\u6c60\u4e4b\u524d\uff0c\u786e\u4fdd\u6240\u6709\u4efb\u52a1\u5df2\u7ecf\u5b8c\u6210\u662f\u4e00\u4e2a\u826f\u597d\u7684\u5b9e\u8df5\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528concurrent.futures\u6a21\u5757\u4e2d\u7684as_completed()\u6216wait()\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u76f4\u5230\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor, as_completed<\/p>\n<p>def task(n):<\/p>\n<p>    print(f&quot;Task {n} is running&quot;)<\/p>\n<p>    return n * 2<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=5)<\/p>\n<h2><strong>\u63d0\u4ea4\u4efb\u52a1\u5e76\u83b7\u53d6\u671f\u7269\u5bf9\u8c61<\/strong><\/h2>\n<p>futures = [executor.submit(task, i) for i in range(10)]<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210<\/strong><\/h2>\n<p>for future in as_completed(futures):<\/p>\n<p>    print(f&quot;Result: {future.result()}&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor.shutdown(wait=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u7ebf\u7a0b\u6c60\u5173\u95ed\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><h3>3.1 \u907f\u514d\u6b7b\u9501<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u8981\u6ce8\u610f\u907f\u514d\u6b7b\u9501\u7684\u53d1\u751f\u3002\u6b7b\u9501\u901a\u5e38\u7531\u4e8e\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u800c\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u3002\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u786e\u4fdd\u4efb\u52a1\u4e4b\u95f4\u6ca1\u6709\u76f8\u4e92\u4f9d\u8d56\uff0c\u907f\u514d\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>3.2 \u5904\u7406\u672a\u5b8c\u6210\u7684\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u5728\u5173\u95ed\u7ebf\u7a0b\u6c60\u4e4b\u524d\uff0c\u5904\u7406\u672a\u5b8c\u6210\u7684\u4efb\u52a1\u662f\u5fc5\u8981\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u5904\u7406\u4efb\u52a1\u6267\u884c\u4e2d\u7684\u9519\u8bef\uff0c\u6216\u8005\u8bbe\u7f6e\u5408\u7406\u7684\u8d85\u65f6\u673a\u5236\uff0c\u4ee5\u9632\u6b62\u4efb\u52a1\u65e0\u9650\u671f\u963b\u585e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor, TimeoutError<\/p>\n<p>def task(n):<\/p>\n<p>    if n == 5:<\/p>\n<p>        raise ValueError(&quot;An error occurred in task 5&quot;)<\/p>\n<p>    return n * 2<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=5)<\/p>\n<h2><strong>\u63d0\u4ea4\u4efb\u52a1\u5e76\u83b7\u53d6\u671f\u7269\u5bf9\u8c61<\/strong><\/h2>\n<p>futures = [executor.submit(task, i) for i in range(10)]<\/p>\n<h2><strong>\u5904\u7406\u4efb\u52a1\u7ed3\u679c<\/strong><\/h2>\n<p>for future in futures:<\/p>\n<p>    try:<\/p>\n<p>        result = future.result(timeout=2)<\/p>\n<p>        print(f&quot;Result: {result}&quot;)<\/p>\n<p>    except TimeoutError:<\/p>\n<p>        print(&quot;Task timed out&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Task raised an exception: {e}&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor.shutdown(wait=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u7ebf\u7a0b\u6c60\u7684\u9ad8\u7ea7\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>4.1 \u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6c60\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u8d1f\u8f7d\u53ef\u80fd\u4f1a\u52a8\u6001\u53d8\u5316\uff0c\u6b64\u65f6\u53ef\u4ee5\u8003\u8651\u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\u3002\u867d\u7136ThreadPoolExecutor\u4e0d\u76f4\u63a5\u652f\u6301\u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u521b\u5efa\u7ebf\u7a0b\u6c60\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>import time<\/p>\n<p>def task(n):<\/p>\n<p>    time.sleep(1)<\/p>\n<p>    print(f&quot;Task {n} completed&quot;)<\/p>\n<p>    return n * 2<\/p>\n<h2><strong>\u521d\u59cb\u7ebf\u7a0b\u6c60\u5927\u5c0f<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=5)<\/p>\n<h2><strong>\u63d0\u4ea4\u4efb\u52a1<\/strong><\/h2>\n<p>futures = [executor.submit(task, i) for i in range(10)]<\/p>\n<h2><strong>\u5173\u95ed\u521d\u59cb\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor.shutdown(wait=True)<\/p>\n<h2><strong>\u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6c60\u5927\u5c0f<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=10)<\/p>\n<h2><strong>\u63d0\u4ea4\u65b0\u4efb\u52a1<\/strong><\/h2>\n<p>futures.extend([executor.submit(task, i) for i in range(10, 20)])<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210<\/strong><\/h2>\n<p>for future in futures:<\/p>\n<p>    print(f&quot;Result: {future.result()}&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor.shutdown(wait=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u4efb\u52a1\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>ThreadPoolExecutor\u5141\u8bb8\u4e3a\u6bcf\u4e2a\u4efb\u52a1\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728\u4efb\u52a1\u5b8c\u6210\u65f6\u5904\u7406\u5176\u7ed3\u679c\u3002\u56de\u8c03\u51fd\u6570\u5728\u4efb\u52a1\u5b8c\u6210\u540e\u88ab\u8c03\u7528\uff0c\u5e76\u63a5\u6536\u671f\u7269\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002\u901a\u8fc7\u56de\u8c03\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u5904\u7406\u4efb\u52a1\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def task(n):<\/p>\n<p>    return n * 2<\/p>\n<p>def callback(future):<\/p>\n<p>    print(f&quot;Task result: {future.result()}&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=5)<\/p>\n<h2><strong>\u63d0\u4ea4\u4efb\u52a1\u5e76\u8bbe\u7f6e\u56de\u8c03<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    future = executor.submit(task, i)<\/p>\n<p>    future.add_done_callback(callback)<\/p>\n<h2><strong>\u5173\u95ed\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor.shutdown(wait=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u7ebf\u7a0b\u6c60\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><h3>5.1 \u5408\u7406\u9009\u62e9\u7ebf\u7a0b\u6c60\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u7406\u7684\u7ebf\u7a0b\u6c60\u5927\u5c0f\u5bf9\u7a0b\u5e8f\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u7ebf\u7a0b\u6c60\u5927\u5c0f\u901a\u5e38\u53d6\u51b3\u4e8e\u4efb\u52a1\u7684\u6027\u8d28\u548c\u7cfb\u7edf\u8d44\u6e90\u3002\u5bf9\u4e8eIO\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u9009\u62e9\u8f83\u5927\u7684\u7ebf\u7a0b\u6c60\uff0c\u4ee5\u5145\u5206\u5229\u7528\u7cfb\u7edf\u7684\u5e76\u53d1\u80fd\u529b\u3002\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u7ebf\u7a0b\u6c60\u5927\u5c0f\u53ef\u4ee5\u63a5\u8fd1\u7cfb\u7edfCPU\u6838\u5fc3\u6570\uff0c\u4ee5\u907f\u514d\u7ebf\u7a0b\u95f4\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h3>5.2 \u76d1\u63a7\u548c\u8c03\u8bd5\u7ebf\u7a0b\u6c60<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u76d1\u63a7\u7ebf\u7a0b\u6c60\u7684\u72b6\u6001\u548c\u6027\u80fd\u662f\u5fc5\u8981\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u5206\u6790\u5de5\u5177\u7b49\u624b\u6bb5\u6765\u76d1\u63a7\u7ebf\u7a0b\u6c60\u7684\u6267\u884c\u60c5\u51b5\u3002\u5f53\u7a0b\u5e8f\u51fa\u73b0\u6027\u80fd\u74f6\u9888\u6216\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u8bd5\u5de5\u5177\u5206\u6790\u7ebf\u7a0b\u6c60\u7684\u72b6\u6001\uff0c\u4ece\u800c\u627e\u51fa\u95ee\u9898\u6240\u5728\u5e76\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>5.3 \u5904\u7406\u5f02\u5e38\u548c\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u5904\u7406\u4efb\u52a1\u6267\u884c\u4e2d\u7684\u5f02\u5e38\u548c\u9519\u8bef\u662f\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u671f\u7269\u5bf9\u8c61\u7684exception()\u65b9\u6cd5\u83b7\u53d6\u4efb\u52a1\u6267\u884c\u4e2d\u7684\u5f02\u5e38\u4fe1\u606f\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002\u5bf9\u4e8e\u4e0d\u53ef\u6062\u590d\u7684\u9519\u8bef\uff0c\u53ef\u4ee5\u9009\u62e9\u7ec8\u6b62\u7a0b\u5e8f\u6216\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def task(n):<\/p>\n<p>    if n == 5:<\/p>\n<p>        raise ValueError(&quot;An error occurred in task 5&quot;)<\/p>\n<p>    return n * 2<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=5)<\/p>\n<h2><strong>\u63d0\u4ea4\u4efb\u52a1\u5e76\u83b7\u53d6\u671f\u7269\u5bf9\u8c61<\/strong><\/h2>\n<p>futures = [executor.submit(task, i) for i in range(10)]<\/p>\n<h2><strong>\u5904\u7406\u4efb\u52a1\u7ed3\u679c<\/strong><\/h2>\n<p>for future in futures:<\/p>\n<p>    if future.exception():<\/p>\n<p>        print(f&quot;Task raised an exception: {future.exception()}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Result: {future.result()}&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>executor.shutdown(wait=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u548c\u7ba1\u7406\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u548c\u8d44\u6e90\u5229\u7528\u6548\u7387\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u80fd\u4e3a\u60a8\u5728\u4f7f\u7528Python\u7ebf\u7a0b\u6c60\u65f6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u7ebf\u7a0b\u6c60\u5173\u95ed\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7ebf\u7a0b\u6c60\u7684\u5173\u95ed\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>shutdown()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u6b64\u65b9\u6cd5\u4f1a\u963b\u6b62\u65b0\u7684\u4efb\u52a1\u88ab\u63d0\u4ea4\uff0c\u5e76\u7b49\u5f85\u5df2\u63d0\u4ea4\u7684\u4efb\u52a1\u5b8c\u6210\u3002\u53ef\u4ee5\u9009\u62e9\u5728\u5173\u95ed\u65f6\u4f7f\u7528<code>wait=True<\/code>\u53c2\u6570\uff0c\u8fd9\u6837\u4f1a\u963b\u585e\u76f4\u5230\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\u3002\u5982\u679c\u9700\u8981\u7acb\u5373\u5173\u95ed\u800c\u4e0d\u7b49\u5f85\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutdown(wait=False)<\/code>\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u7ebf\u7a0b\u6c60\u4e2d\uff0c\u5f02\u5e38\u4f1a\u88ab\u6355\u83b7\u5e76\u5b58\u50a8\u5728Future\u5bf9\u8c61\u4e2d\u3002\u901a\u8fc7\u8c03\u7528Future\u5bf9\u8c61\u7684<code>result()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u4efb\u52a1\u7684\u8fd4\u56de\u503c\u6216\u629b\u51fa\u7684\u5f02\u5e38\u3002\u5982\u679c\u5728\u4efb\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u5f02\u5e38\uff0c<code>result()<\/code>\u65b9\u6cd5\u4f1a\u91cd\u65b0\u629b\u51fa\u8be5\u5f02\u5e38\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u8fdb\u884c\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p><strong>\u5173\u95ed\u7ebf\u7a0b\u6c60\u540e\u662f\u5426\u53ef\u4ee5\u91cd\u65b0\u542f\u52a8\uff1f<\/strong><br \/>\u7ebf\u7a0b\u6c60\u5173\u95ed\u540e\uff0c\u65e0\u6cd5\u91cd\u65b0\u542f\u52a8\u3002\u5982\u679c\u9700\u8981\u518d\u6b21\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff0c\u5fc5\u987b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u6c60\u5b9e\u4f8b\u3002\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u5efa\u8bae\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u5185\u5408\u7406\u7ba1\u7406\u7ebf\u7a0b\u6c60\u7684\u521b\u5efa\u548c\u5173\u95ed\uff0c\u4ee5\u907f\u514d\u9891\u7e41\u7684\u8d44\u6e90\u5f00\u9500\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5173\u95ed\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7ebf\u7a0b\u6c60\u7684shutdown()\u65b9\u6cd5\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7ba1\u7406\u7ebf\u7a0b\u6c60\u7684\u751f\u547d\u5468\u671f [&hellip;]","protected":false},"author":3,"featured_media":1020820,"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\/1020810"}],"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=1020810"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020810\/revisions"}],"predecessor-version":[{"id":1020821,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020810\/revisions\/1020821"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1020820"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1020810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1020810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1020810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}