{"id":1112107,"date":"2025-01-08T17:35:29","date_gmt":"2025-01-08T09:35:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112107.html"},"modified":"2025-01-08T17:35:32","modified_gmt":"2025-01-08T09:35:32","slug":"%e5%9c%a8python%e7%a8%8b%e5%ba%8f%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e5%a4%9a%e5%bc%80%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112107.html","title":{"rendered":"\u5728python\u7a0b\u5e8f\u4e2d\u5982\u4f55\u5229\u7528\u591a\u5f00\u7ebf\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074250\/781822cd-af1a-4c9a-acc3-a42ca9f2befb.webp\" alt=\"\u5728python\u7a0b\u5e8f\u4e2d\u5982\u4f55\u5229\u7528\u591a\u5f00\u7ebf\u7a0b\" \/><\/p>\n<p><p> \u5728Python\u7a0b\u5e8f\u4e2d\u5229\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 <code>threading<\/code> \u6a21\u5757\u3001<code>concurrent.futures<\/code> \u6a21\u5757\u4ee5\u53ca <code>multiprocessing<\/code> \u6a21\u5757\u3002<strong>\u4f7f\u7528\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3001\u6539\u5584\u7528\u6237\u4f53\u9a8c\u3001\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u4f18\u52bf<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a<code>Thread<\/code>\u5bf9\u8c61\u5e76\u4f20\u9012\u4e00\u4e2a\u76ee\u6807\u51fd\u6570\u6765\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Thread started&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(&quot;Thread finished&quot;)<\/p>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    t = threading.Thread(target=worker)<\/p>\n<p>    threads.append(t)<\/p>\n<p>    t.start()<\/p>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p>print(&quot;All threads completed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e865\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6267\u884c<code>worker<\/code>\u51fd\u6570\u3002\u5728\u4e3b\u7ebf\u7a0b\u4e2d\uff0c\u6211\u4eec\u542f\u52a8\u4e86\u6240\u6709\u7684\u7ebf\u7a0b\uff0c\u5e76\u7b49\u5f85\u5b83\u4eec\u5b8c\u6210\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001THREADING\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>threading<\/code>\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u6807\u51c6\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7684\u7ebf\u7a0b\u7ba1\u7406\u529f\u80fd\uff0c\u5141\u8bb8\u4f60\u8f7b\u677e\u5730\u521b\u5efa\u548c\u7ba1\u7406\u591a\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u521b\u5efa\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4f60\u9700\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2a<code>Thread<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f20\u9012\u4e00\u4e2a\u76ee\u6807\u51fd\u6570\u548c\u53ef\u9009\u7684\u53c2\u6570\u3002\u7136\u540e\uff0c\u8c03\u7528<code>start()<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(i)<\/p>\n<p>thread = threading.Thread(target=print_numbers)<\/p>\n<p>thread.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>print_numbers<\/code>\u51fd\u6570\u5c06\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u7ebf\u7a0b\u7c7b<\/h4>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>Thread<\/code>\u7c7b\u6765\u521b\u5efa\u7ebf\u7a0b\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u5c01\u88c5\u7ebf\u7a0b\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class MyThread(threading.Thread):<\/p>\n<p>    def run(self):<\/p>\n<p>        for i in range(10):<\/p>\n<p>            print(i)<\/p>\n<p>thread = MyThread()<\/p>\n<p>thread.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684<code>MyThread<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199\u4e86<code>run<\/code>\u65b9\u6cd5\u6765\u5b9a\u4e49\u7ebf\u7a0b\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u7ebf\u7a0b\u540c\u6b65<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u540c\u6b65\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u907f\u514d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u5f15\u8d77\u7684\u6570\u636e\u7ade\u4e89\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Lock<\/code>\u5bf9\u8c61\u6765\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def print_numbers():<\/p>\n<p>    with lock:<\/p>\n<p>        for i in range(10):<\/p>\n<p>            print(i)<\/p>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    thread = threading.Thread(target=print_numbers)<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/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\u5728\u540c\u4e00\u65f6\u95f4\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h4>1.4 \u7ebf\u7a0b\u95f4\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>\u7ebf\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u901a\u5e38\u901a\u8fc7\u961f\u5217\u6765\u5b9e\u73b0\u3002<code>queue<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>q = queue.Queue()<\/p>\n<p>def producer():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        q.put(i)<\/p>\n<p>def consumer():<\/p>\n<p>    while not q.empty():<\/p>\n<p>        item = q.get()<\/p>\n<p>        print(item)<\/p>\n<p>        q.task_done()<\/p>\n<p>producer_thread = threading.Thread(target=producer)<\/p>\n<p>consumer_thread = threading.Thread(target=consumer)<\/p>\n<p>producer_thread.start()<\/p>\n<p>consumer_thread.start()<\/p>\n<p>producer_thread.join()<\/p>\n<p>consumer_thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u4ea7\u8005\u7ebf\u7a0b\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e8c\u3001CONCURRENT.FUTURES\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u7ea7\u63a5\u53e3\uff0c\u7528\u4e8e\u5f02\u6b65\u6267\u884c\u4efb\u52a1\u3002\u5b83\u63d0\u4f9b\u4e86<code>ThreadPoolExecutor<\/code>\u7c7b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u7ebf\u7a0b\u6c60\uff0c\u5e76\u5f02\u6b65\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u521b\u5efa\u7ebf\u7a0b\u6c60<\/h4>\n<\/p>\n<p><p><code>ThreadPoolExecutor<\/code>\u7c7b\u5141\u8bb8\u4f60\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u5e76\u63d0\u4ea4\u4efb\u52a1\u7ed9\u7ebf\u7a0b\u6c60\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(i)<\/p>\n<p>with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    for _ in range(5):<\/p>\n<p>        executor.submit(print_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b5\u4e2a\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\uff0c\u5e76\u5411\u7ebf\u7a0b\u6c60\u63d0\u4ea4\u4e865\u4e2a\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210<\/h4>\n<\/p>\n<p><p><code>ThreadPoolExecutor<\/code>\u7c7b\u63d0\u4f9b\u4e86<code>as_completed<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u7b49\u5f85\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 print_numbers():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(i)<\/p>\n<p>with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    futures = [executor.submit(print_numbers) for _ in range(5)]<\/p>\n<p>    for future in as_completed(futures):<\/p>\n<p>        future.result()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>as_completed<\/code>\u65b9\u6cd5\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e09\u3001MULTIPROCESSING\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u5141\u8bb8\u4f60\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u5e76\u5728\u8fd9\u4e9b\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7c7b\u4f3c\u4e8e<code>threading<\/code>\u6a21\u5757\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u521b\u5efa\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Process<\/code>\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(i)<\/p>\n<p>process = multiprocessing.Process(target=print_numbers)<\/p>\n<p>process.start()<\/p>\n<p>process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>print_numbers<\/code>\u51fd\u6570\u5c06\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u8fdb\u7a0b\u4e2d\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528\u8fdb\u7a0b\u6c60<\/h4>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Pool<\/code>\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u7ba1\u7406\u8fdb\u7a0b\u6c60\uff0c\u5e76\u5f02\u6b65\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(i)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    with multiprocessing.Pool(processes=5) as pool:<\/p>\n<p>        pool.map(print_numbers, range(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b5\u4e2a\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u6c60\uff0c\u5e76\u5411\u8fdb\u7a0b\u6c60\u63d0\u4ea4\u4e865\u4e2a\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>3.3 \u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h4>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u7528\u4e8e\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u7c7b\uff0c\u5305\u62ec<code>Queue<\/code>\u3001<code>Pipe<\/code>\u548c<code>Value<\/code>\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(10):<\/p>\n<p>        queue.put(i)<\/p>\n<p>def consumer(queue):<\/p>\n<p>    while not queue.empty():<\/p>\n<p>        item = queue.get()<\/p>\n<p>        print(item)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/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>    consumer_process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u4ea7\u8005\u8fdb\u7a0b\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\uff0c\u6d88\u8d39\u8005\u8fdb\u7a0b\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u56db\u3001ASYNCIO\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e\u4e8b\u4ef6\u5faa\u73af\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u3002\u5b83\u5141\u8bb8\u4f60\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\uff0c\u800c\u65e0\u9700\u663e\u5f0f\u5730\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u521b\u5efa\u534f\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728<code>asyncio<\/code>\u4e2d\uff0c\u534f\u7a0b\u662f\u5f02\u6b65\u51fd\u6570\u7684\u57fa\u672c\u5355\u5143\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>async def<\/code>\u8bed\u6cd5\u5b9a\u4e49\u534f\u7a0b\uff0c\u5e76\u4f7f\u7528<code>await<\/code>\u5173\u952e\u5b57\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def print_numbers():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(i)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>asyncio.run(print_numbers())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>print_numbers<\/code>\u534f\u7a0b\u5c06\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u5f02\u6b65\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u5e76\u53d1\u6267\u884c\u534f\u7a0b<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>asyncio.gather<\/code>\u51fd\u6570\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u534f\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def print_numbers():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(i)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>async def main():<\/p>\n<p>    await asyncio.gather(*(print_numbers() for _ in range(5)))<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e76\u53d1\u6267\u884c\u4e865\u4e2a<code>print_numbers<\/code>\u534f\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>4.3 \u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>asyncio.create_task<\/code>\u51fd\u6570\u5c06\u534f\u7a0b\u5305\u88c5\u6210\u4efb\u52a1\uff0c\u5e76\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u5e76\u53d1\u6267\u884c\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def print_numbers():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(i)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>async def main():<\/p>\n<p>    tasks = [asyncio.create_task(print_numbers()) for _ in range(5)]<\/p>\n<p>    await asyncio.gather(*tasks)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e865\u4e2a\u4efb\u52a1\uff0c\u5e76\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u5e76\u53d1\u6267\u884c\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u7bc7\u535a\u5ba2\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u7a0b\u5e8f\u4e2d\u5229\u7528\u591a\u7ebf\u7a0b\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u3001<code>concurrent.futures<\/code>\u6a21\u5757\u3001<code>multiprocessing<\/code>\u6a21\u5757\u548c<code>asyncio<\/code>\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong><code>threading<\/code>\u6a21\u5757<\/strong>\u9002\u7528\u4e8eIO\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c<strong><code>concurrent.futures<\/code>\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\uff0c<strong><code>multiprocessing<\/code>\u6a21\u5757<\/strong>\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u800c<strong><code>asyncio<\/code>\u6a21\u5757<\/strong>\u5219\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u5e76\u53d1\u548c\u4f4e\u5ef6\u8fdf\u7684\u5f02\u6b65\u7f16\u7a0b\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u4f60\u80fd\u591f\u719f\u7ec3\u638c\u63e1\u5728Python\u7a0b\u5e8f\u4e2d\u5229\u7528\u591a\u7ebf\u7a0b\u7684\u6280\u5de7\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u591a\u7ebf\u7a0b\u7684\u597d\u5904\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u591a\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u7279\u522b\u662f\u5728\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\u3002\u901a\u8fc7\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u591a\u4e2a\u7ebf\u7a0b\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u65f6\u540c\u65f6\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u8fd9\u6837\u80fd\u663e\u8457\u51cf\u5c11\u6574\u4f53\u7684\u7b49\u5f85\u65f6\u95f4\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u5f97\u7a0b\u5e8f\u5728\u7528\u6237\u754c\u9762\u65b9\u9762\u66f4\u52a0\u6d41\u7545\uff0c\u907f\u514d\u56e0\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u800c\u5bfc\u81f4\u754c\u9762\u65e0\u54cd\u5e94\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u57fa\u672c\u6b65\u9aa4\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165<code>threading<\/code>\u6a21\u5757\uff0c\u5b9a\u4e49\u9700\u8981\u5728\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684\u51fd\u6570\uff0c\u7136\u540e\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61\u5e76\u542f\u52a8\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528<code>threading.Thread<\/code>\u7c7b\u6765\u521b\u5efa\u7ebf\u7a0b\uff0c\u5e76\u901a\u8fc7\u8c03\u7528<code>start()<\/code>\u65b9\u6cd5\u6765\u8fd0\u884c\u7ebf\u7a0b\u3002\u6700\u540e\uff0c\u4e3a\u4e86\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u90fd\u5b8c\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u6765\u7b49\u5f85\u7ebf\u7a0b\u7684\u7ed3\u675f\u3002<\/p>\n<p><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u591a\u7ebf\u7a0b\u65f6\uff0c\u9700\u7279\u522b\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u9501\uff08<code>threading.Lock<\/code>\uff09\u6765\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002\u6b64\u5916\uff0c\u8fc7\u591a\u7684\u7ebf\u7a0b\u53ef\u80fd\u5bfc\u81f4\u4e0a\u4e0b\u6587\u5207\u6362\u9891\u7e41\uff0c\u4ece\u800c\u5f71\u54cd\u6027\u80fd\uff0c\u56e0\u6b64\u5408\u7406\u8bbe\u7f6e\u7ebf\u7a0b\u6570\u91cf\u4e5f\u662f\u4f18\u5316\u7684\u91cd\u8981\u65b9\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7a0b\u5e8f\u4e2d\u5229\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 threading \u6a21\u5757\u3001concur [&hellip;]","protected":false},"author":3,"featured_media":1112119,"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\/1112107"}],"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=1112107"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112107\/revisions"}],"predecessor-version":[{"id":1112120,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112107\/revisions\/1112120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112119"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}