{"id":1132329,"date":"2025-01-08T20:56:06","date_gmt":"2025-01-08T12:56:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132329.html"},"modified":"2025-01-08T20:56:09","modified_gmt":"2025-01-08T12:56:09","slug":"python%e7%ba%bf%e7%a8%8b%e6%b1%a0%e5%a6%82%e4%bd%95%e5%8f%aa%e4%bc%a0%e4%b8%80%e7%bb%84%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132329.html","title":{"rendered":"python\u7ebf\u7a0b\u6c60\u5982\u4f55\u53ea\u4f20\u4e00\u7ec4\u53c2\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101903\/926a92de-6a32-4aac-a06c-a93d4743a90b.webp\" alt=\"python\u7ebf\u7a0b\u6c60\u5982\u4f55\u53ea\u4f20\u4e00\u7ec4\u53c2\u6570\" \/><\/p>\n<p><p> <strong>Python\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u4f20\u9012\u4e00\u7ec4\u53c2\u6570<\/strong>\u3002\u901a\u8fc7\u5c06\u53c2\u6570\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u6216\u5b57\u5178\uff0c\u53ef\u4ee5\u5c06\u5176\u4f5c\u4e3a\u5355\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u7ebf\u7a0b\u6c60\u3002<strong>\u4f7f\u7528<code>executor.submit()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>executor.map()<\/code>\u65b9\u6cd5<\/strong>\u662f\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>executor.submit()<\/code>\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1.1 \u7ebf\u7a0b\u6c60\u57fa\u7840\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u7ea7\u63a5\u53e3\u6765\u6267\u884c\u5e76\u53d1\u4efb\u52a1\u3002<code>ThreadPoolExecutor<\/code>\u662f\u5176\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u7ba1\u7406\u7ebf\u7a0b\u6c60\u3002\u7ebf\u7a0b\u6c60\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u4f7f\u7528<code>executor.submit()<\/code>\u4f20\u9012\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528<code>submit()<\/code>\u65b9\u6cd5\u6765\u63d0\u4ea4\u4efb\u52a1\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u4f46\u5982\u679c\u6211\u4eec\u60f3\u53ea\u4f20\u9012\u4e00\u7ec4\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06\u53c2\u6570\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u6216\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1.2.1 \u5b9a\u4e49\u4efb\u52a1\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u4efb\u52a1\u51fd\u6570\uff0c\u5b83\u5c06\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def task(params):<\/p>\n<p>    a, b = params<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2.2 \u521b\u5efa\u7ebf\u7a0b\u6c60\u5e76\u63d0\u4ea4\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a<code>ThreadPoolExecutor<\/code>\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528<code>submit()<\/code>\u65b9\u6cd5\u5c06\u4efb\u52a1\u548c\u53c2\u6570\u7ec4\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>params = (1, 2)<\/p>\n<p>with ThreadPoolExecutor(max_workers=2) as executor:<\/p>\n<p>    future = executor.submit(task, params)<\/p>\n<p>    result = future.result()<\/p>\n<p>    print(f&#39;Result: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u53c2\u6570\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u5355\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9<code>submit()<\/code>\u65b9\u6cd5\u3002\u7ebf\u7a0b\u6c60\u4f1a\u8fd0\u884c\u4efb\u52a1\u51fd\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h3>1.3 \u4f7f\u7528\u5b57\u5178\u4f20\u9012\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5143\u7ec4\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><h4>1.3.1 \u4fee\u6539\u4efb\u52a1\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4fee\u6539\u4efb\u52a1\u51fd\u6570\uff0c\u4f7f\u5176\u63a5\u6536\u4e00\u4e2a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def task(params):<\/p>\n<p>    a = params[&#39;a&#39;]<\/p>\n<p>    b = params[&#39;b&#39;]<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3.2 \u521b\u5efa\u7ebf\u7a0b\u6c60\u5e76\u63d0\u4ea4\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u521b\u5efa\u7ebf\u7a0b\u6c60\u5e76\u63d0\u4ea4\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">params = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>with ThreadPoolExecutor(max_workers=2) as executor:<\/p>\n<p>    future = executor.submit(task, params)<\/p>\n<p>    result = future.result()<\/p>\n<p>    print(f&#39;Result: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u4f20\u9012\u4e00\u7ec4\u53c2\u6570\uff0c\u5e76\u5728\u4efb\u52a1\u51fd\u6570\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>executor.map()<\/code>\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>2.1 \u57fa\u7840\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u65b9\u6cd5\u662f<code>ThreadPoolExecutor<\/code>\u7684\u53e6\u4e00\u79cd\u63d0\u4ea4\u4efb\u52a1\u7684\u65b9\u5f0f\u3002\u5b83\u4e0e<code>submit()<\/code>\u65b9\u6cd5\u4e0d\u540c\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u63d0\u4ea4\u591a\u4e2a\u4efb\u52a1\uff0c\u5e76\u5c06\u7ed3\u679c\u4f5c\u4e3a\u8fed\u4ee3\u5668\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u4f7f\u7528<code>executor.map()<\/code>\u4f20\u9012\u53c2\u6570<\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u53c2\u6570\u7ec4\u653e\u5165\u4e00\u4e2a\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\uff0c\u7136\u540e\u4f20\u9012\u7ed9<code>map()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>2.2.1 \u5b9a\u4e49\u4efb\u52a1\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u4efb\u52a1\u51fd\u6570\uff0c\u4e0e\u4e4b\u524d\u7c7b\u4f3c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def task(params):<\/p>\n<p>    a, b = params<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2.2 \u521b\u5efa\u7ebf\u7a0b\u6c60\u5e76\u63d0\u4ea4\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u7ebf\u7a0b\u6c60\uff0c\u5e76\u4f7f\u7528<code>map()<\/code>\u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">params_list = [(1, 2), (3, 4), (5, 6)]<\/p>\n<p>with ThreadPoolExecutor(max_workers=2) as executor:<\/p>\n<p>    results = executor.map(task, params_list)<\/p>\n<p>    for result in results:<\/p>\n<p>        print(f&#39;Result: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u591a\u4e2a\u53c2\u6570\u7ec4\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5e76\u5c06\u5217\u8868\u4f20\u9012\u7ed9<code>map()<\/code>\u65b9\u6cd5\u3002\u7ebf\u7a0b\u6c60\u4f1a\u5e76\u53d1\u5730\u8fd0\u884c\u4efb\u52a1\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>2.3 \u4f7f\u7528\u5b57\u5178\u4f20\u9012\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u548c<code>map()<\/code>\u65b9\u6cd5\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><h4>2.3.1 \u4fee\u6539\u4efb\u52a1\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4fee\u6539\u4efb\u52a1\u51fd\u6570\uff0c\u4f7f\u5176\u63a5\u6536\u4e00\u4e2a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def task(params):<\/p>\n<p>    a = params[&#39;a&#39;]<\/p>\n<p>    b = params[&#39;b&#39;]<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3.2 \u521b\u5efa\u7ebf\u7a0b\u6c60\u5e76\u63d0\u4ea4\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u7ebf\u7a0b\u6c60\uff0c\u5e76\u4f7f\u7528<code>map()<\/code>\u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">params_list = [{&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;a&#39;: 3, &#39;b&#39;: 4}, {&#39;a&#39;: 5, &#39;b&#39;: 6}]<\/p>\n<p>with ThreadPoolExecutor(max_workers=2) as executor:<\/p>\n<p>    results = executor.map(task, params_list)<\/p>\n<p>    for result in results:<\/p>\n<p>        print(f&#39;Result: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u548c<code>map()<\/code>\u65b9\u6cd5\u4f20\u9012\u591a\u4e2a\u53c2\u6570\u7ec4\uff0c\u5e76\u5e76\u53d1\u5730\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u7ebf\u7a0b\u6c60\u7684\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><h3>3.1 I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u975e\u5e38\u9002\u5408\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u3002\u8fd9\u4e9b\u4efb\u52a1\u5f80\u5f80\u4f1a\u56e0\u4e3a\u7b49\u5f85I\/O\u64cd\u4f5c\u800c\u963b\u585e\u7ebf\u7a0b\uff0c\u901a\u8fc7\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u5e76\u53d1\u5730\u6267\u884c\u591a\u4e2aI\/O\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def fetch_url(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    return response.status_code<\/p>\n<p>urls = [<\/p>\n<p>    &#39;https:\/\/www.example.com&#39;,<\/p>\n<p>    &#39;https:\/\/www.python.org&#39;,<\/p>\n<p>    &#39;https:\/\/www.github.com&#39;<\/p>\n<p>]<\/p>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    results = executor.map(fetch_url, urls)<\/p>\n<p>    for result in results:<\/p>\n<p>        print(f&#39;Status Code: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 CPU\u5bc6\u96c6\u578b\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u7ebf\u7a0b\u6c60\u66f4\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002\u7136\u800c\uff0c\u7531\u4e8ePython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\uff0cCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u901a\u5e38\u4e0d\u5982I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u8868\u73b0\u826f\u597d\u3002\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5efa\u8bae\u4f7f\u7528<code>concurrent.futures.ProcessPoolExecutor<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def compute_square(n):<\/p>\n<p>    return n * n<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    results = executor.map(compute_square, numbers)<\/p>\n<p>    for result in results:<\/p>\n<p>        print(f&#39;Square: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><h3>4.1 \u7ebf\u7a0b\u5b89\u5168<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u5982\u679c\u4efb\u52a1\u51fd\u6570\u4e2d\u6d89\u53ca\u5230\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\uff0c\u9700\u8981\u4f7f\u7528\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff08\u5982\u9501\uff09\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>shared_resource = 0<\/p>\n<p>def task(n):<\/p>\n<p>    global shared_resource<\/p>\n<p>    with lock:<\/p>\n<p>        shared_resource += n<\/p>\n<p>    return shared_resource<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    results = executor.map(task, numbers)<\/p>\n<p>    for result in results:<\/p>\n<p>        print(f&#39;Shared Resource: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u63d0\u4ea4\u4efb\u52a1\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f02\u5e38\u3002\u9700\u8981\u5728\u4efb\u52a1\u51fd\u6570\u4e2d\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u6216\u8005\u5728\u83b7\u53d6\u7ed3\u679c\u65f6\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def task(n):<\/p>\n<p>    try:<\/p>\n<p>        if n == 2:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&#39;An error occurred&#39;)<\/p>\n<p>        return n<\/p>\n<p>    except Exception as e:<\/p>\n<p>        return f&#39;Error: {e}&#39;<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    results = executor.map(task, numbers)<\/p>\n<p>    for result in results:<\/p>\n<p>        print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u548c\u6267\u884c\u5e76\u53d1\u4efb\u52a1\u3002<strong>\u4f7f\u7528<code>executor.submit()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>executor.map()<\/code>\u65b9\u6cd5<\/strong>\u662f\u4f20\u9012\u4e00\u7ec4\u53c2\u6570\u7684\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u3002\u7ebf\u7a0b\u6c60\u5728\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\u8868\u73b0\u5c24\u4e3a\u4f18\u5f02\uff0c\u4f46\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u7531\u4e8eGIL\u7684\u5b58\u5728\uff0c\u8868\u73b0\u53ef\u80fd\u4e0d\u5982\u9884\u671f\u3002\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u548c\u5f02\u5e38\u5904\u7406\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7ebf\u7a0b\u6c60\u4e2d\u4f20\u9012\u53c2\u6570\u7ed9\u76ee\u6807\u51fd\u6570\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u7684\u7ebf\u7a0b\u6c60\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>functools.partial<\/code>\u6216<code>lambda<\/code>\u8868\u8fbe\u5f0f\u6765\u4f20\u9012\u53c2\u6570\u3002\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u65f6\uff0c\u53ef\u4ee5\u5c06\u76ee\u6807\u51fd\u6570\u548c\u53c2\u6570\u7ec4\u5408\u8d77\u6765\uff0c\u65b9\u4fbf\u5730\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u4f20\u9012\u76f8\u540c\u7684\u53c2\u6570\u7ec4\u3002<\/p>\n<p><strong>Python\u7ebf\u7a0b\u6c60\u652f\u6301\u54ea\u4e9b\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\uff1f<\/strong><br \/>Python\u7684\u7ebf\u7a0b\u6c60\u652f\u6301\u591a\u79cd\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\uff0c\u5305\u62ec\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u3002\u53ef\u4ee5\u5728\u63d0\u4ea4\u4efb\u52a1\u65f6\uff0c\u901a\u8fc7<code>executor.submit(func, *args, &lt;strong&gt;kwargs)<\/code>\u7684\u5f62\u5f0f\u4f20\u9012\u6240\u9700\u7684\u53c2\u6570\u3002\u4f7f\u7528<code>*args<\/code>\u53ef\u4ee5\u4f20\u9012\u591a\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff0c\u800c<code>&lt;\/strong&gt;kwargs<\/code>\u5219\u53ef\u4ee5\u7528\u4e8e\u4f20\u9012\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7ebf\u7a0b\u6c60\u4e2d\u7684\u8fd4\u56de\u7ed3\u679c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>Future<\/code>\u5bf9\u8c61\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u7684\u8fd4\u56de\u7ed3\u679c\u3002\u5f53\u4f60\u63d0\u4ea4\u4efb\u52a1\u540e\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u8fd4\u56de\u4e00\u4e2a<code>Future<\/code>\u5bf9\u8c61\uff0c\u901a\u8fc7\u8c03\u7528<code>future.result()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u3002\u5982\u679c\u4efb\u52a1\u5931\u8d25\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u629b\u51fa\u5f02\u5e38\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528concurrent.futures.ThreadPoolExecutor\u4f20\u9012\u4e00 [&hellip;]","protected":false},"author":3,"featured_media":1132337,"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\/1132329"}],"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=1132329"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132329\/revisions"}],"predecessor-version":[{"id":1132340,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132329\/revisions\/1132340"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132337"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}