{"id":951658,"date":"2024-12-27T01:08:01","date_gmt":"2024-12-26T17:08:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/951658.html"},"modified":"2024-12-27T01:08:04","modified_gmt":"2024-12-26T17:08:04","slug":"python-%e5%a6%82%e4%bd%95%e7%bb%88%e6%ad%a2%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/951658.html","title":{"rendered":"python \u5982\u4f55\u7ec8\u6b62\u7ebf\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085754\/caee5c32-6250-485f-8d13-0834f3dbbbfc.webp\" alt=\"python \u5982\u4f55\u7ec8\u6b62\u7ebf\u7a0b\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ec8\u6b62\u7ebf\u7a0b\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u7ebf\u7a0b\u6807\u5fd7\u3001\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u3001\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<code>Thread<\/code>\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528\u7ebf\u7a0b\u6807\u5fd7\u7684\u65b9\u5f0f\u6765\u5b89\u5168\u5730\u7ec8\u6b62\u7ebf\u7a0b\uff0c\u56e0\u4e3a\u76f4\u63a5\u5f3a\u5236\u7ec8\u6b62\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u672a\u91ca\u653e\u8d44\u6e90\u3001\u6570\u636e\u4e0d\u4e00\u81f4\u7b49\u95ee\u9898\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u7ebf\u7a0b\u6807\u5fd7<\/p>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6807\u5fd7\u662f\u4e00\u79cd\u6bd4\u8f83\u5b89\u5168\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u7ebf\u7a0b\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u53d8\u91cf\uff0c\u5728\u7ebf\u7a0b\u7684\u5faa\u73af\u4e2d\u4e0d\u65ad\u68c0\u67e5\u8fd9\u4e2a\u6807\u5fd7\u53d8\u91cf\u7684\u503c\uff0c\u5982\u679c\u6807\u5fd7\u53d8\u91cf\u53d8\u4e3aTrue\uff0c\u7ebf\u7a0b\u5c31\u4f1a\u5b89\u5168\u5730\u9000\u51fa\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u7ebf\u7a0b\u6807\u5fd7<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5728\u7ebf\u7a0b\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u53d8\u91cf\uff0c\u5e76\u4e14\u5728\u5faa\u73af\u4e2d\u4e0d\u65ad\u68c0\u67e5\u8fd9\u4e2a\u6807\u5fd7\u53d8\u91cf\u7684\u72b6\u6001\u3002\u5f53\u9700\u8981\u7ec8\u6b62\u7ebf\u7a0b\u65f6\uff0c\u6539\u53d8\u6807\u5fd7\u53d8\u91cf\u7684\u503c\u5373\u53ef\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7ebf\u7a0b\u5728\u5b89\u5168\u7684\u60c5\u51b5\u4e0b\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>class MyThread(threading.Thread):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super(MyThread, self).__init__()<\/p>\n<p>        self._stop_event = threading.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>    def stop(self):<\/p>\n<p>        self._stop_event.set()<\/p>\n<h2><strong>\u4f7f\u7528<\/strong><\/h2>\n<p>thread = MyThread()<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>thread.stop()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>MyThread<\/code>\u7c7b\u6709\u4e00\u4e2a<code>_stop_event<\/code>\u5c5e\u6027\uff0c\u5b83\u662f\u4e00\u4e2a<code>threading.Event<\/code>\u5bf9\u8c61\u3002\u5728\u7ebf\u7a0b\u7684<code>run<\/code>\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528<code>is_set<\/code>\u65b9\u6cd5\u68c0\u67e5\u8fd9\u4e2a\u4e8b\u4ef6\u662f\u5426\u88ab\u8bbe\u7f6e\u3002\u5982\u679c\u662f\uff0c\u5219\u9000\u51fa\u5faa\u73af\uff0c\u4ece\u800c\u5b89\u5168\u5730\u7ec8\u6b62\u7ebf\u7a0b\u3002<code>stop<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bbe\u7f6e\u8fd9\u4e2a\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u6761\u4ef6\u53d8\u91cf\u53ef\u4ee5\u7528\u4e8e\u7ebf\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\uff0c\u5e2e\u52a9\u6211\u4eec\u5728\u67d0\u4e9b\u6761\u4ef6\u4e0b\u7ec8\u6b62\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>class MyThread(threading.Thread):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super(MyThread, self).__init__()<\/p>\n<p>        self.condition = threading.Condition()<\/p>\n<p>        self.running = True<\/p>\n<p>    def run(self):<\/p>\n<p>        while self.running:<\/p>\n<p>            with self.condition:<\/p>\n<p>                self.condition.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t(timeout=1)<\/p>\n<p>                print(&quot;Thread is running...&quot;)<\/p>\n<p>    def stop(self):<\/p>\n<p>        with self.condition:<\/p>\n<p>            self.running = False<\/p>\n<p>            self.condition.notify()<\/p>\n<h2><strong>\u4f7f\u7528<\/strong><\/h2>\n<p>thread = MyThread()<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>thread.stop()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u4e86<code>threading.Condition<\/code>\u5bf9\u8c61\u548c<code>notify<\/code>\u65b9\u6cd5\u6765\u901a\u77e5\u7ebf\u7a0b\u7ec8\u6b62\u3002\u5728<code>run<\/code>\u65b9\u6cd5\u4e2d\uff0c\u901a\u8fc7<code>wait<\/code>\u65b9\u6cd5\u6765\u7b49\u5f85\u7ec8\u6b62\u4fe1\u53f7\uff0c<code>stop<\/code>\u65b9\u6cd5\u4e2d\u901a\u8fc7\u8bbe\u7f6e<code>running<\/code>\u4e3a<code>False<\/code>\u5e76\u8c03\u7528<code>notify<\/code>\u65b9\u6cd5\u6765\u7ec8\u6b62\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code><\/p>\n<\/p>\n<p><p><code>ThreadPoolExecutor<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u7ba1\u7406\u7ebf\u7a0b\u6c60\u3002\u901a\u8fc7\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u7ba1\u7406\u7ebf\u7a0b<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u591a\u4e2a\u7ebf\u7a0b\u7684\u6267\u884c\u548c\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>import time<\/p>\n<p>def task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Thread is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u4f7f\u7528<\/strong><\/h2>\n<p>executor = ThreadPoolExecutor(max_workers=1)<\/p>\n<p>future = executor.submit(task)<\/p>\n<p>time.sleep(5)<\/p>\n<p>executor.shutdown(wait=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ThreadPoolExecutor<\/code>\u88ab\u7528\u6765\u7ba1\u7406\u7ebf\u7a0b\u3002\u901a\u8fc7<code>submit<\/code>\u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\uff0c\u5e76\u4f7f\u7528<code>shutdown<\/code>\u65b9\u6cd5\u6765\u7ec8\u6b62\u7ebf\u7a0b\u6c60\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<code>Thread<\/code><\/p>\n<\/p>\n<p><p><code>Thread<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u652f\u6301\u66f4\u9ad8\u7ea7\u7684\u7ebf\u7a0b\u63a7\u5236\u529f\u80fd\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u5b89\u5168\u548c\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u7ec8\u6b62\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>Thread<\/code>\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>Thread<\/code>\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u7b80\u4fbf\u5730\u63a7\u5236\u7ebf\u7a0b\u7684\u5f00\u59cb\u548c\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from thread import Thread<\/p>\n<p>import time<\/p>\n<p>def task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Thread is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u4f7f\u7528<\/strong><\/h2>\n<p>thread = Thread(target=task)<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>thread.stop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Thread<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684<code>stop<\/code>\u65b9\u6cd5\u6765\u7ec8\u6b62\u7ebf\u7a0b\u3002\u901a\u8fc7\u8c03\u7528<code>stop<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b89\u5168\u5730\u7ec8\u6b62\u7ebf\u7a0b\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ec8\u6b62\u7ebf\u7a0b\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7ebf\u7a0b\u6807\u5fd7\u3001<code>threading.Event<\/code>\u5bf9\u8c61\u3001<code>concurrent.futures.ThreadPoolExecutor<\/code>\u548c\u7b2c\u4e09\u65b9\u5e93<code>Thread<\/code>\u7b49\u3002<strong>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528\u7ebf\u7a0b\u6807\u5fd7\u7684\u65b9\u5f0f\u6765\u5b89\u5168\u5730\u7ec8\u6b62\u7ebf\u7a0b\u3002<\/strong>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5728\u5faa\u73af\u4e2d\u68c0\u67e5\u6807\u5fd7\u53d8\u91cf\u7684\u72b6\u6001\uff0c\u786e\u4fdd\u7ebf\u7a0b\u5728\u5b89\u5168\u7684\u60c5\u51b5\u4e0b\u7ec8\u6b62\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u548c\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684\u7ebf\u7a0b\u7ec8\u6b62\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b89\u5168\u5730\u505c\u6b62\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\uff1f<\/strong><br \/>\u505c\u6b62\u4e00\u4e2a\u7ebf\u7a0b\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4f7f\u7528\u6807\u5fd7\u4f4d\u3002\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u6807\u5fd7\uff0c\u8ba9\u7ebf\u7a0b\u5728\u6267\u884c\u5faa\u73af\u65f6\u68c0\u67e5\u8fd9\u4e2a\u6807\u5fd7\u3002\u5982\u679c\u6807\u5fd7\u88ab\u8bbe\u7f6e\u4e3aTrue\uff0c\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u5b89\u5168\u9000\u51fa\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7ebf\u7a0b\u80fd\u591f\u5e72\u51c0\u5229\u843d\u5730\u7ed3\u675f\uff0c\u800c\u4e0d\u4f1a\u9020\u6210\u8d44\u6e90\u6cc4\u9732\u6216\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/p>\n<p><strong>\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>join()<\/code>\u65b9\u6cd5\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u3002\u542f\u52a8\u7ebf\u7a0b\u540e\uff0c\u53ef\u4ee5\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8c03\u7528<code>join()<\/code>\uff0c\u8fd9\u6837\u4e3b\u7ebf\u7a0b\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u76ee\u6807\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\uff08daemon thread\uff09\u4e5f\u662f\u4e00\u79cd\u65b9\u6cd5\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u5728\u4e3b\u7a0b\u5e8f\u7ed3\u675f\u65f6\u81ea\u52a8\u7ec8\u6b62\u3002<\/p>\n<p><strong>\u5982\u679c\u76f4\u63a5\u4f7f\u7528<code>Thread<\/code>\u7684<code>_stop()<\/code>\u65b9\u6cd5\uff0c\u53ef\u80fd\u4f1a\u6709\u4ec0\u4e48\u98ce\u9669\uff1f<\/strong><br \/>\u76f4\u63a5\u8c03\u7528<code>_stop()<\/code>\u65b9\u6cd5\u662f\u4e0d\u63a8\u8350\u7684\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u7acb\u5373\u505c\u6b62\uff0c\u800c\u4e0d\u8fdb\u884c\u4efb\u4f55\u6e05\u7406\u5de5\u4f5c\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u80fd\u4f1a\u5f15\u53d1\u8d44\u6e90\u6cc4\u9732\u3001\u9501\u672a\u91ca\u653e\u7b49\u95ee\u9898\uff0c\u8fdb\u800c\u5f71\u54cd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002\u56e0\u6b64\uff0c\u5b89\u5168\u7684\u7ec8\u6b62\u7ebf\u7a0b\u65b9\u5f0f\u5e94\u59cb\u7ec8\u4f18\u5148\u8003\u8651\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ec8\u6b62\u7ebf\u7a0b\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u7ebf\u7a0b\u6807\u5fd7\u3001\u4f7f\u7528threading.Event\u5bf9\u8c61\u3001\u4f7f\u7528concurren [&hellip;]","protected":false},"author":3,"featured_media":951660,"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\/951658"}],"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=951658"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951658\/revisions"}],"predecessor-version":[{"id":951661,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951658\/revisions\/951661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/951660"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=951658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=951658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=951658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}