{"id":1114517,"date":"2025-01-08T17:58:06","date_gmt":"2025-01-08T09:58:06","guid":{"rendered":""},"modified":"2025-01-08T17:58:10","modified_gmt":"2025-01-08T09:58:10","slug":"python-%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e8%bf%90%e8%a1%8c%e4%b8%a4%e4%b8%aa%e5%88%86%e6%94%af-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1114517.html","title":{"rendered":"Python \u5982\u4f55\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5206\u652f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075639\/754d0cfc-a9b6-46f5-9670-0d185d4296c9.webp\" alt=\"Python \u5982\u4f55\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5206\u652f\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d: <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5206\u652f\u3001\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u548c<code>multiprocessing<\/code>\u6a21\u5757\u3001\u5f02\u6b65\u7f16\u7a0b\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0c<code>threading<\/code>\u6a21\u5757\u7528\u4e8e\u591a\u7ebf\u7a0b\u7f16\u7a0b\uff0c\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff1b<code>multiprocessing<\/code>\u6a21\u5757\u7528\u4e8e\u591a\u8fdb\u7a0b\u7f16\u7a0b\uff0c\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<code>asyncio<\/code>\u6a21\u5757\u5219\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u4e00\u79cd\u5b9e\u73b0\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u5e76\u53d1\u7684I\/O\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001THREADING \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>threading<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u6a21\u5757\u3002\u7ebf\u7a0b\u662f\u6700\u5c0f\u7684\u6267\u884c\u5355\u5143\uff0c\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u3002\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u5168\u5c40\u53d8\u91cf\u548c\u8d44\u6e90\uff0c\u8fd9\u4f7f\u5f97\u7ebf\u7a0b\u95f4\u901a\u4fe1\u53d8\u5f97\u5bb9\u6613\uff0c\u4f46\u4e5f\u5e26\u6765\u4e86\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u521b\u5efa\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading.Thread<\/code>\u7c7b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u548c\u542f\u52a8\u4e24\u4e2a\u7ebf\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def task1():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(&quot;Task 1 - Iteration&quot;, i)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def task2():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(&quot;Task 2 - Iteration&quot;, i)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61<\/strong><\/h2>\n<p>thread1 = threading.Thread(target=task1)<\/p>\n<p>thread2 = threading.Thread(target=task2)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p>print(&quot;Both threads have finished their tasks.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>task1<\/code>\u548c<code>task2<\/code>\u51fd\u6570\u5206\u522b\u4ee3\u8868\u4e24\u4e2a\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u7ebf\u7a0b<code>thread1<\/code>\u548c<code>thread2<\/code>\uff0c\u5e76\u5206\u522b\u5c06\u8fd9\u4e24\u4e2a\u4efb\u52a1\u5206\u914d\u7ed9\u5b83\u4eec\u3002\u901a\u8fc7\u8c03\u7528<code>start()<\/code>\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\uff0c\u5e76\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u7b49\u5f85\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u7ebf\u7a0b\u9501<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u5168\u5c40\u53d8\u91cf\u548c\u8d44\u6e90\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u7ebf\u7a0b\u9501\u6765\u907f\u514d\u8d44\u6e90\u7ade\u4e89\u3002Python\u4e2d\u7684<code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>Lock<\/code>\u7c7b\u6765\u5b9e\u73b0\u7ebf\u7a0b\u9501\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<h2><strong>\u521b\u5efa\u9501\u5bf9\u8c61<\/strong><\/h2>\n<p>lock = threading.Lock()<\/p>\n<h2><strong>\u5171\u4eab\u8d44\u6e90<\/strong><\/h2>\n<p>shared_counter = 0<\/p>\n<p>def increment_counter():<\/p>\n<p>    global shared_counter<\/p>\n<p>    with lock:  # \u83b7\u53d6\u9501<\/p>\n<p>        for _ in range(100000):<\/p>\n<p>            shared_counter += 1<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for _ in range(2):<\/p>\n<p>    thread = threading.Thread(target=increment_counter)<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>print(&quot;Final counter value:&quot;, shared_counter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>lock<\/code>\u5bf9\u8c61\u6765\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539<code>shared_counter<\/code>\u53d8\u91cf\uff0c\u4ece\u800c\u907f\u514d\u4e86\u8d44\u6e90\u7ade\u4e89\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001MULTIPROCESSING \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u5b9e\u73b0\u591a\u8fdb\u7a0b\u7f16\u7a0b\u7684\u6a21\u5757\u3002\u8fdb\u7a0b\u662f\u72ec\u7acb\u8fd0\u884c\u7684\u7a0b\u5e8f\u5b9e\u4f8b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u8d44\u6e90\u3002\u591a\u8fdb\u7a0b\u7f16\u7a0b\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u80fd\u5229\u7528\u591a\u6838CPU\u7684\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u521b\u5efa\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>multiprocessing.Process<\/code>\u7c7b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u548c\u542f\u52a8\u4e24\u4e2a\u8fdb\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def task1():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(&quot;Task 1 - Iteration&quot;, i)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def task2():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(&quot;Task 2 - Iteration&quot;, i)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b\u5bf9\u8c61<\/strong><\/h2>\n<p>process1 = multiprocessing.Process(target=task1)<\/p>\n<p>process2 = multiprocessing.Process(target=task2)<\/p>\n<h2><strong>\u542f\u52a8\u8fdb\u7a0b<\/strong><\/h2>\n<p>process1.start()<\/p>\n<p>process2.start()<\/p>\n<h2><strong>\u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>process1.join()<\/p>\n<p>process2.join()<\/p>\n<p>print(&quot;Both processes have finished their tasks.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>task1<\/code>\u548c<code>task2<\/code>\u51fd\u6570\u5206\u522b\u4ee3\u8868\u4e24\u4e2a\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u8fdb\u7a0b<code>process1<\/code>\u548c<code>process2<\/code>\uff0c\u5e76\u5206\u522b\u5c06\u8fd9\u4e24\u4e2a\u4efb\u52a1\u5206\u914d\u7ed9\u5b83\u4eec\u3002\u901a\u8fc7\u8c03\u7528<code>start()<\/code>\u65b9\u6cd5\u542f\u52a8\u8fdb\u7a0b\uff0c\u5e76\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u7b49\u5f85\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u673a\u5236\u6765\u5171\u4eab\u6570\u636e\u3002Python\u4e2d\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cdIPC\u673a\u5236\uff0c\u4f8b\u5982<code>Queue<\/code>\u3001<code>Pipe<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def producer(queue):<\/p>\n<p>    for i in range(5):<\/p>\n<p>        queue.put(i)<\/p>\n<p>        print(&quot;Produced&quot;, i)<\/p>\n<p>def consumer(queue):<\/p>\n<p>    while True:<\/p>\n<p>        item = queue.get()<\/p>\n<p>        if item is None:<\/p>\n<p>            break<\/p>\n<p>        print(&quot;Consumed&quot;, item)<\/p>\n<h2><strong>\u521b\u5efa\u961f\u5217\u5bf9\u8c61<\/strong><\/h2>\n<p>queue = multiprocessing.Queue()<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b\u5bf9\u8c61<\/strong><\/h2>\n<p>producer_process = multiprocessing.Process(target=producer, args=(queue,))<\/p>\n<p>consumer_process = multiprocessing.Process(target=consumer, args=(queue,))<\/p>\n<h2><strong>\u542f\u52a8\u8fdb\u7a0b<\/strong><\/h2>\n<p>producer_process.start()<\/p>\n<p>consumer_process.start()<\/p>\n<h2><strong>\u7b49\u5f85\u751f\u4ea7\u8005\u5b8c\u6210<\/strong><\/h2>\n<p>producer_process.join()<\/p>\n<h2><strong>\u53d1\u9001\u7ec8\u6b62\u4fe1\u53f7<\/strong><\/h2>\n<p>queue.put(None)<\/p>\n<h2><strong>\u7b49\u5f85\u6d88\u8d39\u8005\u5b8c\u6210<\/strong><\/h2>\n<p>consumer_process.join()<\/p>\n<p>print(&quot;Both processes have finished their tasks.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Queue<\/code>\u5bf9\u8c61\u5728\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u751f\u4ea7\u8005\u8fdb\u7a0b\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\u4e2d\uff0c\u6d88\u8d39\u8005\u8fdb\u7a0b\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001ASYNCIO \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>asyncio<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u7684\u6a21\u5757\u3002\u5f02\u6b65\u7f16\u7a0b\u662f\u4e00\u79cd\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf\u5e76\u53d1\u7684I\/O\u64cd\u4f5c\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u521b\u5efa\u5f02\u6b65\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528<code>asyncio.create_task<\/code>\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u548c\u8fd0\u884c\u4e24\u4e2a\u5f02\u6b65\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def task1():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(&quot;Task 1 - Iteration&quot;, i)<\/p>\n<p>        aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t asyncio.sleep(1)<\/p>\n<p>async def task2():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(&quot;Task 2 - Iteration&quot;, i)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>async def main():<\/p>\n<p>    # \u521b\u5efa\u5f02\u6b65\u4efb\u52a1<\/p>\n<p>    task1_coro = asyncio.create_task(task1())<\/p>\n<p>    task2_coro = asyncio.create_task(task2())<\/p>\n<p>    # \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210<\/p>\n<p>    await task1_coro<\/p>\n<p>    await task2_coro<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u51fd\u6570<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p>print(&quot;Both tasks have finished.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>task1<\/code>\u548c<code>task2<\/code>\u51fd\u6570\u5206\u522b\u4ee3\u8868\u4e24\u4e2a\u4e0d\u540c\u7684\u5f02\u6b65\u4efb\u52a1\u3002\u6211\u4eec\u4f7f\u7528<code>asyncio.create_task<\/code>\u51fd\u6570\u521b\u5efa\u5f02\u6b65\u4efb\u52a1\uff0c\u5e76\u4f7f\u7528<code>await<\/code>\u5173\u952e\u5b57\u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>3.2 \u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u8d44\u6e90\u7684\u7ba1\u7406\u4e5f\u9700\u8981\u4f7f\u7528\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002Python\u4e2d\u7684<code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>async with<\/code>\u8bed\u6cd5\u6765\u5b9e\u73b0\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>class AsyncResource:<\/p>\n<p>    async def __aenter__(self):<\/p>\n<p>        print(&quot;Entering context&quot;)<\/p>\n<p>        return self<\/p>\n<p>    async def __aexit__(self, exc_type, exc, tb):<\/p>\n<p>        print(&quot;Exiting context&quot;)<\/p>\n<p>    async def do_something(self):<\/p>\n<p>        print(&quot;Doing something&quot;)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>async def main():<\/p>\n<p>    async with AsyncResource() as resource:<\/p>\n<p>        await resource.do_something()<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u51fd\u6570<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>AsyncResource<\/code>\u7c7b\u5b9e\u73b0\u4e86\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u63a5\u53e3\uff0c\u5373<code>__aenter__<\/code>\u548c<code>__aexit__<\/code>\u65b9\u6cd5\u3002\u6211\u4eec\u4f7f\u7528<code>async with<\/code>\u8bed\u6cd5\u6765\u7ba1\u7406\u8d44\u6e90\u7684\u8fdb\u5165\u548c\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5206\u652f<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u591a\u7ebf\u7a0b\u7f16\u7a0b\uff08<code>threading<\/code>\u6a21\u5757\uff09<\/strong>\uff1a\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u5bb9\u6613\uff0c\u4f46\u9700\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/li>\n<li><strong>\u591a\u8fdb\u7a0b\u7f16\u7a0b\uff08<code>multiprocessing<\/code>\u6a21\u5757\uff09<\/strong>\uff1a\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u9700\u8981IPC\u673a\u5236\uff0c\u8fdb\u7a0b\u9694\u79bb\u6027\u597d\u3002<\/li>\n<li><strong>\u5f02\u6b65\u7f16\u7a0b\uff08<code>asyncio<\/code>\u6a21\u5757\uff09<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u91cf\u5e76\u53d1\u7684I\/O\u64cd\u4f5c\uff0c\u4f7f\u7528\u5f02\u6b65\u4efb\u52a1\u548c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ba1\u7406\u8d44\u6e90\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u4efb\u52a1\u7684\u7279\u6027\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u548c\u54cd\u5e94\u80fd\u529b\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5e76\u884c\u5904\u7406\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5b9e\u73b0\u5e76\u884c\u5904\u7406\u3002\u591a\u7ebf\u7a0b\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u800c\u591a\u8fdb\u7a0b\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u7ebf\u7a0b\uff0c\u6216\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u6765\u521b\u5efa\u8fdb\u7a0b\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u51fd\u6570\u6709\u4ec0\u4e48\u6280\u5de7\u5417\uff1f<\/strong><br \/>\u8981\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>threading.Thread<\/code>\u521b\u5efa\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u5206\u522b\u6267\u884c\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u4e2d\u7684<code>ThreadPoolExecutor<\/code>\u6216<code>ProcessPoolExecutor<\/code>\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u63a5\u53e3\u6765\u7ba1\u7406\u5e76\u53d1\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u5e76\u884c\u4efb\u52a1\u7684\u8fd4\u56de\u503c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u961f\u5217\u6216\u4f7f\u7528<code>Future<\/code>\u5bf9\u8c61\u6765\u83b7\u53d6\u8fd4\u56de\u503c\u3002\u5982\u679c\u4f7f\u7528<code>concurrent.futures<\/code>\uff0c\u53ef\u4ee5\u8c03\u7528<code>submit<\/code>\u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\uff0c\u5e76\u901a\u8fc7<code>result()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u7684\u8fd4\u56de\u503c\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u5e76\u884c\u8ba1\u7b97\u7684\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: Python\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5206\u652f\u3001\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528thr [&hellip;]","protected":false},"author":3,"featured_media":1114525,"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\/1114517"}],"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=1114517"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114517\/revisions"}],"predecessor-version":[{"id":1114526,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114517\/revisions\/1114526"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114525"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}