{"id":1082033,"date":"2025-01-08T12:45:03","date_gmt":"2025-01-08T04:45:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082033.html"},"modified":"2025-01-08T12:45:05","modified_gmt":"2025-01-08T04:45:05","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e4%b8%80%e4%b8%aa%e7%ba%bf%e7%a8%8b%e7%ad%89%e5%be%85-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082033.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183743\/32b9ccb5-6a1a-4121-8332-851b5b5fb840.webp\" alt=\"python\u5982\u4f55\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\uff0c\u4f8b\u5982\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u3001<code>threading.Event<\/code>\u5bf9\u8c61\u3001<code>threading.Condition<\/code>\u5bf9\u8c61\u548c<code>threading.Lock<\/code>\u5bf9\u8c61\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u800c<code>threading.Event<\/code>\u5bf9\u8c61\u548c<code>threading.Condition<\/code>\u5bf9\u8c61\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u5236\u548c\u7075\u6d3b\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>time.sleep()<\/code>\u51fd\u6570\u4f7f\u5f53\u524d\u7ebf\u7a0b\u6682\u505c\u6267\u884c\u6307\u5b9a\u7684\u65f6\u95f4\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09\u3002\u8fd9\u662f\u6700\u76f4\u63a5\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Worker thread starting&quot;)<\/p>\n<p>    time.sleep(5)<\/p>\n<p>    print(&quot;Worker thread finished&quot;)<\/p>\n<p>import threading<\/p>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<p>t.join()  # \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>worker<\/code>\u51fd\u6570\u4e2d\u7684<code>time.sleep(5)<\/code>\u4f7f\u7ebf\u7a0b\u6682\u505c\u6267\u884c5\u79d2\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>threading.Event<\/code>\u5bf9\u8c61\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u540c\u6b65\u539f\u8bed\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u7b49\u5f85\u548c\u901a\u77e5\u673a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>event = threading.Event()<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Worker thread w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ting&quot;)<\/p>\n<p>    event.wait()<\/p>\n<p>    print(&quot;Worker thread resumed&quot;)<\/p>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u6a21\u62df\u4e00\u4e9b\u5de5\u4f5c<\/strong><\/h2>\n<p>import time<\/p>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u901a\u77e5worker\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c<\/strong><\/h2>\n<p>event.set()<\/p>\n<p>t.join()  # \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>worker<\/code>\u7ebf\u7a0b\u8c03\u7528\u4e86<code>event.wait()<\/code>\uff0c\u5b83\u4f1a\u4e00\u76f4\u7b49\u5f85\u76f4\u5230<code>event.set()<\/code>\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>threading.Condition<\/code>\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>threading.Condition<\/code>\u5bf9\u8c61\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u590d\u6742\u7684\u7ebf\u7a0b\u95f4\u540c\u6b65\uff0c\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53d1\u51fa\u901a\u77e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>condition = threading.Condition()<\/p>\n<p>def worker():<\/p>\n<p>    with condition:<\/p>\n<p>        print(&quot;Worker thread waiting&quot;)<\/p>\n<p>        condition.wait()<\/p>\n<p>        print(&quot;Worker thread resumed&quot;)<\/p>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u6a21\u62df\u4e00\u4e9b\u5de5\u4f5c<\/strong><\/h2>\n<p>import time<\/p>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u901a\u77e5worker\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c<\/strong><\/h2>\n<p>with condition:<\/p>\n<p>    condition.notify()<\/p>\n<p>t.join()  # \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>worker<\/code>\u7ebf\u7a0b\u5728<code>condition.wait()<\/code>\u5904\u7b49\u5f85\uff0c\u76f4\u5230\u4e3b\u7ebf\u7a0b\u8c03\u7528<code>condition.notify()<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>threading.Lock<\/code>\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>threading.Lock<\/code>\u5bf9\u8c61\u4e5f\u662f\u4e00\u79cd\u540c\u6b65\u539f\u8bed\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u4e92\u65a5\u548c\u7b49\u5f85\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def worker():<\/p>\n<p>    with lock:<\/p>\n<p>        print(&quot;Worker thread acquired the lock&quot;)<\/p>\n<p>        lock.acquire()<\/p>\n<p>        print(&quot;Worker thread resumed&quot;)<\/p>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u6a21\u62df\u4e00\u4e9b\u5de5\u4f5c<\/strong><\/h2>\n<p>import time<\/p>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u91ca\u653e\u9501\uff0c\u901a\u77e5worker\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c<\/strong><\/h2>\n<p>lock.release()<\/p>\n<p>t.join()  # \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>worker<\/code>\u7ebf\u7a0b\u5728<code>lock.acquire()<\/code>\u5904\u7b49\u5f85\uff0c\u76f4\u5230\u4e3b\u7ebf\u7a0b\u8c03\u7528<code>lock.release()<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u3001<code>threading.Event<\/code>\u5bf9\u8c61\u3001<code>threading.Condition<\/code>\u5bf9\u8c61\u548c<code>threading.Lock<\/code>\u5bf9\u8c61\u7b49\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7684\u7b49\u5f85\u548c\u901a\u77e5\u673a\u5236\u3002<\/p>\n<\/p>\n<ol>\n<li><strong><code>time.sleep()<\/code>\u51fd\u6570<\/strong>\uff1a\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u7ebf\u7a0b\u6682\u505c\u6267\u884c\u6307\u5b9a\u7684\u65f6\u95f4\u3002<\/li>\n<li><strong><code>threading.Event<\/code>\u5bf9\u8c61<\/strong>\uff1a\u63d0\u4f9b\u7b80\u5355\u7684\u7b49\u5f85\u548c\u901a\u77e5\u673a\u5236\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u7b49\u5f85\u4e8b\u4ef6\u7684\u53d1\u751f\u3002<\/li>\n<li><strong><code>threading.Condition<\/code>\u5bf9\u8c61<\/strong>\uff1a\u63d0\u4f9b\u66f4\u590d\u6742\u7684\u7b49\u5f85\u548c\u901a\u77e5\u673a\u5236\uff0c\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53d1\u51fa\u901a\u77e5\u3002<\/li>\n<li><strong><code>threading.Lock<\/code>\u5bf9\u8c61<\/strong>\uff1a\u7528\u4e8e\u5b9e\u73b0\u4e92\u65a5\u548c\u7b49\u5f85\u673a\u5236\uff0c\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u76f4\u5230\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u91ca\u653e\u9501\u3002<\/li>\n<\/ol>\n<p><p>\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u540c\u6b65\u539f\u8bed\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u7ebf\u7a0b\u7684\u6267\u884c\u987a\u5e8f\u548c\u534f\u8c03\u7ebf\u7a0b\u4e4b\u95f4\u7684\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7ebf\u7a0b\u6682\u505c\u4e00\u6bb5\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u6765\u4f7f\u7ebf\u7a0b\u6682\u505c\u6307\u5b9a\u7684\u65f6\u95f4\u3002\u53ea\u9700\u4f20\u5165\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u65f6\u95f4\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>time.sleep(5)<\/code>\u5c06\u4f7f\u5f53\u524d\u7ebf\u7a0b\u6682\u505c5\u79d2\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u7a0b\u5e8f\u6267\u884c\u4e2d\u95f4\u7b49\u5f85\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u4ee5\u907f\u514d\u6570\u636e\u7ade\u4e89\uff1f<\/strong><br \/>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u6570\u636e\u7ade\u4e89\u53ef\u80fd\u5bfc\u81f4\u4e0d\u4e00\u81f4\u7684\u7ed3\u679c\u3002\u4f7f\u7528\u7ebf\u7a0b\u9501\uff08<code>threading.Lock<\/code>\uff09\u53ef\u4ee5\u6709\u6548\u5730\u540c\u6b65\u7ebf\u7a0b\u3002\u5728\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u4e4b\u524d\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u83b7\u53d6\u9501\uff0c\u5b8c\u6210\u64cd\u4f5c\u540e\u91ca\u653e\u9501\uff0c\u4ece\u800c\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u8be5\u8d44\u6e90\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6570\u636e\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u63a7\u5236\u7ebf\u7a0b\u7684\u6267\u884c\u987a\u5e8f\uff1f<\/strong><br \/>\u63a7\u5236\u7ebf\u7a0b\u6267\u884c\u987a\u5e8f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>threading.Event<\/code>\u6216<code>threading.Condition<\/code>\u6765\u5b9e\u73b0\u3002<code>Event<\/code>\u5bf9\u8c61\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u53d1\u751f\uff0c\u800c<code>Condition<\/code>\u5bf9\u8c61\u5219\u63d0\u4f9b\u4e86\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u4f7f\u5f97\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u8fdb\u884c\u4ea4\u4e92\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u7ba1\u7406\u7ebf\u7a0b\u4e4b\u95f4\u7684\u534f\u8c03\u4e0e\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\uff0c\u4f8b\u5982\u4f7f\u7528time.sleep()\u51fd\u6570\u3001threading.E [&hellip;]","protected":false},"author":3,"featured_media":1082037,"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\/1082033"}],"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=1082033"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082033\/revisions"}],"predecessor-version":[{"id":1082038,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082033\/revisions\/1082038"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082037"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}