{"id":1092224,"date":"2025-01-08T14:19:13","date_gmt":"2025-01-08T06:19:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1092224.html"},"modified":"2025-01-08T14:19:16","modified_gmt":"2025-01-08T06:19:16","slug":"python%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e7%ab%96%e7%9b%b4%e8%be%93%e5%87%ba%e4%b8%a4%e6%ac%a1-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1092224.html","title":{"rendered":"python\u5982\u4f55\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205345\/3c734e4a-34d6-4ae3-b149-211669efaa1c.webp\" alt=\"python\u5982\u4f55\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d:<\/p>\n<p><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b<\/strong>\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\u3002\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0cPython\u7684<code>threading<\/code>\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u5e76\u542f\u52a8\u591a\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u4efb\u52a1\uff0c\u901a\u8fc7\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u6267\u884c\u72ec\u7acb\u7684\u8f93\u51fa\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u3002\u5728\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c<code>multiprocessing<\/code>\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u6267\u884c\u72ec\u7acb\u7684\u8f93\u51fa\u64cd\u4f5c\u3002\u540c\u6837\uff0c\u5f02\u6b65\u7f16\u7a0b\u4f7f\u7528<code>asyncio<\/code>\u5e93\u4e5f\u80fd\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<strong>\u591a\u7ebf\u7a0b\u7f16\u7a0b<\/strong>\u662f\u5176\u4e2d\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u6765\u5e76\u884c\u6267\u884c\u4efb\u52a1\uff0c\u4ece\u800c\u5b9e\u73b0\u540c\u65f6\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u7f16\u7a0b\u662f\u4e00\u79cd\u5e38\u7528\u7684\u5e76\u884c\u7f16\u7a0b\u6280\u672f\uff0c\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u6765\u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002Python\u7684<code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u901a\u8fc7\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u6267\u884c\u72ec\u7acb\u7684\u8f93\u51fa\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\u7f16\u7a0b\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def print_vertical(text):<\/p>\n<p>    for char in text:<\/p>\n<p>        print(char)<\/p>\n<p>thread1 = threading.Thread(target=print_vertical, args=(&quot;Hello&quot;,))<\/p>\n<p>thread2 = threading.Thread(target=print_vertical, args=(&quot;World&quot;,))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u7ebf\u7a0b<code>thread1<\/code>\u548c<code>thread2<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u6267\u884c<code>print_vertical<\/code>\u51fd\u6570\uff0c\u5e76\u8f93\u51fa\u201cHello\u201d\u548c\u201cWorld\u201d\u3002\u901a\u8fc7\u542f\u52a8\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u591a\u7ebf\u7a0b\u7f16\u7a0b<\/p>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u7f16\u7a0b\u662f\u4e00\u79cd\u5e38\u7528\u7684\u5e76\u884c\u7f16\u7a0b\u6280\u672f\uff0c\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u6765\u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002Python\u7684<code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u901a\u8fc7\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u6267\u884c\u72ec\u7acb\u7684\u8f93\u51fa\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\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\u8fd9\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u76ee\u6807\u51fd\u6570\u548c\u53c2\u6570\u3002\u76ee\u6807\u51fd\u6570\u662f\u5728\u65b0\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684\u51fd\u6570\uff0c\u53c2\u6570\u662f\u4f20\u9012\u7ed9\u76ee\u6807\u51fd\u6570\u7684\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u7ebf\u7a0b\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def print_vertical(text):<\/p>\n<p>    for char in text:<\/p>\n<p>        print(char)<\/p>\n<p>thread1 = threading.Thread(target=print_vertical, args=(&quot;Hello&quot;,))<\/p>\n<p>thread2 = threading.Thread(target=print_vertical, args=(&quot;World&quot;,))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u7ebf\u7a0b<code>thread1<\/code>\u548c<code>thread2<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u6267\u884c<code>print_vertical<\/code>\u51fd\u6570\uff0c\u5e76\u8f93\u51fa\u201cHello\u201d\u548c\u201cWorld\u201d\u3002\u901a\u8fc7\u542f\u52a8\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7ebf\u7a0b\u540c\u6b65<\/h3>\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\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u5176\u4ed6\u95ee\u9898\u3002Python\u7684<code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u540c\u6b65\u673a\u5236\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\u9501\uff08Lock\uff09\u548c\u6761\u4ef6\u53d8\u91cf\uff08Condition\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u9501\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def print_vertical(text):<\/p>\n<p>    with lock:<\/p>\n<p>        for char in text:<\/p>\n<p>            print(char)<\/p>\n<p>thread1 = threading.Thread(target=print_vertical, args=(&quot;Hello&quot;,))<\/p>\n<p>thread2 = threading.Thread(target=print_vertical, args=(&quot;World&quot;,))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u9501<code>lock<\/code>\u6765\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u6267\u884c<code>print_vertical<\/code>\u51fd\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u7ebf\u7a0b\u95f4\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002Python\u7684<code>queue<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u9ad8\u6548\u7684\u7ebf\u7a0b\u95f4\u901a\u4fe1\u673a\u5236\u3002<code>queue.Queue<\/code>\u7c7b\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u961f\u5217\u6765\u4ea4\u6362\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u961f\u5217\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>def producer(q):<\/p>\n<p>    for i in range(5):<\/p>\n<p>        q.put(i)<\/p>\n<p>        print(f&quot;Produced {i}&quot;)<\/p>\n<p>def consumer(q):<\/p>\n<p>    while True:<\/p>\n<p>        item = q.get()<\/p>\n<p>        if item is None:<\/p>\n<p>            break<\/p>\n<p>        print(f&quot;Consumed {item}&quot;)<\/p>\n<p>q = queue.Queue()<\/p>\n<p>thread1 = threading.Thread(target=producer, args=(q,))<\/p>\n<p>thread2 = threading.Thread(target=consumer, args=(q,))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>q.put(None)<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u751f\u4ea7\u8005\u7ebf\u7a0b<code>producer<\/code>\u548c\u4e00\u4e2a\u6d88\u8d39\u8005\u7ebf\u7a0b<code>consumer<\/code>\uff0c\u5b83\u4eec\u901a\u8fc7\u961f\u5217<code>q<\/code>\u6765\u4ea4\u6362\u6570\u636e\u3002\u751f\u4ea7\u8005\u7ebf\u7a0b\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6570\u636e\u5e76\u5904\u7406\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u591a\u8fdb\u7a0b\u7f16\u7a0b<\/p>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u7f16\u7a0b\u662f\u4e00\u79cd\u5e76\u884c\u7f16\u7a0b\u6280\u672f\uff0c\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\u6765\u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\u3002\u901a\u8fc7\u5728\u6bcf\u4e2a\u8fdb\u7a0b\u4e2d\u6267\u884c\u72ec\u7acb\u7684\u8f93\u51fa\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\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\u8fd9\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u76ee\u6807\u51fd\u6570\u548c\u53c2\u6570\u3002\u76ee\u6807\u51fd\u6570\u662f\u5728\u65b0\u8fdb\u7a0b\u4e2d\u6267\u884c\u7684\u51fd\u6570\uff0c\u53c2\u6570\u662f\u4f20\u9012\u7ed9\u76ee\u6807\u51fd\u6570\u7684\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u8fdb\u7a0b\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def print_vertical(text):<\/p>\n<p>    for char in text:<\/p>\n<p>        print(char)<\/p>\n<p>process1 = multiprocessing.Process(target=print_vertical, args=(&quot;Hello&quot;,))<\/p>\n<p>process2 = multiprocessing.Process(target=print_vertical, args=(&quot;World&quot;,))<\/p>\n<p>process1.start()<\/p>\n<p>process2.start()<\/p>\n<p>process1.join()<\/p>\n<p>process2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u8fdb\u7a0b<code>process1<\/code>\u548c<code>process2<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u6267\u884c<code>print_vertical<\/code>\u51fd\u6570\uff0c\u5e76\u8f93\u51fa\u201cHello\u201d\u548c\u201cWorld\u201d\u3002\u901a\u8fc7\u542f\u52a8\u8fd9\u4e24\u4e2a\u8fdb\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u8fdb\u7a0b\u540c\u6b65<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u8fdb\u7a0b\u540c\u6b65\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\u3002\u5982\u679c\u591a\u4e2a\u8fdb\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u5176\u4ed6\u95ee\u9898\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u540c\u6b65\u673a\u5236\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\u9501\uff08Lock\uff09\u548c\u6761\u4ef6\u53d8\u91cf\uff08Condition\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u9501\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>lock = multiprocessing.Lock()<\/p>\n<p>def print_vertical(text):<\/p>\n<p>    with lock:<\/p>\n<p>        for char in text:<\/p>\n<p>            print(char)<\/p>\n<p>process1 = multiprocessing.Process(target=print_vertical, args=(&quot;Hello&quot;,))<\/p>\n<p>process2 = multiprocessing.Process(target=print_vertical, args=(&quot;World&quot;,))<\/p>\n<p>process1.start()<\/p>\n<p>process2.start()<\/p>\n<p>process1.join()<\/p>\n<p>process2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u9501<code>lock<\/code>\u6765\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u540c\u65f6\u6267\u884c<code>print_vertical<\/code>\u51fd\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u591a\u4e2a\u8fdb\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u8fdb\u7a0b\u95f4\u901a\u4fe1\u673a\u5236\uff0c\u4f8b\u5982\u7ba1\u9053\uff08Pipe\uff09\u548c\u961f\u5217\uff08Queue\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u961f\u5217\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def producer(q):<\/p>\n<p>    for i in range(5):<\/p>\n<p>        q.put(i)<\/p>\n<p>        print(f&quot;Produced {i}&quot;)<\/p>\n<p>def consumer(q):<\/p>\n<p>    while True:<\/p>\n<p>        item = q.get()<\/p>\n<p>        if item is None:<\/p>\n<p>            break<\/p>\n<p>        print(f&quot;Consumed {item}&quot;)<\/p>\n<p>q = multiprocessing.Queue()<\/p>\n<p>process1 = multiprocessing.Process(target=producer, args=(q,))<\/p>\n<p>process2 = multiprocessing.Process(target=consumer, args=(q,))<\/p>\n<p>process1.start()<\/p>\n<p>process2.start()<\/p>\n<p>process1.join()<\/p>\n<p>q.put(None)<\/p>\n<p>process2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u751f\u4ea7\u8005\u8fdb\u7a0b<code>producer<\/code>\u548c\u4e00\u4e2a\u6d88\u8d39\u8005\u8fdb\u7a0b<code>consumer<\/code>\uff0c\u5b83\u4eec\u901a\u8fc7\u961f\u5217<code>q<\/code>\u6765\u4ea4\u6362\u6570\u636e\u3002\u751f\u4ea7\u8005\u8fdb\u7a0b\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\uff0c\u6d88\u8d39\u8005\u8fdb\u7a0b\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6570\u636e\u5e76\u5904\u7406\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5f02\u6b65\u7f16\u7a0b<\/p>\n<\/p>\n<p><p>\u5f02\u6b65\u7f16\u7a0b\u662f\u4e00\u79cd\u5e76\u884c\u7f16\u7a0b\u6280\u672f\uff0c\u901a\u8fc7\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u5f02\u6b65\u51fd\u6570\u6765\u6267\u884c\u4efb\u52a1\u3002Python\u7684<code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u521b\u5efa\u548c\u7ba1\u7406\u5f02\u6b65\u4efb\u52a1\u3002\u901a\u8fc7\u5728\u6bcf\u4e2a\u5f02\u6b65\u4efb\u52a1\u4e2d\u6267\u884c\u72ec\u7acb\u7684\u8f93\u51fa\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\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\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5f02\u6b65\u51fd\u6570\u662f\u5728\u5f02\u6b65\u4efb\u52a1\u4e2d\u6267\u884c\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u5f02\u6b65\u4efb\u52a1\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def print_vertical(text):<\/p>\n<p>    for char in text:<\/p>\n<p>        print(char)<\/p>\n<p>        aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t asyncio.sleep(0.1)<\/p>\n<p>async def main():<\/p>\n<p>    task1 = asyncio.create_task(print_vertical(&quot;Hello&quot;))<\/p>\n<p>    task2 = asyncio.create_task(print_vertical(&quot;World&quot;))<\/p>\n<p>    await task1<\/p>\n<p>    await task2<\/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\u4e86\u4e24\u4e2a\u5f02\u6b65\u4efb\u52a1<code>task1<\/code>\u548c<code>task2<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u6267\u884c<code>print_vertical<\/code>\u51fd\u6570\uff0c\u5e76\u8f93\u51fa\u201cHello\u201d\u548c\u201cWorld\u201d\u3002\u901a\u8fc7\u542f\u52a8\u8fd9\u4e24\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5f02\u6b65\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u51fd\u6570\u662f\u4f7f\u7528<code>async def<\/code>\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>await<\/code>\u5173\u952e\u5b57\u6765\u6682\u505c\u6267\u884c\uff0c\u7b49\u5f85\u5176\u4ed6\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def print_vertical(text):<\/p>\n<p>    for char in text:<\/p>\n<p>        print(char)<\/p>\n<p>        await asyncio.sleep(0.1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>print_vertical<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u5b83\u5728\u6bcf\u6b21\u8f93\u51fa\u5b57\u7b26\u540e\u6682\u505c\u6267\u884c0.1\u79d2\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u5b9e\u73b0\u5e76\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u5f02\u6b65\u4efb\u52a1\u540c\u6b65<\/h3>\n<\/p>\n<p><p>\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u5f02\u6b65\u4efb\u52a1\u540c\u6b65\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u5982\u679c\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u5176\u4ed6\u95ee\u9898\u3002Python\u7684<code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u540c\u6b65\u673a\u5236\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\u9501\uff08Lock\uff09\u548c\u6761\u4ef6\u53d8\u91cf\uff08Condition\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u9501\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>lock = asyncio.Lock()<\/p>\n<p>async def print_vertical(text):<\/p>\n<p>    async with lock:<\/p>\n<p>        for char in text:<\/p>\n<p>            print(char)<\/p>\n<p>            await asyncio.sleep(0.1)<\/p>\n<p>async def main():<\/p>\n<p>    task1 = asyncio.create_task(print_vertical(&quot;Hello&quot;))<\/p>\n<p>    task2 = asyncio.create_task(print_vertical(&quot;World&quot;))<\/p>\n<p>    await task1<\/p>\n<p>    await task2<\/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\u4f7f\u7528\u4e00\u4e2a\u9501<code>lock<\/code>\u6765\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u53ef\u4ee5\u540c\u65f6\u6267\u884c<code>print_vertical<\/code>\u51fd\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b<\/strong>\u662fPython\u4e2d\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u591a\u7ebf\u7a0b\u7f16\u7a0b\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u6765\u5e76\u884c\u6267\u884c\u4efb\u52a1\uff0c\u800c\u591a\u8fdb\u7a0b\u7f16\u7a0b\u5219\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\u6765\u5e76\u884c\u6267\u884c\u4efb\u52a1\u3002\u5f02\u6b65\u7f16\u7a0b\u901a\u8fc7\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u5f02\u6b65\u51fd\u6570\u6765\u5b9e\u73b0\u5e76\u884c\u8f93\u51fa\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5e76\u884c\u7f16\u7a0b\u6280\u672f\u6765\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u3002\u5728\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\/\u8fdb\u7a0b\u540c\u6b65\u548c\u901a\u4fe1\u662f\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u95ee\u9898\uff0c\u800c\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u5f02\u6b65\u4efb\u52a1\u540c\u6b65\u548c\u5f02\u6b65\u51fd\u6570\u7684\u4f7f\u7528\u662f\u5173\u952e\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u6709\u6548\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7ad6\u76f4\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8981\u5b9e\u73b0\u7ad6\u76f4\u8f93\u51fa\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u9010\u884c\u6253\u5370\u5185\u5bb9\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u5c06\u9700\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u653e\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528for\u5faa\u73af\u9010\u4e2a\u6253\u5370\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">lines = [&quot;\u7b2c\u4e00\u884c&quot;, &quot;\u7b2c\u4e8c\u884c&quot;, &quot;\u7b2c\u4e09\u884c&quot;]\nfor line in lines:\n    print(line)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u540c\u65f6\u8f93\u51fa\u591a\u6b21\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u5d4c\u5957\u5b9e\u73b0\u540c\u65f6\u8f93\u51fa\u591a\u6b21\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u8f93\u51fa\u76f8\u540c\u7684\u5185\u5bb9\u4e24\u6b21\uff0c\u53ef\u4ee5\u5c06\u8f93\u51fa\u7684\u5185\u5bb9\u653e\u5728\u5916\u5c42\u5faa\u73af\u4e2d\uff0c\u5e76\u5728\u5185\u5c42\u5faa\u73af\u4e2d\u6253\u5370\uff1a<\/p>\n<pre><code class=\"language-python\">lines = [&quot;\u5185\u5bb91&quot;, &quot;\u5185\u5bb92&quot;, &quot;\u5185\u5bb93&quot;]\nfor _ in range(2):  # \u8f93\u51fa\u4e24\u6b21\n    for line in lines:\n        print(line)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06\u8f93\u51fa\u7ed3\u679c\u683c\u5f0f\u5316\u4e3a\u7ad6\u76f4\u6392\u5217\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u7ad6\u76f4\u6392\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684join\u65b9\u6cd5\uff0c\u5e76\u6307\u5b9a\u6362\u884c\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">output = &quot;\\n&quot;.join([&quot;\u5185\u5bb9A&quot;, &quot;\u5185\u5bb9B&quot;, &quot;\u5185\u5bb9C&quot;])\nprint(output)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c\u8f93\u51fa\u65f6\u4f1a\u5728\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u63d2\u5165\u6362\u884c\u7b26\uff0c\u4ece\u800c\u5b9e\u73b0\u7ad6\u76f4\u6392\u5217\u7684\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: \u4f7f\u7528\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u540c\u65f6\u7ad6\u76f4\u8f93\u51fa\u4e24\u6b21\u3002\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0cPython [&hellip;]","protected":false},"author":3,"featured_media":1092231,"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\/1092224"}],"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=1092224"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092224\/revisions"}],"predecessor-version":[{"id":1092232,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092224\/revisions\/1092232"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1092231"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1092224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1092224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1092224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}