{"id":1082304,"date":"2025-01-08T12:47:34","date_gmt":"2025-01-08T04:47:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082304.html"},"modified":"2025-01-08T12:47:36","modified_gmt":"2025-01-08T04:47:36","slug":"python%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%a6%82%e4%bd%95%e4%b8%80%e7%9b%b4%e8%bf%90%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1082304.html","title":{"rendered":"python\u591a\u7ebf\u7a0b\u5982\u4f55\u4e00\u76f4\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183909\/3172fa83-9fe9-40f7-bda3-51eb92081af7.webp\" alt=\"python\u591a\u7ebf\u7a0b\u5982\u4f55\u4e00\u76f4\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>Python\u591a\u7ebf\u7a0b\u5982\u4f55\u4e00\u76f4\u8fd0\u884c<\/strong>\u53ef\u4ee5\u901a\u8fc7<strong>\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u3001\u5faa\u73af\u8fd0\u884c\u3001\u4fe1\u53f7\u5904\u7406<\/strong>\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u5b88\u62a4\u7ebf\u7a0b\u662f\u4e00\u79cd\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u540e\u81ea\u52a8\u5173\u95ed\u7684\u7ebf\u7a0b\uff0c\u901a\u8fc7\u8bbe\u7f6e\u5b88\u62a4\u7ebf\u7a0b\u53ef\u4ee5\u786e\u4fdd\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u540e\u7ee7\u7eed\u8fd0\u884c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u5b9e\u73b0Python\u591a\u7ebf\u7a0b\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b88\u62a4\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u5b88\u62a4\u7ebf\u7a0b\u662f\u6307\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u540e\uff0c\u4f9d\u7136\u7ee7\u7eed\u8fd0\u884c\u7684\u7ebf\u7a0b\u3002Python\u4e2d\u7684\u7ebf\u7a0b\u5bf9\u8c61\u63d0\u4f9b\u4e86<code>daemon<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u5c06\u7ebf\u7a0b\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u3002\u5f53\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u5b88\u62a4\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u5bf9\u8c61\u7684<code>daemon<\/code>\u5c5e\u6027\u6765\u5c06\u5176\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\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>    while True:<\/p>\n<p>        print(&quot;Worker thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61<\/strong><\/h2>\n<p>t = threading.Thread(target=worker)<\/p>\n<h2><strong>\u5c06\u7ebf\u7a0b\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b<\/strong><\/h2>\n<p>t.daemon = True<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>t.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c<\/strong><\/h2>\n<p>print(&quot;M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n thread is running&quot;)<\/p>\n<p>time.sleep(5)<\/p>\n<p>print(&quot;Main thread is ending&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>worker<\/code>\u51fd\u6570\u4f1a\u4e00\u76f4\u8fd0\u884c\u5e76\u6253\u5370\u6d88\u606f\u3002\u6211\u4eec\u5c06\u7ebf\u7a0b<code>t<\/code>\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u8fd9\u6837\u5f53\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e5f\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b88\u62a4\u7ebf\u7a0b\u7684\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5b88\u62a4\u7ebf\u7a0b\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u540e\u53f0\u4efb\u52a1\u3001\u5b9a\u65f6\u4efb\u52a1\u3001\u76d1\u63a7\u4efb\u52a1\u7b49\u90fd\u53ef\u4ee5\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def monitor():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Monitoring system status&quot;)<\/p>\n<p>        time.sleep(2)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u5b88\u62a4\u7ebf\u7a0b<\/strong><\/h2>\n<p>monitor_thread = threading.Thread(target=monitor)<\/p>\n<p>monitor_thread.daemon = True<\/p>\n<p>monitor_thread.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    print(&quot;Main thread is doing work&quot;)<\/p>\n<p>    time.sleep(1)<\/p>\n<p>print(&quot;Main thread is ending&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b<code>monitor_thread<\/code>\uff0c\u7528\u4e8e\u76d1\u63a7\u7cfb\u7edf\u72b6\u6001\u3002\u4e3b\u7ebf\u7a0b\u6267\u884c\u4e00\u7cfb\u5217\u4efb\u52a1\u540e\u7ed3\u675f\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e5f\u4f1a\u968f\u4e4b\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5faa\u73af\u8fd0\u884c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u7ebf\u7a0b\u7684\u6301\u7eed\u8fd0\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Worker thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    print(&quot;Main thread is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728<code>worker<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u4f7f\u5f97\u7ebf\u7a0b<code>t<\/code>\u4f1a\u4e00\u76f4\u8fd0\u884c\u3002\u4e3b\u7ebf\u7a0b\u540c\u6837\u4f7f\u7528\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u6765\u4fdd\u6301\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u7ebf\u7a0b\u9000\u51fa<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5728\u67d0\u4e9b\u6761\u4ef6\u4e0b\u9000\u51fa\u7ebf\u7a0b\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u5171\u4eab\u7684\u6807\u5fd7\u53d8\u91cf\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u9000\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<h2><strong>\u5171\u4eab\u7684\u6807\u5fd7\u53d8\u91cf<\/strong><\/h2>\n<p>exit_flag = False<\/p>\n<p>def worker():<\/p>\n<p>    global exit_flag<\/p>\n<p>    while not exit_flag:<\/p>\n<p>        print(&quot;Worker thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    print(&quot;Main thread is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<h2><strong>\u8bbe\u7f6e\u9000\u51fa\u6807\u5fd7<\/strong><\/h2>\n<p>exit_flag = True<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>t.join()<\/p>\n<p>print(&quot;Main thread is ending&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf<code>exit_flag<\/code>\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u9000\u51fa\u3002\u4e3b\u7ebf\u7a0b\u5728\u6267\u884c\u5b8c\u4efb\u52a1\u540e\uff0c\u8bbe\u7f6e<code>exit_flag<\/code>\u4e3a<code>True<\/code>\uff0c\u7136\u540e\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4fe1\u53f7\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fe1\u53f7\u5904\u7406\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u8fd0\u884c\u548c\u9000\u51fa\u3002Python\u7684<code>signal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u4fe1\u53f7\u7684\u673a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6ce8\u518c\u4fe1\u53f7\u5904\u7406\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>import signal<\/p>\n<p>import sys<\/p>\n<h2><strong>\u5171\u4eab\u7684\u6807\u5fd7\u53d8\u91cf<\/strong><\/h2>\n<p>exit_flag = False<\/p>\n<p>def worker():<\/p>\n<p>    global exit_flag<\/p>\n<p>    while not exit_flag:<\/p>\n<p>        print(&quot;Worker thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def signal_handler(sig, frame):<\/p>\n<p>    global exit_flag<\/p>\n<p>    exit_flag = True<\/p>\n<p>    print(&quot;Signal received, exiting...&quot;)<\/p>\n<h2><strong>\u6ce8\u518c\u4fe1\u53f7\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>signal.signal(signal.SIGINT, signal_handler)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7b49\u5f85<\/strong><\/h2>\n<p>t.join()<\/p>\n<p>print(&quot;Main thread is ending&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>signal.signal<\/code>\u51fd\u6570\u6ce8\u518c\u4e86\u4e00\u4e2a\u4fe1\u53f7\u5904\u7406\u51fd\u6570<code>signal_handler<\/code>\uff0c\u5f53\u63a5\u6536\u5230<code>SIGINT<\/code>\u4fe1\u53f7\uff08\u901a\u5e38\u662fCtrl+C\uff09\u65f6\uff0c\u4f1a\u8bbe\u7f6e<code>exit_flag<\/code>\u4e3a<code>True<\/code>\uff0c\u4ece\u800c\u7ec8\u6b62\u7ebf\u7a0b\u7684\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ebf\u7a0b\u6c60<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u591a\u4e2a\u7ebf\u7a0b\u5e76\u5206\u914d\u4efb\u52a1\u3002Python\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u6c60\uff08ThreadPoolExecutor\uff09\u6765\u7b80\u5316\u8fd9\u79cd\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6267\u884c\u4efb\u52a1<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>import time<\/p>\n<p>def worker(name):<\/p>\n<p>    while True:<\/p>\n<p>        print(f&quot;Worker {name} is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    for i in range(3):<\/p>\n<p>        executor.submit(worker, i)<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    print(&quot;Main thread is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b3\u4e2a\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\uff0c\u5e76\u63d0\u4ea4\u4e863\u4e2a\u4efb\u52a1\u3002\u6bcf\u4e2a\u4efb\u52a1\u5bf9\u5e94\u4e00\u4e2a<code>worker<\/code>\u51fd\u6570\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u81ea\u52a8\u7ba1\u7406\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u8c03\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ba1\u7406\u7ebf\u7a0b\u6c60\u7684\u9000\u51fa<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>shutdown<\/code>\u65b9\u6cd5\u6765\u7ba1\u7406\u7ebf\u7a0b\u6c60\u7684\u9000\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\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 worker(name):<\/p>\n<p>    while True:<\/p>\n<p>        print(f&quot;Worker {name} is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    for i in range(3):<\/p>\n<p>        executor.submit(worker, i)<\/p>\n<p>    # \u4e3b\u7ebf\u7a0b\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4<\/p>\n<p>    time.sleep(10)<\/p>\n<p>    # \u5173\u95ed\u7ebf\u7a0b\u6c60<\/p>\n<p>    executor.shutdown(wait=False)<\/p>\n<p>    print(&quot;Thread pool is shutting down&quot;)<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c<\/strong><\/h2>\n<p>print(&quot;Main thread is ending&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u8c03\u7528<code>executor.shutdown<\/code>\u65b9\u6cd5\u5173\u95ed\u7ebf\u7a0b\u6c60\uff0c\u5e76\u8bbe\u7f6e<code>wait<\/code>\u53c2\u6570\u4e3a<code>False<\/code>\uff0c\u8fd9\u6837\u4e3b\u7ebf\u7a0b\u4e0d\u4f1a\u7b49\u5f85\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u6807\u51c6\u5e93\u4e2d\u7684\u7ebf\u7a0b\u6a21\u5757\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u591a\u7ebf\u7a0b\u7ba1\u7406\u529f\u80fd\uff0c\u4f8b\u5982<code>threading2<\/code>\u3001<code>concurrent.futures<\/code>\u3001<code>gevent<\/code>\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>gevent<\/code>\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u4f7f\u7528<code>gevent<\/code>\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>gevent<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install gevent<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>gevent<\/code>\u5e93\u521b\u5efa\u548c\u7ba1\u7406\u591a\u7ebf\u7a0b\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gevent<\/p>\n<p>from gevent import monkey<\/p>\n<p>import time<\/p>\n<h2><strong>\u6253\u8865\u4e01\uff0c\u786e\u4fdd\u6240\u6709\u963b\u585e\u64cd\u4f5c\u90fd\u88abgevent\u7ba1\u7406<\/strong><\/h2>\n<p>monkey.patch_all()<\/p>\n<p>def worker(name):<\/p>\n<p>    while True:<\/p>\n<p>        print(f&quot;Worker {name} is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u534f\u7a0b<\/strong><\/h2>\n<p>gevent.spawn(worker, &quot;A&quot;)<\/p>\n<p>gevent.spawn(worker, &quot;B&quot;)<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    print(&quot;Main thread is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>gevent.spawn<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e24\u4e2a\u534f\u7a0b\uff0c\u6bcf\u4e2a\u534f\u7a0b\u5bf9\u5e94\u4e00\u4e2a<code>worker<\/code>\u51fd\u6570\u3002<code>gevent<\/code>\u5e93\u901a\u8fc7\u534f\u7a0b\u5b9e\u73b0\u4e86\u7c7b\u4f3c\u591a\u7ebf\u7a0b\u7684\u5e76\u53d1\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>asyncio<\/code><\/h3>\n<\/p>\n<p><p>Python\u7684<code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f02\u6b65\u7f16\u7a0b\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u5b9e\u73b0\u591a\u4efb\u52a1\u7684\u5e76\u53d1\u6267\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>asyncio<\/code>\u5b9e\u73b0\u5f02\u6b65\u4efb\u52a1<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def worker(name):<\/p>\n<p>    while True:<\/p>\n<p>        print(f&quot;Worker {name} is running&quot;)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>async def main():<\/p>\n<p>    # \u521b\u5efa\u5e76\u542f\u52a8\u5f02\u6b65\u4efb\u52a1<\/p>\n<p>    task1 = asyncio.create_task(worker(&quot;A&quot;))<\/p>\n<p>    task2 = asyncio.create_task(worker(&quot;B&quot;))<\/p>\n<p>    # \u4e3b\u4efb\u52a1\u7ee7\u7eed\u8fd0\u884c<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Main task is running&quot;)<\/p>\n<p>        await asyncio.sleep(2)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u4efb\u52a1<\/strong><\/h2>\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<code>asyncio.create_task<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e24\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5bf9\u5e94\u4e00\u4e2a<code>worker<\/code>\u51fd\u6570\u3002<code>asyncio.run<\/code>\u51fd\u6570\u7528\u4e8e\u8fd0\u884c\u4e3b\u4efb\u52a1<code>main<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5f02\u6b65\u4efb\u52a1\u7684\u9000\u51fa<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u53d6\u6d88\u5f02\u6b65\u4efb\u52a1\u6765\u5904\u7406\u4efb\u52a1\u7684\u9000\u51fa\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 worker(name):<\/p>\n<p>    while True:<\/p>\n<p>        print(f&quot;Worker {name} is running&quot;)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>async def main():<\/p>\n<p>    # \u521b\u5efa\u5e76\u542f\u52a8\u5f02\u6b65\u4efb\u52a1<\/p>\n<p>    task1 = asyncio.create_task(worker(&quot;A&quot;))<\/p>\n<p>    task2 = asyncio.create_task(worker(&quot;B&quot;))<\/p>\n<p>    # \u4e3b\u4efb\u52a1\u7ee7\u7eed\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4<\/p>\n<p>    await asyncio.sleep(10)<\/p>\n<p>    # \u53d6\u6d88\u5f02\u6b65\u4efb\u52a1<\/p>\n<p>    task1.cancel()<\/p>\n<p>    task2.cancel()<\/p>\n<p>    print(&quot;Tasks are cancelled&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u4efb\u52a1<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u4e3b\u4efb\u52a1\u4e2d\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u8c03\u7528<code>task1.cancel<\/code>\u548c<code>task2.cancel<\/code>\u65b9\u6cd5\u53d6\u6d88\u5f02\u6b65\u4efb\u52a1\uff0c\u4ece\u800c\u5b9e\u73b0\u4efb\u52a1\u7684\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0Python\u591a\u7ebf\u7a0b\u7684\u6301\u7eed\u8fd0\u884c\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u3001\u5faa\u73af\u7ed3\u6784\u3001\u4fe1\u53f7\u5904\u7406\u3001\u7ebf\u7a0b\u6c60\u3001\u7b2c\u4e09\u65b9\u5e93\uff0c\u8fd8\u662f<code>asyncio<\/code>\u6a21\u5757\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u3002\u5e0c\u671b\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u6301\u7eed\u8fd0\u884c\u7684\u591a\u7ebf\u7a0b\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u8981\u6267\u884c\u7684\u4efb\u52a1\u51fd\u6570\u3002\u63a5\u7740\uff0c\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u5e76\u542f\u52a8\u5b83\u4eec\u3002\u4e3a\u4e86\u786e\u4fdd\u7ebf\u7a0b\u80fd\u591f\u6301\u7eed\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u6765\u4fdd\u6301\u7ebf\u7a0b\u6d3b\u8dc3\u3002\u8bb0\u5f97\u5728\u8bbe\u8ba1\u7ebf\u7a0b\u65f6\uff0c\u5408\u7406\u8bbe\u7f6e\u9000\u51fa\u6761\u4ef6\uff0c\u907f\u514d\u521b\u5efa\u65e0\u6cd5\u7ec8\u6b62\u7684\u7ebf\u7a0b\u3002<\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u5728Python\u4e2d\u6709\u4ec0\u4e48\u4f18\u7f3a\u70b9\uff1f<\/strong><br \/>\u591a\u7ebf\u7a0b\u5728Python\u4e2d\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\uff0c\u4f7f\u5f97\u591a\u4e2a\u4efb\u52a1\u53ef\u4ee5\u540c\u65f6\u8fdb\u884c\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\u8868\u73b0\u51fa\u8272\u3002\u7136\u800c\uff0cPython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501(GIL)\u9650\u5236\u4e86CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u6027\u80fd\u63d0\u5347\uff0c\u56e0\u6b64\u5728\u8ba1\u7b97\u5bc6\u96c6\u578b\u5e94\u7528\u4e2d\uff0c\u591a\u7ebf\u7a0b\u53ef\u80fd\u5e76\u4e0d\u5982\u591a\u8fdb\u7a0b\u6709\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u7ba1\u7406Python\u591a\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\uff1f<\/strong><br \/>\u7ba1\u7406\u591a\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u72b6\u6001\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u542f\u52a8\u548c\u505c\u6b62\u3002\u5229\u7528<code>join()<\/code>\u65b9\u6cd5\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\uff0c\u907f\u514d\u4e3b\u7a0b\u5e8f\u5728\u5b50\u7ebf\u7a0b\u8fd0\u884c\u65f6\u63d0\u524d\u9000\u51fa\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>threading.Event<\/code>\u6765\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u7ebf\u7a0b\u7684\u6267\u884c\u4e0e\u505c\u6b62\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u5b89\u5168\u5730\u7ec8\u6b62\u7ebf\u7a0b\uff0c\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u6709\u6548\u91ca\u653e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u591a\u7ebf\u7a0b\u5982\u4f55\u4e00\u76f4\u8fd0\u884c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u3001\u5faa\u73af\u8fd0\u884c\u3001\u4fe1\u53f7\u5904\u7406\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u5b88\u62a4\u7ebf\u7a0b\u662f\u4e00\u79cd\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u540e [&hellip;]","protected":false},"author":3,"featured_media":1082307,"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\/1082304"}],"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=1082304"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082304\/revisions"}],"predecessor-version":[{"id":1082313,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082304\/revisions\/1082313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082307"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}