{"id":1096256,"date":"2025-01-08T14:59:28","date_gmt":"2025-01-08T06:59:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096256.html"},"modified":"2025-01-08T14:59:30","modified_gmt":"2025-01-08T06:59:30","slug":"python%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e8%b7%91%e4%b8%a4%e4%b8%aa%e5%be%aa%e7%8e%af-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096256.html","title":{"rendered":"python\u5982\u4f55\u540c\u65f6\u8dd1\u4e24\u4e2a\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211536\/092a4b3b-9095-408e-b830-40ad46872525.webp\" alt=\"python\u5982\u4f55\u540c\u65f6\u8dd1\u4e24\u4e2a\u5faa\u73af\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8981\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5b9e\u73b0\u3002\u8fd9\u662f\u56e0\u4e3aPython\u672c\u8eab\u7684\u7ebf\u7a0b\uff08Threading\uff09\u548c\u8fdb\u7a0b\uff08Multiprocessing\uff09\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5e76\u884c\u5730\u8fd0\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7ebf\u7a0b\u3001\u4f7f\u7528\u8fdb\u7a0b\u3001\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/strong>\uff0c\u5176\u4e2d\u4f7f\u7528\u7ebf\u7a0b\u662f\u6700\u5e38\u89c1\u4e14\u6700\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u4e0b\u9762\u8be6\u7ec6\u89e3\u91ca\u4f7f\u7528\u7ebf\u7a0b\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0c\u4ece\u800c\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def loop1():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Loop 1 running&quot;)<\/p>\n<p>        time.sleep(1)  # \u6a21\u62df\u67d0\u4e9b\u5de5\u4f5c<\/p>\n<p>def loop2():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Loop 2 running&quot;)<\/p>\n<p>        time.sleep(1)  # \u6a21\u62df\u67d0\u4e9b\u5de5\u4f5c<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = threading.Thread(target=loop1)<\/p>\n<p>thread2 = threading.Thread(target=loop2)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5b50\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570<code>loop1<\/code>\u548c<code>loop2<\/code>\uff0c\u6bcf\u4e2a\u51fd\u6570\u5185\u90fd\u6709\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u6211\u4eec\u901a\u8fc7<code>threading.Thread<\/code>\u521b\u5efa\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5206\u522b\u8fd0\u884c\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u542f\u52a8\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u8fdb\u7a0b\u6765\u5b9e\u73b0\u5e76\u884c\u8fd0\u884c\u591a\u4e2a\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def loop1():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Loop 1 running&quot;)<\/p>\n<p>        time.sleep(1)  # \u6a21\u62df\u67d0\u4e9b\u5de5\u4f5c<\/p>\n<p>def loop2():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Loop 2 running&quot;)<\/p>\n<p>        time.sleep(1)  # \u6a21\u62df\u67d0\u4e9b\u5de5\u4f5c<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b<\/strong><\/h2>\n<p>process1 = multiprocessing.Process(target=loop1)<\/p>\n<p>process2 = multiprocessing.Process(target=loop2)<\/p>\n<h2><strong>\u542f\u52a8\u8fdb\u7a0b<\/strong><\/h2>\n<p>process1.start()<\/p>\n<p>process2.start()<\/p>\n<h2><strong>\u4e3b\u8fdb\u7a0b\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>process1.join()<\/p>\n<p>process2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570<code>loop1<\/code>\u548c<code>loop2<\/code>\uff0c\u6bcf\u4e2a\u51fd\u6570\u5185\u90fd\u6709\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u6211\u4eec\u901a\u8fc7<code>multiprocessing.Process<\/code>\u521b\u5efa\u4e24\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u5206\u522b\u8fd0\u884c\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u542f\u52a8\u8fd9\u4e24\u4e2a\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5e76\u884c\u8fd0\u884c\u591a\u4e2a\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def loop1():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Loop 1 running&quot;)<\/p>\n<p>        aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t asyncio.sleep(1)  # \u6a21\u62df\u67d0\u4e9b\u5de5\u4f5c<\/p>\n<p>async def loop2():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Loop 2 running&quot;)<\/p>\n<p>        await asyncio.sleep(1)  # \u6a21\u62df\u67d0\u4e9b\u5de5\u4f5c<\/p>\n<p>async def main():<\/p>\n<p>    await asyncio.gather(loop1(), loop2())<\/p>\n<h2><strong>\u8fd0\u884c\u5f02\u6b65\u4efb\u52a1<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5f02\u6b65\u51fd\u6570<code>loop1<\/code>\u548c<code>loop2<\/code>\uff0c\u6bcf\u4e2a\u51fd\u6570\u5185\u90fd\u6709\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u6211\u4eec\u901a\u8fc7<code>asyncio.gather<\/code>\u5e76\u884c\u8fd0\u884c\u8fd9\u4e24\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u7136\u540e\u901a\u8fc7<code>asyncio.run<\/code>\u542f\u52a8\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7684\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p><strong>\u591a\u7ebf\u7a0b<\/strong>\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u6bd4\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u4efb\u52a1\u7684\u74f6\u9888\u5728\u4e8eI\/O\u64cd\u4f5c\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u800c\u4e0d\u662fCPU\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p><strong>\u591a\u8fdb\u7a0b<\/strong>\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u6bd4\u5982\u8ba1\u7b97\u5bc6\u96c6\u7684\u4efb\u52a1\uff0c\u56e0\u4e3aPython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u4f1a\u9650\u5236\u591a\u7ebf\u7a0b\u7684\u5e76\u884c\u6267\u884c\uff0c\u800c\u591a\u8fdb\u7a0b\u53ef\u4ee5\u7ed5\u8fc7\u8fd9\u4e2a\u9650\u5236\u3002<\/p>\n<\/p>\n<p><p><strong>\u5f02\u6b65\u7f16\u7a0b<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u5e76\u53d1I\/O\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u6bd4\u5982\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><p>\u6839\u636e\u4efb\u52a1\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e76\u884c\u6267\u884c\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5229\u7528\u7cfb\u7edf\u8d44\u6e90\u3002\u65e0\u8bba\u662f\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u8fd8\u662f\u5f02\u6b65\u7f16\u7a0b\uff0c\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0Python\u4e2d\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\u591a\u7ebf\u7a0b\u4e2d\u7684\u4e00\u4e9b\u91cd\u8981\u6982\u5ff5<\/h3>\n<\/p>\n<p><p><strong>GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c24\u5176\u662fCPython\u89e3\u91ca\u5668\uff0c\u5b58\u5728\u4e00\u4e2a\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\uff0c\u5b83\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u6267\u884cPython\u5b57\u8282\u7801\u3002\u8fd9\u662f\u4e3a\u4e86\u4fdd\u62a4Python\u5185\u90e8\u5bf9\u8c61\u7ed3\u6784\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u5b89\u5168\u6027\u3002\u867d\u7136GIL\u7684\u5b58\u5728\u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u7684\u5e76\u884c\u6267\u884c\uff0c\u4f46\u5728I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u591a\u7ebf\u7a0b\u4f9d\u7136\u80fd\u591f\u5e26\u6765\u6027\u80fd\u63d0\u5347\uff0c\u56e0\u4e3a\u7ebf\u7a0b\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u65f6\u53ef\u4ee5\u8ba9\u51faGIL\u3002<\/p>\n<\/p>\n<p><p><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u5b89\u5168\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u3002\u7ebf\u7a0b\u5b89\u5168\u6307\u7684\u662f\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u800c\u4e0d\u4f1a\u4ea7\u751f\u7ade\u6001\u6761\u4ef6\u3002\u4e3a\u4e86\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u6bd4\u5982\u9501\uff08Lock\uff09\u3001\u6761\u4ef6\u53d8\u91cf\uff08Condition\uff09\u3001\u4fe1\u53f7\u91cf\uff08Semaphore\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u9501\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>shared_data = 0<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_increment():<\/p>\n<p>    global shared_data<\/p>\n<p>    with lock:<\/p>\n<p>        temp = shared_data<\/p>\n<p>        temp += 1<\/p>\n<p>        shared_data = temp<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for _ in range(10):<\/p>\n<p>    thread = threading.Thread(target=safe_increment)<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u6240\u6709\u5b50\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>print(shared_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>threading.Lock<\/code>\u786e\u4fdd\u5bf9\u5171\u4eab\u6570\u636e<code>shared_data<\/code>\u7684\u8bbf\u95ee\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>\u7ebf\u7a0b\u6c60<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u4efb\u52a1\u65f6\uff0c\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u53ef\u80fd\u4f1a\u5f88\u5927\u3002\u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u7ba1\u7406\u7ebf\u7a0b\u7684\u673a\u5236\uff0c\u901a\u8fc7\u590d\u7528\u7ebf\u7a0b\u6765\u51cf\u5c11\u5f00\u9500\u3002Python\u4e2d\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7684\u4f8b\u5b50\uff1a<\/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>    print(f&quot;Processing {n}&quot;)<\/p>\n<p>    time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/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<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u5e76\u63d0\u4ea4\u4e8610\u4e2a\u4efb\u52a1\u3002\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u4f1a\u590d\u7528\u6765\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u591a\u8fdb\u7a0b\u4e2d\u7684\u4e00\u4e9b\u91cd\u8981\u6982\u5ff5<\/h3>\n<\/p>\n<p><p><strong>\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4e0d\u540c\u8fdb\u7a0b\u6709\u5404\u81ea\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u9700\u8981\u7279\u6b8a\u7684\u673a\u5236\u6765\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cdIPC\u673a\u5236\uff0c\u6bd4\u5982\u7ba1\u9053\uff08Pipe\uff09\u3001\u961f\u5217\uff08Queue\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u961f\u5217\u7684\u4f8b\u5b50\uff1a<\/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(f&quot;Produced {i}&quot;)<\/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(f&quot;Consumed {item}&quot;)<\/p>\n<p>queue = multiprocessing.Queue()<\/p>\n<p>producer_process = multiprocessing.Process(target=producer, args=(queue,))<\/p>\n<p>consumer_process = multiprocessing.Process(target=consumer, args=(queue,))<\/p>\n<p>producer_process.start()<\/p>\n<p>consumer_process.start()<\/p>\n<p>producer_process.join()<\/p>\n<p>queue.put(None)  # \u7ed3\u675f\u6d88\u8d39\u8005\u8fdb\u7a0b<\/p>\n<p>consumer_process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>multiprocessing.Queue<\/code>\u5728\u4e24\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u8fdb\u7a0b\u6c60<\/strong><\/p>\n<\/p>\n<p><p>\u4e0e\u7ebf\u7a0b\u6c60\u7c7b\u4f3c\uff0c\u8fdb\u7a0b\u6c60\u7528\u4e8e\u7ba1\u7406\u548c\u590d\u7528\u8fdb\u7a0b\u3002Python\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u8fdb\u7a0b\u6c60\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u8fdb\u7a0b\u6c60\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ProcessPoolExecutor<\/p>\n<p>import time<\/p>\n<p>def task(n):<\/p>\n<p>    print(f&quot;Processing {n}&quot;)<\/p>\n<p>    time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b\u6c60<\/strong><\/h2>\n<p>with ProcessPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    for i in range(10):<\/p>\n<p>        executor.submit(task, i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ProcessPoolExecutor<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u8fdb\u7a0b\u6c60\uff0c\u5e76\u63d0\u4ea4\u4e8610\u4e2a\u4efb\u52a1\u3002\u8fdb\u7a0b\u6c60\u4e2d\u7684\u8fdb\u7a0b\u4f1a\u590d\u7528\u6765\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u5f02\u6b65\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e9b\u91cd\u8981\u6982\u5ff5<\/h3>\n<\/p>\n<p><p><strong>\u4e8b\u4ef6\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>\u4e8b\u4ef6\u5faa\u73af\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u6838\u5fc3\u3002\u5b83\u4e0d\u65ad\u5faa\u73af\uff0c\u68c0\u67e5\u5e76\u6267\u884c\u51c6\u5907\u597d\u7684\u5f02\u6b65\u4efb\u52a1\u3002Python\u7684<code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e8b\u4ef6\u5faa\u73af\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u5faa\u73af\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def task():<\/p>\n<p>    print(&quot;Task started&quot;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(&quot;Task finished&quot;)<\/p>\n<p>loop = asyncio.get_event_loop()<\/p>\n<p>loop.run_until_complete(task())<\/p>\n<p>loop.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\uff0c\u5e76\u8fd0\u884c\u4e86\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>\u534f\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u534f\u7a0b\u662f\u5f02\u6b65\u7f16\u7a0b\u4e2d\u7684\u57fa\u672c\u5355\u4f4d\u3002\u5b83\u4eec\u662f\u53ef\u4ee5\u6682\u505c\u548c\u6062\u590d\u7684\u51fd\u6570\uff0c\u901a\u8fc7<code>await<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u5f02\u6b65\u6267\u884c\u5176\u4ed6\u534f\u7a0b\u3002\u534f\u7a0b\u7684\u5b9a\u4e49\u4f7f\u7528<code>async def<\/code>\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5e76\u53d1\u548c\u5e76\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u5e76\u53d1\u662f\u6307\u5728\u540c\u4e00\u65f6\u95f4\u6bb5\u5185\u5904\u7406\u591a\u4e2a\u4efb\u52a1\uff0c\u800c\u5e76\u884c\u662f\u6307\u5728\u540c\u4e00\u65f6\u523b\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u5f02\u6b65\u7f16\u7a0b\u4e2d\u7684\u5e76\u53d1\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af\u6765\u5b9e\u73b0\uff0c\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u4e2d\u7684\u5e76\u884c\u901a\u8fc7\u786c\u4ef6\u8d44\u6e90\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u6982\u5ff5\u548c\u673a\u5236\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u9009\u62e9\u548c\u4f7f\u7528\u9002\u5408\u7684\u5e76\u884c\u6267\u884c\u65b9\u5f0f\u6765\u5b9e\u73b0Python\u4e2d\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5faa\u73af\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\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5b9e\u73b0\u5e76\u884c\u5faa\u73af\u3002\u591a\u7ebf\u7a0b\u9002\u5408\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u800c\u591a\u8fdb\u7a0b\u66f4\u9002\u5408\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u7ebf\u7a0b\uff0c\u5e76\u5229\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u7684<code>ThreadPoolExecutor<\/code>\u6216<code>ProcessPoolExecutor<\/code>\u6765\u7b80\u5316\u5e76\u884c\u6267\u884c\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\u6765\u5904\u7406\u591a\u4e2a\u5faa\u73af\uff1f<\/strong><br \/>\u5f02\u6b65\u7f16\u7a0b\u662f\u5904\u7406\u591a\u4e2a\u5faa\u73af\u7684\u53e6\u4e00\u79cd\u6709\u6548\u65b9\u6cd5\u3002\u4f7f\u7528<code>asyncio<\/code>\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u534f\u7a0b\uff0c\u8fd9\u4e9b\u534f\u7a0b\u80fd\u591f\u5728\u9047\u5230I\/O\u64cd\u4f5c\u65f6\u6302\u8d77\u6267\u884c\uff0c\u4ece\u800c\u5b9e\u73b0\u5e76\u53d1\u3002\u901a\u8fc7<code>async<\/code>\u548c<code>await<\/code>\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u591a\u4e2a\u4efb\u52a1\u4e4b\u95f4\u5207\u6362\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u5bf9\u4e8e\u540c\u65f6\u8fd0\u884c\u7684\u4e24\u4e2a\u5faa\u73af\uff0c\u6709\u4ec0\u4e48\u6027\u80fd\u4f18\u5316\u5efa\u8bae\uff1f<\/strong><br \/>\u5728\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5faa\u73af\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\u6765\u51cf\u5c11\u5185\u5b58\u6d88\u8017\uff0c\u5e76\u901a\u8fc7\u8c03\u6574\u5faa\u73af\u7684\u6267\u884c\u987a\u5e8f\u6765\u4f18\u5316\u6027\u80fd\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u907f\u514d\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u7684\u5f71\u54cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u8fdb\u7a0b\u65b9\u5f0f\u6765\u5b9e\u73b0\u66f4\u597d\u7684\u6027\u80fd\u3002\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e5f\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u5faa\u73af\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8981\u540c\u65f6\u8fd0\u884c\u4e24\u4e2a\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5b9e\u73b0\u3002\u8fd9\u662f\u56e0\u4e3aPython\u672c\u8eab\u7684\u7ebf\u7a0b\uff08Thread [&hellip;]","protected":false},"author":3,"featured_media":1096263,"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\/1096256"}],"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=1096256"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096256\/revisions"}],"predecessor-version":[{"id":1096266,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096256\/revisions\/1096266"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096263"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}