{"id":271921,"date":"2024-05-16T13:04:45","date_gmt":"2024-05-16T05:04:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/271921.html"},"modified":"2024-05-16T13:04:53","modified_gmt":"2024-05-16T05:04:53","slug":"python3-%e9%a1%b9%e7%9b%ae%e4%b8%bb%e7%ba%bf%e7%a8%8b%e5%a6%82%e4%bd%95%e9%94%80%e6%af%81%e5%ad%90%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/271921.html","title":{"rendered":"Python3 \u9879\u76ee\u4e3b\u7ebf\u7a0b\u5982\u4f55\u9500\u6bc1\u5b50\u7ebf\u7a0b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194526\/04ba159c-18a3-46dd-8f64-9a3d29b0da55.webp\" alt=\"Python3 \u9879\u76ee\u4e3b\u7ebf\u7a0b\u5982\u4f55\u9500\u6bc1\u5b50\u7ebf\u7a0b\" \/><\/p>\n<p><p><strong>Python3\u4e2d\uff0c\u9879\u76ee\u4e3b\u7ebf\u7a0b\u65e0\u6cd5\u76f4\u63a5\u9500\u6bc1\u5b50\u7ebf\u7a0b\u3001\u9700\u8981\u4f7f\u7528\u5b50\u7ebf\u7a0b\u7684<code>daemon<\/code>\u5c5e\u6027\u6216\u8005\u8bbe\u7f6e\u7ebf\u7a0b\u7ec8\u6b62\u7684\u6807\u5fd7\u4f4d\u6765\u95f4\u63a5\u63a7\u5236\u5b50\u7ebf\u7a0b\u7684\u7ec8\u6b62\u3002<\/strong> \u5b50\u7ebf\u7a0b\u7684<code>daemon<\/code>\u5c5e\u6027\u4e3aTrue\u65f6\uff0c\u8868\u793a\u8be5\u7ebf\u7a0b\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u4f1a\u968f\u4e4b\u7ed3\u675f\u3002\u7136\u800c\uff0c\u5373\u4f7f\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u4e5f\u4e0d\u4f1a\u7acb\u5373\u7ed3\u675f\uff0c\u800c\u662f\u4f1a\u5728\u8fd0\u884c\u5230\u4e0b\u4e00\u4e2a\u8c03\u5ea6\u70b9\u65f6\u624d\u968f\u7740\u4e3b\u7ebf\u7a0b\u4e00\u8d77\u9000\u51fa\u3002\u4e3a\u4e86\u5b89\u5168\u5730\u7ed3\u675f\u5b50\u7ebf\u7a0b\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u4e2a\u6807\u5fd7\u4f4d\uff0c\u5728\u5b50\u7ebf\u7a0b\u4e2d\u68c0\u67e5\u8fd9\u4e2a\u6807\u5fd7\u4f4d\u51b3\u5b9a\u662f\u5426\u9000\u51fa\u3002\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u5728\u5b50\u7ebf\u7a0b\u6267\u884c\u7684\u4efb\u52a1\u4e2d\u52a0\u5165\u68c0\u67e5\u903b\u8f91\uff0c\u4fdd\u8bc1\u5b50\u7ebf\u7a0b\u80fd\u591f\u53ca\u65f6\u54cd\u5e94\u6807\u5fd7\u4f4d\u7684\u6539\u53d8\uff0c\u4ece\u800c\u4f18\u96c5\u5730\u5173\u95ed\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u5b88\u62a4\u7ebf\u7a0b\uff08Daemon Thread\uff09\u662f\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5728\u540e\u53f0\u63d0\u4f9b\u4e00\u79cd\u7279\u6b8a\u670d\u52a1\u7684\u7ebf\u7a0b\uff0c\u5176\u751f\u547d\u5468\u671f\u4f9d\u8d56\u4e8e\u4e3b\u7ebf\u7a0b\u3002\u5f53\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e0d\u8bba\u5b8c\u6210\u4e0e\u5426\u4e5f\u4f1a\u968f\u4e4b\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u5b88\u62a4\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def daemon_thread():<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(0.5)<\/p>\n<p>        print(&quot;Daemon thread running.&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u7ebf\u7a0b\uff0c\u5e76\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b<\/strong><\/h2>\n<p>d_thread = threading.Thread(target=daemon_thread)<\/p>\n<p>d_thread.daemon = True<\/p>\n<p>d_thread.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7684\u5176\u4ed6\u5de5\u4f5c<\/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(2)<\/p>\n<p>print(&quot;Main thread is terminating.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7ebf\u7a0b\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u7684\u51fd\u6570\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u8bbe\u7f6e\u4e86<code>daemon=True<\/code>\uff0c\u5f53\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u540e2\u79d2\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4e5f\u4f1a\u7acb\u5373\u7ec8\u6b62\uff0c\u4e0d\u4f1a\u65e0\u9650\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u7ebf\u7a0b\u7ec8\u6b62\u6807\u5fd7<\/h3>\n<\/p>\n<p><p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5b50\u7ebf\u7a0b\u5728\u672a\u5b8c\u6210\u4efb\u52a1\u7684\u60c5\u51b5\u4e0b\u5c31\u5f3a\u5236\u7ec8\u6b62\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u7ec8\u6b62\u7684\u6807\u5fd7\u4f4d\u6765\u4f18\u96c5\u5730\u7ed3\u675f\u5b50\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>\u6dfb\u52a0\u7ec8\u6b62\u6807\u5fd7<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker(thread_stop_event):<\/p>\n<p>    while not thread_stop_event.is_set():<\/p>\n<p>        print(&quot;Worker thread is running.&quot;)<\/p>\n<p>        time.sleep(0.5)<\/p>\n<h2><strong>\u7528\u4e8e\u505c\u6b62\u7ebf\u7a0b\u7684\u4e8b\u4ef6<\/strong><\/h2>\n<p>thread_stop_event = threading.Event()<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u5b50\u7ebf\u7a0b<\/strong><\/h2>\n<p>t = threading.Thread(target=worker, args=(thread_stop_event,))<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u5176\u4ed6\u5de5\u4f5c<\/strong><\/h2>\n<p>time.sleep(2)<\/p>\n<h2><strong>\u901a\u77e5\u5b50\u7ebf\u7a0b\u7ec8\u6b62<\/strong><\/h2>\n<p>thread_stop_event.set()<\/p>\n<p>t.join() # \u7b49\u5f85\u5b50\u7ebf\u7a0b\u7ed3\u675f<\/p>\n<p>print(&quot;Worker thread has been terminated.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2aEvent\u5bf9\u8c61\u7ed9\u5b50\u7ebf\u7a0b\u3002\u5f53\u6211\u4eec\u8bbe\u7f6e\u4e86\u8fd9\u4e2aEvent\u65f6\uff0c\u5b50\u7ebf\u7a0b\u5c06\u901a\u8fc7\u68c0\u67e5\u8fd9\u4e2aEvent\u662f\u5426\u88ab\u8bbe\u7f6e\u6765\u51b3\u5b9a\u662f\u5426\u7ec8\u6b62\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7efc\u5408\u8003\u8651\u5b50\u7ebf\u7a0b\u7684\u9000\u51fa<\/h3>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u5b50\u7ebf\u7a0b\u7684\u6267\u884c\u903b\u8f91\u65f6\uff0c\u5efa\u8bae\u7efc\u5408\u8003\u8651\u5b50\u7ebf\u7a0b\u7684\u8fd0\u884c\u7b56\u7565\u548c\u8d44\u6e90\u91ca\u653e\u60c5\u51b5\u3002\u5b50\u7ebf\u7a0b\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u6d89\u53ca\u5230\u8d44\u6e90\u9501\u5b9a\u3001\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u60c5\u51b5\uff0c\u8fd9\u5c31\u9700\u8981\u5728\u7ed3\u675f\u5b50\u7ebf\u7a0b\u524d\u786e\u4fdd\u8fd9\u4e9b\u64cd\u4f5c\u4e0d\u4f1a\u56e0\u4e3a\u7ebf\u7a0b\u7684\u975e\u6b63\u5e38\u9000\u51fa\u800c\u7559\u4e0b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u5168\u9000\u51fa\u5b50\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_worker(thread_stop_event, resource_lock):<\/p>\n<p>    # \u4e00\u4e9b\u521d\u59cb\u5316\u64cd\u4f5c...<\/p>\n<p>    try:<\/p>\n<p>        while not thread_stop_event.is_set():<\/p>\n<p>            with resource_lock: # \u83b7\u53d6\u8d44\u6e90\u9501<\/p>\n<p>                # \u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4f8b\u5982\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90<\/p>\n<p>                pass<\/p>\n<p>            # \u53ef\u80fd\u7684\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>            time.sleep(0.5)<\/p>\n<p>    finally:<\/p>\n<p>        # \u91ca\u653e\u8d44\u6e90\u3001\u6e05\u7406\u5de5\u4f5c<\/p>\n<p>        pass<\/p>\n<h2><strong>\u521b\u5efa\u9501\u548c\u4e8b\u4ef6<\/strong><\/h2>\n<p>res_lock = threading.Lock()<\/p>\n<p>thread_stop_event = threading.Event()<\/p>\n<h2><strong>\u542f\u52a8\u5b50\u7ebf\u7a0b<\/strong><\/h2>\n<p>t = threading.Thread(target=safe_worker, args=(thread_stop_event, res_lock))<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u5728\u9002\u5f53\u7684\u65f6\u5019\u7ed3\u675f\u5b50\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread_stop_event.set()<\/p>\n<p>t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9664\u4e86\u4f7f\u7528\u4e86Event\u4ee5\u5916\uff0c\u8fd8\u5f15\u5165\u4e86Lock\u6765\u4fdd\u8bc1\u8d44\u6e90\u7684\u5b89\u5168\u8bbf\u95ee\uff0c\u5e76\u5728<code>try...finally...<\/code>\u5757\u4e2d\u6dfb\u52a0\u4e86\u6e05\u7406\u4ee3\u7801\uff0c\u4fdd\u8bc1\u4e86\u5373\u4f7f\u5728\u5f02\u5e38\u60c5\u51b5\u4e0b\u5b50\u7ebf\u7a0b\u4e5f\u80fd\u91ca\u653e\u5176\u6301\u6709\u7684\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8003\u8651\u7ebf\u7a0b\u540c\u6b65\u548c\u7ade\u6001\u6761\u4ef6<\/h3>\n<\/p>\n<p><p>\u8bbe\u8ba1\u7ebf\u7a0b\u7ec8\u6b62\u7b56\u7565\u65f6\uff0c\u8fd8\u9700\u8981\u7279\u522b\u6ce8\u610f\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898\u3002\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u548c\u4fee\u6539\u5171\u4eab\u6570\u636e\uff0c\u5c31\u53ef\u80fd\u53d1\u751f\u7ade\u6001\u6761\u4ef6\uff0c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u7ade\u6001\u6761\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def thread_task(thread_stop_event, shared_data, data_lock):<\/p>\n<p>    while not thread_stop_event.is_set():<\/p>\n<p>        with data_lock:<\/p>\n<p>            # \u5b89\u5168\u5730\u64cd\u4f5c\u5171\u4eab\u6570\u636e<\/p>\n<p>            shared_data[&#039;count&#039;] += 1<\/p>\n<p>        # \u5176\u4ed6\u64cd\u4f5c...<\/p>\n<p>        time.sleep(0.1)<\/p>\n<p>shared_dict = {&#039;count&#039;: 0}<\/p>\n<p>data_lock = threading.Lock()<\/p>\n<p>thread_stop_event = threading.Event()<\/p>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    t = threading.Thread(target=thread_task, args=(thread_stop_event, shared_dict, data_lock))<\/p>\n<p>    t.start()<\/p>\n<p>    threads.append(t)<\/p>\n<h2><strong>\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c<\/strong><\/h2>\n<p>time.sleep(2)<\/p>\n<h2><strong>\u901a\u77e5\u6240\u6709\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>thread_stop_event.set()<\/p>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<h2><strong>\u5b89\u5168\u5730\u8bbf\u95ee\u5171\u4eab\u6570\u636e<\/strong><\/h2>\n<p>with data_lock:<\/p>\n<p>    print(&quot;Final counter value:&quot;, shared_dict[&#039;count&#039;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u591a\u4e2a\u7ebf\u7a0b\u5171\u540c\u64cd\u4f5c\u4e00\u4e2a\u5171\u4eab\u5b57\u5178\uff0c\u5e76\u4f7f\u7528\u9501\u6765\u907f\u514d\u7ade\u6001\u6761\u4ef6\uff0c\u786e\u4fdd\u5373\u4f7f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u4e5f\u80fd\u6b63\u786e\u66f4\u65b0\u5171\u4eab\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u7684\u7ebf\u7a0b\u4e00\u65e6\u542f\u52a8\uff0c\u5c31\u6ca1\u6709\u5b98\u65b9\u76f4\u63a5\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u5f3a\u884c\u7ec8\u6b62\u5b83\u4eec\u3002\u6211\u4eec\u901a\u5e38\u901a\u8fc7\u5b88\u62a4\u7ebf\u7a0b\u6216\u8005\u7ec8\u6b62\u6807\u5fd7\u7684\u65b9\u5f0f\uff0c\u914d\u5408\u9002\u5f53\u7684\u540c\u6b65\u63aa\u65bd\uff0c\u4ee5\u5b89\u5168\u3001\u53ef\u63a7\u7684\u6d41\u7a0b\u7ed3\u675f\u5b50\u7ebf\u7a0b\u7684\u6267\u884c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u6709\u52a9\u4e8e\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<ol>\n<li>\n<p><strong>\u5982\u4f55\u505c\u6b62Python3\u9879\u76ee\u4e2d\u6b63\u5728\u8fd0\u884c\u7684\u5b50\u7ebf\u7a0b\uff1f<\/strong><br \/>\n\u5f53\u6211\u4eec\u5728Python\u9879\u76ee\u4e2d\u521b\u5efa\u4e86\u5b50\u7ebf\u7a0b\u65f6\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5728\u9002\u5f53\u7684\u65f6\u5019\u505c\u6b62\u5b83\u4eec\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u6216\u5176\u4ed6\u95ee\u9898\u3002\u8981\u505c\u6b62\u5b50\u7ebf\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u548c\u4e00\u4e2a\u6807\u8bb0\u53d8\u91cf\u6765\u534f\u8c03\u5b50\u7ebf\u7a0b\u7684\u6267\u884c\u3002\u5f53\u6807\u8bb0\u53d8\u91cf\u8bbe\u7f6e\u4e3aTrue\u65f6\uff0c\u5b50\u7ebf\u7a0b\u4f1a\u77e5\u9053\u505c\u6b62\u8fd0\u884c\u5e76\u9000\u51fa\u3002\u60a8\u53ef\u4ee5\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8bbe\u7f6e\u6807\u8bb0\u53d8\u91cf\u4e3aTrue\uff0c\u7136\u540e\u5728\u5b50\u7ebf\u7a0b\u4e2d\u68c0\u67e5\u6807\u8bb0\u53d8\u91cf\u7684\u72b6\u6001\uff0c\u5982\u679c\u4e3aTrue\uff0c\u5219\u9000\u51fa\u5b50\u7ebf\u7a0b\u7684\u6267\u884c\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6709\u6ca1\u6709\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u65b9\u6cd5\u6765\u9500\u6bc1Python3\u9879\u76ee\u4e2d\u7684\u5b50\u7ebf\u7a0b\uff1f<\/strong><br \/>\n\u9664\u4e86\u4f7f\u7528\u6807\u8bb0\u53d8\u91cf\u6765\u505c\u6b62\u5b50\u7ebf\u7a0b\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>threading.Thread<\/code>\u7684<code>join()<\/code>\u65b9\u6cd5\u6765\u7b49\u5f85\u5b50\u7ebf\u7a0b\u5b8c\u6210\u6267\u884c\u3002\u5f53\u8c03\u7528\u8be5\u65b9\u6cd5\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u5728\u5b50\u7ebf\u7a0b\u5b8c\u6210\u540e\u7ee7\u7eed\u6267\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b\u76f4\u5230\u5b50\u7ebf\u7a0b\u5b8c\u6210\uff0c\u56e0\u6b64\u53ef\u4ee5\u4fdd\u8bc1\u5b50\u7ebf\u7a0b\u7684\u6b63\u5e38\u9000\u51fa\u3002\u60a8\u53ef\u4ee5\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8c03\u7528<code>join()<\/code>\u65b9\u6cd5\u6765\u7b49\u5f85\u6240\u6709\u5b50\u7ebf\u7a0b\u5b8c\u6210\uff0c\u4ece\u800c\u4f18\u96c5\u5730\u9500\u6bc1\u5b50\u7ebf\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Python3\u9879\u76ee\u4e2d\u5982\u4f55\u5904\u7406\u5b50\u7ebf\u7a0b\u629b\u51fa\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\n\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u5b50\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u4f46\u662f\u8fd9\u4e9b\u5f02\u5e38\u4e0d\u4f1a\u4f20\u9012\u5230\u4e3b\u7ebf\u7a0b\u3002\u4e3a\u4e86\u5904\u7406\u5b50\u7ebf\u7a0b\u7684\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u5757\u6765\u6355\u83b7\u5b50\u7ebf\u7a0b\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002\u5728\u5b50\u7ebf\u7a0b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u5757\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u7136\u540e\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u5c06\u5f02\u5e38\u4f20\u9012\u7ed9\u4e3b\u7ebf\u7a0b\u3002\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5<code>Event<\/code>\u5bf9\u8c61\u7684\u72b6\u6001\u6765\u83b7\u53d6\u5b50\u7ebf\u7a0b\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u5e76\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u5b50\u7ebf\u7a0b\u51fa\u73b0\u5f02\u5e38\u65f6\uff0c\u6211\u4eec\u80fd\u591f\u53ca\u65f6\u5730\u6355\u83b7\u5e76\u5904\u7406\u5b83\u4eec\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"Python3\u4e2d\uff0c\u9879\u76ee\u4e3b\u7ebf\u7a0b\u65e0\u6cd5\u76f4\u63a5\u9500\u6bc1\u5b50\u7ebf\u7a0b\u3001\u9700\u8981\u4f7f\u7528\u5b50\u7ebf\u7a0b\u7684daemon\u5c5e\u6027\u6216\u8005\u8bbe\u7f6e\u7ebf\u7a0b\u7ec8\u6b62\u7684\u6807\u5fd7\u4f4d\u6765\u95f4\u63a5 [&hellip;]","protected":false},"author":3,"featured_media":271932,"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\/271921"}],"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=271921"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/271921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/271932"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=271921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=271921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=271921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}