{"id":978248,"date":"2024-12-27T06:36:12","date_gmt":"2024-12-26T22:36:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/978248.html"},"modified":"2024-12-27T06:36:14","modified_gmt":"2024-12-26T22:36:14","slug":"python%e4%b8%adthreading%e5%a6%82%e4%bd%95%e7%bb%88%e6%ad%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/978248.html","title":{"rendered":"python\u4e2dthreading\u5982\u4f55\u7ec8\u6b62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203332\/bfe5e080-6a75-4a27-ab46-dc5b42383843.webp\" alt=\"python\u4e2dthreading\u5982\u4f55\u7ec8\u6b62\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u7ec8\u6b62\u7ebf\u7a0b\u5e76\u4e0d\u662f\u4e00\u4ef6\u7b80\u5355\u7684\u4e8b\u60c5\uff0c\u56e0\u4e3aPython\u7684<code>threading<\/code>\u6a21\u5757\u5e76\u6ca1\u6709\u63d0\u4f9b\u4e00\u4e2a\u76f4\u63a5\u7684\u65b9\u6cd5\u6765\u7ec8\u6b62\u7ebf\u7a0b\u3002\u4e0d\u8fc7\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u7ebf\u7a0b\u7684\u7ec8\u6b62\uff1a<strong>\u4f7f\u7528\u6807\u5fd7\u4f4d\u6765\u63a7\u5236\u7ebf\u7a0b\u3001\u5229\u7528\u7ebf\u7a0b\u7684\u5b88\u62a4\u6a21\u5f0f\u3001\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u6216\u7b2c\u4e09\u65b9\u5e93\u3001\u8bbe\u7f6e\u8d85\u65f6<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5b9e\u73b0\u7ebf\u7a0b\u7684\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u6807\u5fd7\u4f4d\u6765\u63a7\u5236\u7ebf\u7a0b<\/p>\n<\/p>\n<p><p>\u6807\u5fd7\u4f4d\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u7ebf\u7a0b\u63a7\u5236\u3002\u901a\u8fc7\u5728\u7ebf\u7a0b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u6807\u5fd7\u53d8\u91cf\uff0c\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u8be5\u53d8\u91cf\u6765\u63a7\u5236\u5b50\u7ebf\u7a0b\u7684\u8fd0\u884c\u548c\u505c\u6b62\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u6807\u5fd7\u4f4d<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u542f\u52a8\u7ebf\u7a0b\u4e4b\u524d\uff0c\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u7684\u6807\u5fd7\u53d8\u91cf\uff0c\u4f8b\u5982\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u6216\u901a\u8fc7<code>threading.Event()<\/code>\u5bf9\u8c61\u3002<code>threading.Event()<\/code>\u662f\u7ebf\u7a0b\u4e4b\u95f4\u7684\u4e00\u79cd\u901a\u4fe1\u673a\u5236\uff0c\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u542f\u52a8\u548c\u505c\u6b62\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5728\u7ebf\u7a0b\u4e2d\u68c0\u67e5\u6807\u5fd7\u4f4d<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5b50\u7ebf\u7a0b\u7684\u8fd0\u884c\u5faa\u73af\u4e2d\uff0c\u5b9a\u671f\u68c0\u67e5\u6807\u5fd7\u4f4d\u3002\u5982\u679c\u6807\u5fd7\u4f4d\u88ab\u8bbe\u7f6e\u4e3a\u771f\uff0c\u5219\u7ebf\u7a0b\u5e94\u8be5\u7ed3\u675f\u5176\u8fd0\u884c\uff0c\u9000\u51fa\u5faa\u73af\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<h2><strong>\u4e3b\u7ebf\u7a0b\u4f11\u7720 5 \u79d2\u540e\u505c\u6b62\u5b50\u7ebf\u7a0b<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<p>stop_event.set()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>stop_event<\/code>\u662f\u4e00\u4e2a\u7ebf\u7a0b\u4e8b\u4ef6\u5bf9\u8c61\uff0c\u5b50\u7ebf\u7a0b\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u68c0\u67e5\u8fd9\u4e2a\u4e8b\u4ef6\u5bf9\u8c61\u662f\u5426\u88ab\u8bbe\u7f6e\uff0c\u4ee5\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528\u7ebf\u7a0b\u7684\u5b88\u62a4\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>\u5b88\u62a4\u7ebf\u7a0b\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7ebf\u7a0b\uff0c\u5b83\u4f1a\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u81ea\u52a8\u7ed3\u675f\u3002\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u907f\u514d\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5b50\u7ebf\u7a0b\u7ed3\u675f\u800c\u957f\u65f6\u95f4\u963b\u585e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u5b88\u62a4\u7ebf\u7a0b<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5728\u521b\u5efa\u7ebf\u7a0b\u65f6\uff0c\u901a\u8fc7\u8bbe\u7f6e<code>daemon<\/code>\u5c5e\u6027\u4e3a<code>True<\/code>\u6765\u5c06\u5176\u8bbe\u7f6e\u4e3a\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<h2><strong>\u4e3b\u7ebf\u7a0b\u4f11\u7720 3 \u79d2\u540e\u7ed3\u675f<\/strong><\/h2>\n<p>time.sleep(3)<\/p>\n<p>print(&quot;M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n thread is exiting&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7ebf\u7a0b\u662f\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5f53\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u5b50\u7ebf\u7a0b\u4e5f\u4f1a\u81ea\u52a8\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u6216\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>Python\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u522b\u7684\u63a5\u53e3\u6765\u7ba1\u7406\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u3002\u901a\u8fc7\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u7ebf\u7a0b\u7684\u542f\u52a8\u548c\u505c\u6b62\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><code>ThreadPoolExecutor<\/code>\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u63d0\u4ea4\u4efb\u52a1\u6765\u7ba1\u7406\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u53d6\u6d88\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor, as_completed<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>with ThreadPoolExecutor(max_workers=1) as executor:<\/p>\n<p>    future = executor.submit(worker)<\/p>\n<p>    time.sleep(5)<\/p>\n<p>    future.cancel()  # \u53d6\u6d88\u4efb\u52a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>future.cancel()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c1d\u8bd5\u53d6\u6d88\u4efb\u52a1\uff0c\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>cancel()<\/code>\u53ea\u6709\u5728\u4efb\u52a1\u5c1a\u672a\u5f00\u59cb\u65f6\u624d\u80fd\u6210\u529f\u53d6\u6d88\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u7ebf\u7a0b\u7ba1\u7406\u529f\u80fd\uff0c\u4f8b\u5982<code>thread2<\/code>\u5e93\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u66f4\u65b9\u4fbf\u5730\u63a7\u5236\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8bbe\u7f6e\u8d85\u65f6<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u8d85\u65f6\u6765\u95f4\u63a5\u5b9e\u73b0\u7ebf\u7a0b\u7684\u7ec8\u6b62\u3002\u8fd9\u9002\u7528\u4e8e\u90a3\u4e9b\u6709\u660e\u786e\u7684\u4efb\u52a1\u7ed3\u675f\u6761\u4ef6\u548c\u8d85\u65f6\u65f6\u95f4\u7684\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u7ebf\u7a0b\u8d85\u65f6<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u7a0b\u4efb\u52a1\u4e2d\u5b9e\u73b0\u8d85\u65f6\u673a\u5236\uff0c\u4f7f\u7ebf\u7a0b\u5728\u8d85\u8fc7\u4e00\u5b9a\u65f6\u95f4\u540e\u81ea\u52a8\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker(timeout):<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    while time.time() - start_time &lt; timeout:<\/p>\n<p>        print(&quot;Thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    print(&quot;Thread is stopping due to timeout&quot;)<\/p>\n<p>thread = threading.Thread(target=worker, args=(5,))<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7ebf\u7a0b\u57285\u79d2\u540e\u4f1a\u81ea\u52a8\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u4e2d\u6ca1\u6709\u76f4\u63a5\u7ec8\u6b62\u7ebf\u7a0b\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7<strong>\u6807\u5fd7\u4f4d\u3001\u5b88\u62a4\u7ebf\u7a0b\u3001<code>concurrent.futures<\/code>\u6a21\u5757\u548c\u8d85\u65f6\u673a\u5236<\/strong>\u6765\u5b9e\u73b0\u7ebf\u7a0b\u7684\u63a7\u5236\u548c\u7ec8\u6b62\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f18\u96c5\u5730\u7ec8\u6b62\u4e00\u4e2aPython\u7ebf\u7a0b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f18\u96c5\u5730\u7ec8\u6b62\u7ebf\u7a0b\u901a\u5e38\u9700\u8981\u4f7f\u7528\u6807\u5fd7\u4f4d\u6216\u4e8b\u4ef6\u5bf9\u8c61\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u4f4d\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u5b9a\u671f\u68c0\u67e5\u8fd9\u4e2a\u6807\u5fd7\uff0c\u4ee5\u51b3\u5b9a\u662f\u5426\u9000\u51fa\u3002\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7<code>event.is_set()<\/code>\u65b9\u6cd5\u68c0\u67e5\u662f\u5426\u9700\u8981\u505c\u6b62\u8fd0\u884c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u662f\u5426\u53ef\u4ee5\u5f3a\u5236\u7ec8\u6b62\u7ebf\u7a0b\uff1f<\/strong><br \/>Python\u7684<code>threading<\/code>\u6a21\u5757\u5e76\u4e0d\u63d0\u4f9b\u76f4\u63a5\u5f3a\u5236\u7ec8\u6b62\u7ebf\u7a0b\u7684\u529f\u80fd\uff0c\u56e0\u4e3a\u5f3a\u5236\u7ec8\u6b62\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u6216\u6570\u636e\u4e0d\u4e00\u81f4\u3002\u63a8\u8350\u7684\u505a\u6cd5\u662f\u8ba9\u7ebf\u7a0b\u81ea\u884c\u68c0\u67e5\u7ec8\u6b62\u6761\u4ef6\uff0c\u5e76\u5728\u5b8c\u6210\u5176\u4efb\u52a1\u540e\u5b89\u5168\u9000\u51fa\u3002<\/p>\n<p><strong>\u7ec8\u6b62\u7ebf\u7a0b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u7ec8\u6b62\u7ebf\u7a0b\u65f6\uff0c\u91cd\u8981\u7684\u662f\u8981\u786e\u4fdd\u7ebf\u7a0b\u5728\u9000\u51fa\u4e4b\u524d\u5b8c\u6210\u5fc5\u8981\u7684\u6e05\u7406\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u786e\u4fdd\u5173\u95ed\u6587\u4ef6\u53e5\u67c4\u3001\u91ca\u653e\u9501\u6216\u4fdd\u5b58\u91cd\u8981\u6570\u636e\u3002\u5ffd\u7565\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u7684\u4e0d\u7a33\u5b9a\u6216\u6570\u636e\u4e22\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ec8\u6b62\u7ebf\u7a0b\u5e76\u4e0d\u662f\u4e00\u4ef6\u7b80\u5355\u7684\u4e8b\u60c5\uff0c\u56e0\u4e3aPython\u7684threading\u6a21\u5757\u5e76\u6ca1\u6709\u63d0\u4f9b\u4e00\u4e2a\u76f4\u63a5\u7684\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":978257,"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\/978248"}],"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=978248"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978248\/revisions"}],"predecessor-version":[{"id":978258,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978248\/revisions\/978258"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/978257"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=978248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=978248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=978248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}