{"id":1179783,"date":"2025-01-15T18:28:32","date_gmt":"2025-01-15T10:28:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179783.html"},"modified":"2025-01-15T18:28:40","modified_gmt":"2025-01-15T10:28:40","slug":"python%e7%ba%bf%e7%a8%8b%e5%a6%82%e4%bd%95%e7%9b%b4%e6%8e%a5%e5%85%b3%e9%97%ad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179783.html","title":{"rendered":"python\u7ebf\u7a0b\u5982\u4f55\u76f4\u63a5\u5173\u95ed"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114114\/0c2f2e1f-ff90-467a-b850-23b41b6911fd.webp\" alt=\"python\u7ebf\u7a0b\u5982\u4f55\u76f4\u63a5\u5173\u95ed\" \/><\/p>\n<p><p> <strong>Python\u7ebf\u7a0b\u53ef\u4ee5\u76f4\u63a5\u5173\u95ed\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u3001\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\uff08Daemon threads\uff09\u3001\u4f7f\u7528<code>threading.Timer<\/code>\u5bf9\u8c61\u3001\u5229\u7528\u4fe1\u53f7\u673a\u5236\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61<\/strong>\u662f\u4e00\u79cd\u8f83\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u4e8b\u4ef6\u6807\u5fd7\u6765\u901a\u77e5\u7ebf\u7a0b\u505c\u6b62\u6267\u884c\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u7ebf\u7a0b\u7684\u63a7\u5236\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u65f6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>Event<\/code>\u5bf9\u8c61\uff0c\u5e76\u5c06\u5b83\u4f5c\u4e3a\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf\u4f20\u9012\u7ed9\u7ebf\u7a0b\u51fd\u6570\u3002\u7ebf\u7a0b\u51fd\u6570\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u4e0d\u65ad\u68c0\u67e5\u8fd9\u4e2a<code>Event<\/code>\u5bf9\u8c61\u7684\u72b6\u6001\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u4e8b\u4ef6\u88ab\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u7ebf\u7a0b\u4f1a\u8fdb\u884c\u6e05\u7406\u5de5\u4f5c\u5e76\u9000\u51fa\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f18\u96c5\u5730\u63a7\u5236\u7ebf\u7a0b\u7684\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker(stop_event):<\/p>\n<p>    while not stop_event.is_set():<\/p>\n<p>        print(&quot;Thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    print(&quot;Thread is stopping&quot;)<\/p>\n<p>stop_event = threading.Event()<\/p>\n<p>thread = threading.Thread(target=worker, args=(stop_event,))<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>stop_event.set()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;Thread has been stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c<code>worker<\/code>\u51fd\u6570\u4f1a\u4e0d\u65ad\u68c0\u67e5<code>stop_event<\/code>\u7684\u72b6\u6001\uff0c\u5982\u679c\u68c0\u6d4b\u5230<code>stop_event<\/code>\u88ab\u8bbe\u7f6e\u4e3aTrue\uff0c\u5219\u7ebf\u7a0b\u4f1a\u9000\u51fa\u5faa\u73af\u5e76\u6253\u5370\u201cThread is stopping\u201d\uff0c\u6700\u540e\u4e3b\u7ebf\u7a0b\u8c03\u7528<code>thread.join()<\/code>\u7b49\u5f85\u5b50\u7ebf\u7a0b\u5b8c\u5168\u9000\u51fa\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61<\/h2>\n<\/p>\n<p><h3>1\u3001\u521b\u5efa\u548c\u4f7f\u7528<code>Event<\/code>\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>Event<\/code>\u5bf9\u8c61\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u540c\u6b65\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5728\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u4fe1\u53f7\u3002\u5b83\u63d0\u4f9b\u4e86<code>set()<\/code>\u3001<code>clear()<\/code>\u548c<code>is_set()<\/code>\u7b49\u65b9\u6cd5\uff0c\u7528\u4e8e\u8bbe\u7f6e\u548c\u6e05\u9664\u4e8b\u4ef6\u6807\u5fd7\uff0c\u4ee5\u53ca\u68c0\u67e5\u4e8b\u4ef6\u662f\u5426\u88ab\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker(stop_event):<\/p>\n<p>    while not stop_event.is_set():<\/p>\n<p>        print(&quot;Thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    print(&quot;Thread is stopping&quot;)<\/p>\n<p>stop_event = threading.Event()<\/p>\n<p>thread = threading.Thread(target=worker, args=(stop_event,))<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>stop_event.set()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;Thread has been stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u5728\u521b\u5efa\u5e76\u542f\u52a8\u5b50\u7ebf\u7a0b\u540e\uff0c\u7b49\u5f855\u79d2\u949f\uff0c\u7136\u540e\u8bbe\u7f6e<code>stop_event<\/code>\uff0c\u901a\u77e5\u5b50\u7ebf\u7a0b\u505c\u6b62\u8fd0\u884c\u3002\u5b50\u7ebf\u7a0b\u5728\u68c0\u6d4b\u5230\u4e8b\u4ef6\u88ab\u8bbe\u7f6e\u540e\uff0c\u9000\u51fa\u5faa\u73af\u5e76\u6253\u5370\u201cThread is stopping\u201d\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f18\u96c5\u5173\u95ed\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>Event<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u7ebf\u7a0b\u7684\u4f18\u96c5\u5173\u95ed\u3002\u4e0e\u76f4\u63a5\u5f3a\u5236\u7ec8\u6b62\u7ebf\u7a0b\u7684\u65b9\u6cd5\u76f8\u6bd4\uff0c\u8fd9\u79cd\u65b9\u6cd5\u66f4\u52a0\u5b89\u5168\u548c\u53ef\u63a7\uff0c\u907f\u514d\u4e86\u6f5c\u5728\u7684\u6570\u636e\u635f\u574f\u548c\u8d44\u6e90\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>class WorkerThread(threading.Thread):<\/p>\n<p>    def __init__(self, stop_event):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.stop_event = stop_event<\/p>\n<p>    def run(self):<\/p>\n<p>        while not self.stop_event.is_set():<\/p>\n<p>            print(&quot;Thread is running&quot;)<\/p>\n<p>            time.sleep(1)<\/p>\n<p>        print(&quot;Thread is stopping&quot;)<\/p>\n<p>stop_event = threading.Event()<\/p>\n<p>worker_thread = WorkerThread(stop_event)<\/p>\n<p>worker_thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>stop_event.set()<\/p>\n<p>worker_thread.join()<\/p>\n<p>print(&quot;Thread has been stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7ee7\u627f\u81ea<code>threading.Thread<\/code>\u7684<code>WorkerThread<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199\u4e86\u5b83\u7684<code>run<\/code>\u65b9\u6cd5\u3002\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u521b\u5efa\u5e76\u542f\u52a8<code>WorkerThread<\/code>\u5b9e\u4f8b\u540e\uff0c\u540c\u6837\u7b49\u5f855\u79d2\u949f\uff0c\u7136\u540e\u8bbe\u7f6e<code>stop_event<\/code>\uff0c\u901a\u77e5\u5b50\u7ebf\u7a0b\u505c\u6b62\u8fd0\u884c\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\uff08Daemon threads\uff09<\/h2>\n<\/p>\n<p><h3>1\u3001\u8bbe\u7f6e\u5b88\u62a4\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u5b88\u62a4\u7ebf\u7a0b\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7ebf\u7a0b\uff0c\u5f53\u6240\u6709\u975e\u5b88\u62a4\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u7ed3\u675f\u6240\u6709\u5b88\u62a4\u7ebf\u7a0b\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u7684<code>daemon<\/code>\u5c5e\u6027\u6765\u521b\u5efa\u5b88\u62a4\u7ebf\u7a0b\u3002<\/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;Daemon thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.daemon = True<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>print(&quot;M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n thread is ending&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7ebf\u7a0b\u88ab\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5f53\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u7ed3\u675f\u5b50\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5b88\u62a4\u7ebf\u7a0b\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u5b88\u62a4\u7ebf\u7a0b\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u901a\u77e5\u7ebf\u7a0b\u505c\u6b62\u3002\u7136\u800c\uff0c\u5b83\u7684\u7f3a\u70b9\u662f\u4e0d\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5728\u7ed3\u675f\u524d\u5b8c\u6210\u6240\u6709\u6e05\u7406\u5de5\u4f5c\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u4f1a\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u540e\u7acb\u5373\u7ec8\u6b62\u6240\u6709\u5b88\u62a4\u7ebf\u7a0b\u3002\u56e0\u6b64\uff0c\u5b88\u62a4\u7ebf\u7a0b\u9002\u7528\u4e8e\u90a3\u4e9b\u4e0d\u9700\u8981\u8fdb\u884c\u590d\u6742\u6e05\u7406\u5de5\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528<code>threading.Timer<\/code>\u5bf9\u8c61<\/h2>\n<\/p>\n<p><h3>1\u3001\u521b\u5efa\u548c\u4f7f\u7528<code>Timer<\/code>\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>Timer<\/code>\u5bf9\u8c61\u662f<code>threading<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u7ebf\u7a0b\uff0c\u5b83\u4f1a\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u540e\u6267\u884c\u4e00\u4e2a\u51fd\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>Timer<\/code>\u5bf9\u8c61\u7684<code>cancel()<\/code>\u65b9\u6cd5\u6765\u53d6\u6d88\u5b9a\u65f6\u4efb\u52a1\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u7ebf\u7a0b\u7684\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Timer thread is running&quot;)<\/p>\n<p>timer = threading.Timer(5, worker)<\/p>\n<p>timer.start()<\/p>\n<h2><strong>Cancel the timer before it runs<\/strong><\/h2>\n<p>timer.cancel()<\/p>\n<p>print(&quot;Timer has been cancelled&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u5b9a\u65f6\u5668\u7ebf\u7a0b<code>timer<\/code>\uff0c\u5b83\u4f1a\u57285\u79d2\u949f\u540e\u6267\u884c<code>worker<\/code>\u51fd\u6570\u3002\u7136\u800c\uff0c\u5728\u5b9a\u65f6\u5668\u7ebf\u7a0b\u6267\u884c\u4e4b\u524d\uff0c\u6211\u4eec\u8c03\u7528\u4e86<code>timer.cancel()<\/code>\u65b9\u6cd5\uff0c\u53d6\u6d88\u4e86\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5b9a\u65f6\u4efb\u52a1\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><code>Timer<\/code>\u5bf9\u8c61\u9002\u7528\u4e8e\u90a3\u4e9b\u9700\u8981\u5728\u7279\u5b9a\u65f6\u95f4\u70b9\u6216\u65f6\u95f4\u95f4\u9694\u540e\u6267\u884c\u4efb\u52a1\u7684\u573a\u666f\u3002\u901a\u8fc7<code>Timer<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u548c\u5ef6\u8fdf\u6267\u884c\u529f\u80fd\u3002\u7136\u800c\uff0c\u5982\u679c\u9700\u8981\u9891\u7e41\u53d6\u6d88\u548c\u91cd\u65b0\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\uff0c<code>Timer<\/code>\u5bf9\u8c61\u53ef\u80fd\u4e0d\u592a\u9002\u5408\uff0c\u8fd9\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u5229\u7528\u4fe1\u53f7\u673a\u5236<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528<code>signal<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728Unix\u7c7b\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>signal<\/code>\u6a21\u5757\u6765\u5904\u7406\u4fe1\u53f7\uff0c\u901a\u8fc7\u5411\u7ebf\u7a0b\u53d1\u9001\u7279\u5b9a\u4fe1\u53f7\u6765\u901a\u77e5\u5b83\u505c\u6b62\u8fd0\u884c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython\u4e2d\u53ea\u6709\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u63a5\u6536\u4fe1\u53f7\uff0c\u56e0\u6b64\u9700\u8981\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u5c06\u4fe1\u53f7\u4f20\u9012\u7ed9\u5b50\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import signal<\/p>\n<p>import time<\/p>\n<p>stop_event = threading.Event()<\/p>\n<p>def signal_handler(signum, frame):<\/p>\n<p>    stop_event.set()<\/p>\n<p>signal.signal(signal.SIGINT, signal_handler)<\/p>\n<p>def worker():<\/p>\n<p>    while not stop_event.is_set():<\/p>\n<p>        print(&quot;Thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    print(&quot;Thread is stopping&quot;)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>stop_event.set()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;Thread has been stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4fe1\u53f7\u5904\u7406\u51fd\u6570<code>signal_handler<\/code>\uff0c\u5b83\u4f1a\u5728\u63a5\u6536\u5230<code>SIGINT<\/code>\u4fe1\u53f7\u65f6\u8bbe\u7f6e<code>stop_event<\/code>\uff0c\u901a\u77e5\u7ebf\u7a0b\u505c\u6b62\u8fd0\u884c\u3002\u4e3b\u7ebf\u7a0b\u901a\u8fc7\u8c03\u7528<code>signal.signal()<\/code>\u65b9\u6cd5\u5c06<code>SIGINT<\/code>\u4fe1\u53f7\u7ed1\u5b9a\u5230<code>signal_handler<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4fe1\u53f7\u673a\u5236\u7684\u5c40\u9650\u6027<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4fe1\u53f7\u673a\u5236\u5728Unix\u7c7b\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5728Windows\u7cfb\u7edf\u4e2d\uff0c<code>signal<\/code>\u6a21\u5757\u7684\u529f\u80fd\u6709\u9650\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u4fe1\u53f7\u673a\u5236\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u53ef\u79fb\u690d\u6027\u8f83\u5dee\uff0c\u4e0d\u9002\u7528\u4e8e\u8de8\u5e73\u53f0\u5e94\u7528\u3002\u6b64\u5916\uff0c\u4fe1\u53f7\u673a\u5236\u53ea\u80fd\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u63a5\u6536\u4fe1\u53f7\uff0c\u5bf9\u4e8e\u591a\u7ebf\u7a0b\u5e94\u7528\u7a0b\u5e8f\uff0c\u9700\u8981\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u5c06\u4fe1\u53f7\u4f20\u9012\u7ed9\u5b50\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u7ebf\u7a0b\u6c60\u548c\u4efb\u52a1\u961f\u5217<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code><\/h3>\n<\/p>\n<p><p><code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u7ea7\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u7ebf\u7a0b\u6c60\u548c\u4efb\u52a1\u961f\u5217\u3002\u901a\u8fc7<code>ThreadPoolExecutor<\/code>\u7c7b\uff0c\u53ef\u4ee5\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u6c60\uff0c\u5e76\u63d0\u4ea4\u4efb\u52a1\u5230\u7ebf\u7a0b\u6c60\u4e2d\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>import time<\/p>\n<p>def worker(task_id):<\/p>\n<p>    print(f&quot;Task {task_id} is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Task {task_id} is completed&quot;)<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    future_tasks = [executor.submit(worker, i) for i in range(5)]<\/p>\n<p>for future in concurrent.futures.as_completed(future_tasks):<\/p>\n<p>    future.result()<\/p>\n<p>print(&quot;All tasks are completed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b3\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\uff0c\u5e76\u63d0\u4ea4\u4e865\u4e2a\u4efb\u52a1\u5230\u7ebf\u7a0b\u6c60\u4e2d\u6267\u884c\u3002\u4e3b\u7ebf\u7a0b\u901a\u8fc7<code>as_completed<\/code>\u65b9\u6cd5\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\uff0c\u5e76\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f18\u96c5\u5173\u95ed\u7ebf\u7a0b\u6c60<\/h3>\n<\/p>\n<p><p><code>ThreadPoolExecutor<\/code>\u7c7b\u63d0\u4f9b\u4e86<code>shutdown<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u4f18\u96c5\u5730\u5173\u95ed\u7ebf\u7a0b\u6c60\u3002\u5728\u8c03\u7528<code>shutdown<\/code>\u65b9\u6cd5\u540e\uff0c\u7ebf\u7a0b\u6c60\u4e0d\u518d\u63a5\u53d7\u65b0\u7684\u4efb\u52a1\uff0c\u5e76\u7b49\u5f85\u6240\u6709\u5df2\u63d0\u4ea4\u7684\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>import time<\/p>\n<p>def worker(task_id):<\/p>\n<p>    print(f&quot;Task {task_id} is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Task {task_id} is completed&quot;)<\/p>\n<p>executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)<\/p>\n<p>try:<\/p>\n<p>    future_tasks = [executor.submit(worker, i) for i in range(5)]<\/p>\n<p>    for future in concurrent.futures.as_completed(future_tasks):<\/p>\n<p>        future.result()<\/p>\n<p>finally:<\/p>\n<p>    executor.shutdown(wait=True)<\/p>\n<p>print(&quot;All tasks are completed and thread pool is shutdown&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728<code>try<\/code>\u5757\u4e2d\u63d0\u4ea4\u4efb\u52a1\uff0c\u5e76\u5728<code>finally<\/code>\u5757\u4e2d\u8c03\u7528<code>executor.shutdown(wait=True)<\/code>\u65b9\u6cd5\uff0c\u786e\u4fdd\u7ebf\u7a0b\u6c60\u5728\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u540e\u4f18\u96c5\u5730\u5173\u95ed\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516d\u3001\u7ebf\u7a0b\u7684\u5b89\u5168\u6027\u548c\u540c\u6b65\u673a\u5236<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u9501\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002\u4f7f\u7528\u9501\u673a\u5236\u53ef\u4ee5\u786e\u4fdd\u5728\u67d0\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u4fdd\u8bc1\u7ebf\u7a0b\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>counter = 0<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def increment():<\/p>\n<p>    global counter<\/p>\n<p>    with lock:<\/p>\n<p>        local_counter = counter<\/p>\n<p>        local_counter += 1<\/p>\n<p>        counter = local_counter<\/p>\n<p>threads = [threading.Thread(target=increment) for _ in range(100)]<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>print(f&quot;Final counter value: {counter}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u9501\u5bf9\u8c61<code>lock<\/code>\uff0c\u5e76\u5728<code>increment<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528<code>with lock<\/code>\u8bed\u53e5\u786e\u4fdd\u5bf9\u5171\u4eab\u53d8\u91cf<code>counter<\/code>\u7684\u8bbf\u95ee\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u6761\u4ef6\u53d8\u91cf\u662f\u66f4\u9ad8\u7ea7\u7684\u540c\u6b65\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u7ebf\u7a0b\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u7b49\u5f85\u548c\u901a\u77e5\u5176\u4ed6\u7ebf\u7a0b\u3002\u6761\u4ef6\u53d8\u91cf\u901a\u5e38\u4e0e\u9501\u4e00\u8d77\u4f7f\u7528\uff0c\u901a\u8fc7\u8c03\u7528<code>wait()<\/code>\u548c<code>notify()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u7ebf\u7a0b\u4e4b\u95f4\u7684\u534f\u8c03\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>condition = threading.Condition()<\/p>\n<p>shared_data = []<\/p>\n<p>def producer():<\/p>\n<p>    with condition:<\/p>\n<p>        shared_data.append(1)<\/p>\n<p>        condition.notify()<\/p>\n<p>def consumer():<\/p>\n<p>    with condition:<\/p>\n<p>        while not shared_data:<\/p>\n<p>            condition.wait()<\/p>\n<p>        data = shared_data.pop()<\/p>\n<p>        print(f&quot;Consumed data: {data}&quot;)<\/p>\n<p>producer_thread = threading.Thread(target=producer)<\/p>\n<p>consumer_thread = threading.Thread(target=consumer)<\/p>\n<p>consumer_thread.start()<\/p>\n<p>producer_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\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf<code>condition<\/code>\uff0c\u5e76\u4f7f\u7528\u5b83\u5728\u751f\u4ea7\u8005\u7ebf\u7a0b\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b\u4e4b\u95f4\u8fdb\u884c\u534f\u8c03\u3002\u751f\u4ea7\u8005\u7ebf\u7a0b\u5728\u5411<code>shared_data<\/code>\u4e2d\u6dfb\u52a0\u6570\u636e\u540e\u8c03\u7528<code>condition.notify()<\/code>\u65b9\u6cd5\uff0c\u901a\u77e5\u7b49\u5f85\u7684\u6d88\u8d39\u8005\u7ebf\u7a0b\u3002\u6d88\u8d39\u8005\u7ebf\u7a0b\u5728<code>shared_data<\/code>\u4e3a\u7a7a\u65f6\u8c03\u7528<code>condition.wait()<\/code>\u65b9\u6cd5\uff0c\u7b49\u5f85\u751f\u4ea7\u8005\u7ebf\u7a0b\u7684\u901a\u77e5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e03\u3001\u7ebf\u7a0b\u7684\u8c03\u5ea6\u548c\u4f18\u5148\u7ea7<\/h2>\n<\/p>\n<p><h3>1\u3001\u7ebf\u7a0b\u7684\u8c03\u5ea6<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ebf\u7a0b\u7684\u8c03\u5ea6\u7531\u64cd\u4f5c\u7cfb\u7edf\u8d1f\u8d23\uff0cPython\u89e3\u91ca\u5668\u4e0d\u80fd\u76f4\u63a5\u63a7\u5236\u7ebf\u7a0b\u7684\u8c03\u5ea6\u987a\u5e8f\u3002\u7136\u800c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5408\u7406\u5730\u8bbe\u8ba1\u7ebf\u7a0b\u7684\u6267\u884c\u903b\u8f91\uff0c\u6765\u5f71\u54cd\u7ebf\u7a0b\u7684\u6267\u884c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading.Condition<\/code>\u548c<code>threading.Event<\/code>\u7b49\u540c\u6b65\u673a\u5236\uff0c\u6765\u534f\u8c03\u7ebf\u7a0b\u4e4b\u95f4\u7684\u6267\u884c\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def worker1(event):<\/p>\n<p>    print(&quot;Worker 1 is waiting for event&quot;)<\/p>\n<p>    event.wait()<\/p>\n<p>    print(&quot;Worker 1 is running&quot;)<\/p>\n<p>def worker2(event):<\/p>\n<p>    print(&quot;Worker 2 is setting event&quot;)<\/p>\n<p>    event.set()<\/p>\n<p>event = threading.Event()<\/p>\n<p>thread1 = threading.Thread(target=worker1, args=(event,))<\/p>\n<p>thread2 = threading.Thread(target=worker2, args=(event,))<\/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\u4f8b\u5b50\u4e2d\uff0c<code>worker1<\/code>\u7ebf\u7a0b\u4f1a\u5728<code>event<\/code>\u88ab\u8bbe\u7f6e\u4e4b\u524d\u4e00\u76f4\u7b49\u5f85\uff0c\u800c<code>worker2<\/code>\u7ebf\u7a0b\u4f1a\u5728\u542f\u52a8\u540e\u7acb\u5373\u8bbe\u7f6e<code>event<\/code>\uff0c\u4ece\u800c\u901a\u77e5<code>worker1<\/code>\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u4e2d\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u8bbe\u7f6e\u7ebf\u7a0b\u4f18\u5148\u7ea7\u7684\u529f\u80fd\u3002\u7136\u800c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5408\u7406\u5730\u8bbe\u8ba1\u7ebf\u7a0b\u7684\u6267\u884c\u903b\u8f91\uff0c\u6765\u95f4\u63a5\u5b9e\u73b0\u7ebf\u7a0b\u4f18\u5148\u7ea7\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u6267\u884c\u9891\u7387\uff0c\u6216\u8005\u4f7f\u7528\u961f\u5217\u6765\u7ba1\u7406\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>import queue<\/p>\n<p>task_queue = queue.PriorityQueue()<\/p>\n<p>def worker():<\/p>\n<p>    while not task_queue.empty():<\/p>\n<p>        priority, task = task_queue.get()<\/p>\n<p>        print(f&quot;Executing task with priority {priority}&quot;)<\/p>\n<p>        task()<\/p>\n<p>        task_queue.task_done()<\/p>\n<p>def high_priority_task():<\/p>\n<p>    print(&quot;High priority task is running&quot;)<\/p>\n<p>def low_priority_task():<\/p>\n<p>    print(&quot;Low priority task is running&quot;)<\/p>\n<p>task_queue.put((1, low_priority_task))<\/p>\n<p>task_queue.put((0, high_priority_task))<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>task_queue.join()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>queue.PriorityQueue<\/code>\u6765\u7ba1\u7406\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\uff0c\u5e76\u5728<code>worker<\/code>\u7ebf\u7a0b\u4e2d\u6309\u4f18\u5148\u7ea7\u987a\u5e8f\u6267\u884c\u4efb\u52a1\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u95f4\u63a5\u5b9e\u73b0\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u63a7\u5236\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516b\u3001\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406<\/h2>\n<\/p>\n<p><h3>1\u3001\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa<code>threading.Thread<\/code>\u5bf9\u8c61\u5e76\u8c03\u7528\u5b83\u7684<code>start()<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u3002\u5728\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6\uff0c\u53ef\u4ee5\u8c03\u7528<code>thread.join()<\/code>\u65b9\u6cd5\uff0c\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u6267\u884c\u5e76\u9500\u6bc1\u3002<\/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 is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(&quot;Thread is stopping&quot;)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;Thread has been stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>thread<\/code>\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5b83\u7684<code>start()<\/code>\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\u3002\u4e3b\u7ebf\u7a0b\u901a\u8fc7\u8c03\u7528<code>thread.join()<\/code>\u65b9\u6cd5\u7b49\u5f85\u5b50\u7ebf\u7a0b\u5b8c\u6210\u6267\u884c\u5e76\u9500\u6bc1\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7ebf\u7a0b\u7684\u72b6\u6001\u76d1\u63a7<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>threading.Thread<\/code>\u5bf9\u8c61\u7684<code>is_alive()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u7ebf\u7a0b\u7684\u72b6\u6001\uff0c\u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u4ecd\u5728\u8fd0\u884c\u3002<\/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 is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(&quot;Thread is stopping&quot;)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>while thread.is_alive():<\/p>\n<p>    print(&quot;Waiting for thread to finish&quot;)<\/p>\n<p>    time.sleep(0.5)<\/p>\n<p>print(&quot;Thread has been stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>thread.is_alive()<\/code>\u65b9\u6cd5\u5728\u5faa\u73af\u4e2d\u68c0\u67e5\u7ebf\u7a0b\u7684\u72b6\u6001\uff0c\u5e76\u5728\u5b50\u7ebf\u7a0b\u7ed3\u675f\u540e\u6253\u5370\u201cThread has been stopped\u201d\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e5d\u3001\u7ebf\u7a0b\u7684\u5f02\u5e38\u5904\u7406<\/h2>\n<\/p>\n<p><h3>1\u3001\u6355\u83b7\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u6355\u83b7\u548c\u5904\u7406\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u5728\u7ebf\u7a0b\u51fd\u6570\u4e2d\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u83b7\u53d6\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def worker():<\/p>\n<p>    try:<\/p>\n<p>        raise ValueError(&quot;An error occurred in the thread&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Exception caught in thread: {e}&quot;)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;Thread has been stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728<code>worker<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5728\u5b50\u7ebf\u7a0b\u4e2d\u6253\u5370\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7ebf\u7a0b\u5f02\u5e38\u7684\u4f20\u9012<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u53ef\u80fd\u9700\u8981\u6355\u83b7\u548c\u5904\u7406\u5b50\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38\u3002\u7136\u800c\uff0cPython\u6807\u51c6\u5e93\u4e2d\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u5c06\u5f02\u5e38\u4ece\u5b50\u7ebf\u7a0b\u4f20\u9012\u5230\u4e3b\u7ebf\u7a0b\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u961f\u5217\u6216\u4e8b\u4ef6\u5bf9\u8c61\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>def worker(exception_queue):<\/p>\n<p>    try:<\/p>\n<p>        raise ValueError(&quot;An error occurred in the thread&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        exception_queue.put(e)<\/p>\n<p>exception_queue = queue.Queue()<\/p>\n<p>thread = threading.Thread(target=worker, args=(exception_queue,))<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p>if not exception_queue.empty():<\/p>\n<p>    exception = exception_queue.get()<\/p>\n<p>    print(f&quot;Exception caught in main thread: {exception}&quot;)<\/p>\n<p>print(&quot;Thread has been stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>queue.Queue<\/code>\u5bf9\u8c61\u5728\u5b50\u7ebf\u7a0b\u548c\u4e3b\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u5f02\u5e38\u4fe1\u606f\uff0c\u5e76\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u5341\u3001\u7ebf\u7a0b\u7684\u6027\u80fd\u4f18\u5316<\/h2>\n<\/p>\n<p><h3>1\u3001\u51cf\u5c11\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u5f00\u9500<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u8f83\u5927\uff0c\u53ef\u4ee5\u901a\u8fc7\u91cd\u7528\u7ebf\u7a0b\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6765\u7ba1\u7406\u548c\u91cd\u7528\u7ebf\u7a0b\uff0c\u907f\u514d\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>import time<\/p>\n<p>def worker(task_id):<\/p>\n<p>    print(f&quot;Task {task_id} is running&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Task {task_id} is completed&quot;)<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    future_tasks = [executor.submit(worker, i) for i in range(5)]<\/p>\n<p>for future in concurrent.futures.as_completed(future_tasks):<\/p>\n<p>    future.result()<\/p>\n<p>print(&quot;All tasks are completed<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b89\u5168\u5730\u505c\u6b62\u4e00\u4e2aPython\u7ebf\u7a0b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u76f4\u63a5\u5173\u95ed\u7ebf\u7a0b\u5e76\u4e0d\u662f\u4e00\u79cd\u5b89\u5168\u7684\u505a\u6cd5\u3002\u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u901a\u77e5\u7ebf\u7a0b\u4f55\u65f6\u505c\u6b62\u3002\u4f60\u53ef\u4ee5\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u8ba9\u5de5\u4f5c\u7ebf\u7a0b\u5728\u9002\u5f53\u7684\u65f6\u5019\u68c0\u67e5\u8fd9\u4e2a\u53d8\u91cf\u7684\u72b6\u6001\uff0c\u4ece\u800c\u4f18\u96c5\u5730\u9000\u51fa\u3002<\/p>\n<p><strong>\u7ebf\u7a0b\u7ed3\u675f\u540e\uff0c\u8d44\u6e90\u4f1a\u81ea\u52a8\u91ca\u653e\u5417\uff1f<\/strong><br \/>\u5f53\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0cPython\u4f1a\u81ea\u52a8\u6e05\u7406\u4e0e\u8be5\u7ebf\u7a0b\u76f8\u5173\u7684\u8d44\u6e90\u3002\u4f46\u662f\uff0c\u5982\u679c\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u4e86\u5916\u90e8\u8d44\u6e90\uff08\u5982\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\uff09\uff0c\u5efa\u8bae\u5728\u7ebf\u7a0b\u7ed3\u675f\u524d\u663e\u5f0f\u5173\u95ed\u8fd9\u4e9b\u8d44\u6e90\uff0c\u4ee5\u9632\u6b62\u8d44\u6e90\u6cc4\u9732\u6216\u5176\u4ed6\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728Python\u7ebf\u7a0b\u4e2d\uff0c\u672a\u5904\u7406\u7684\u5f02\u5e38\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u7ec8\u6b62\uff0c\u4f46\u4e0d\u4f1a\u5f71\u54cd\u4e3b\u7ebf\u7a0b\u3002\u4e3a\u4e86\u786e\u4fdd\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38\u5f97\u5230\u59a5\u5584\u5904\u7406\uff0c\u53ef\u4ee5\u5728\u76ee\u6807\u51fd\u6570\u4e2d\u4f7f\u7528try-except\u5757\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u65e5\u5fd7\u8bb0\u5f55\u6216\u6e05\u7406\u64cd\u4f5c\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ebf\u7a0b\u53ef\u4ee5\u76f4\u63a5\u5173\u95ed\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528threading.Event\u5bf9\u8c61\u3001\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\uff08Daemon t [&hellip;]","protected":false},"author":3,"featured_media":1179801,"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\/1179783"}],"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=1179783"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179783\/revisions"}],"predecessor-version":[{"id":1179804,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179783\/revisions\/1179804"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179801"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}