{"id":996476,"date":"2024-12-27T09:15:24","date_gmt":"2024-12-27T01:15:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/996476.html"},"modified":"2024-12-27T09:15:27","modified_gmt":"2024-12-27T01:15:27","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/996476.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5efa\u7acb\u7ebf\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072557\/adf0a726-c289-4601-af4c-c03792aec377.webp\" alt=\"python\u4e2d\u5982\u4f55\u5efa\u7acb\u7ebf\u7a0b\" \/><\/p>\n<blockquote>\n<p><p>search(&quot;python \u5982\u4f55\u5efa\u7acb\u7ebf\u7a0b&quot;)<\/p>\n<p>mclick([0, 1, 3, 4, 10])<\/p>\n<p><strong>end-searching<\/strong><\/p>\n<\/p>\n<\/blockquote>\n<p><h3>Python\u4e2d\u5982\u4f55\u5efa\u7acb\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>threading<\/code>\u6a21\u5757\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002<strong>\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165\u6a21\u5757\u3001\u5b9a\u4e49\u7ebf\u7a0b\u903b\u8f91\u3001\u521b\u5efa\u7ebf\u7a0b\u5b9e\u4f8b\u5e76\u542f\u52a8\u7ebf\u7a0b\u3002<\/strong> \u6838\u5fc3\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>Thread<\/code>\u7c7b\u521b\u5efa\u7ebf\u7a0b\uff1a<\/strong><br \/>\u901a\u8fc7<code>threading.Thread<\/code>\u7c7b\u521b\u5efa\u7ebf\u7a0b\u5b9e\u4f8b\uff0c\u5e76\u5c06\u7ebf\u7a0b\u903b\u8f91\u51fd\u6570\u6216\u7c7b\u7ed1\u5b9a\u5230<code>target<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u542f\u52a8\u7ebf\u7a0b\uff1a<\/strong><br \/>\u4f7f\u7528<code>start()<\/code>\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\uff1a<\/strong><br \/>\u8c03\u7528<code>join()<\/code>\u65b9\u6cd5\u786e\u4fdd\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u6240\u6709\u5b50\u7ebf\u7a0b\u7ed3\u675f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(f&quot;Number: {i}&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = threading.Thread(target=print_numbers)<\/p>\n<p>thread2 = threading.Thread(target=print_numbers)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p>print(&quot;\u6240\u6709\u7ebf\u7a0b\u5df2\u5b8c\u6210&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b63\u6587\u5185\u5bb9\uff08\u591a\u7ebf\u7a0b\u77e5\u8bc6\u8be6\u89e3\uff09<\/h3>\n<\/p>\n<p><h4>\u4e00\u3001Python\u7ebf\u7a0b\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>Python\u7ebf\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u5de5\u5177\uff0c\u8fd0\u884c\u5728\u540c\u4e00\u8fdb\u7a0b\u4e2d\uff0c\u5171\u4eab\u5185\u5b58\u548c\u8d44\u6e90\u3002<strong>\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\uff0c\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u7684\u5b58\u5728\uff0cPython\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u6548\u7387\u6709\u9650\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u3001\u521b\u5efa\u7ebf\u7a0b\u7684\u4e24\u79cd\u65b9\u5f0f<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u51fd\u6570\u65b9\u5f0f\uff1a<\/strong><br \/>\u5c06\u76ee\u6807\u51fd\u6570\u4f20\u9012\u7ed9<code>Thread<\/code>\u7684<code>target<\/code>\u53c2\u6570\uff0c\u521b\u5efa\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def worker():<\/p>\n<p>    print(&quot;\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c&quot;)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ee7\u627f\u65b9\u5f0f\uff1a<\/strong><br \/>\u81ea\u5b9a\u4e49\u4e00\u4e2a\u7ee7\u627f<code>threading.Thread<\/code>\u7c7b\u7684\u5b50\u7c7b\uff0c\u5e76\u91cd\u5199<code>run<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyThread(threading.Thread):<\/p>\n<p>    def run(self):<\/p>\n<p>        print(&quot;\u5b50\u7c7b\u7ebf\u7a0b\u8fd0\u884c&quot;)<\/p>\n<p>thread = MyThread()<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>\u4e09\u3001\u7ebf\u7a0b\u540c\u6b65\u4e0e\u9501\u673a\u5236<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u5171\u4eab\u6570\u636e\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u7ade\u4e89\u6216\u6570\u636e\u4e0d\u4e00\u81f4\u95ee\u9898\u3002\u4e3a\u907f\u514d\u6b64\u95ee\u9898\uff0c\u53ef\u4f7f\u7528\u7ebf\u7a0b\u540c\u6b65\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9501\uff08Lock\uff09<\/strong><br \/>\u9501\u53ef\u786e\u4fdd\u67d0\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lock = threading.Lock()<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_increment():<\/p>\n<p>    global counter<\/p>\n<p>    with lock:<\/p>\n<p>        counter += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6761\u4ef6\u53d8\u91cf\uff08Condition\uff09<\/strong><br \/>\u7528\u4e8e\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u7ebf\u7a0b\u95f4\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">condition = threading.Condition()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u961f\u5217\uff08Queue\uff09\uff1a<\/strong><br \/>\u7528\u4e8e\u7ebf\u7a0b\u95f4\u5b89\u5168\u901a\u4fe1\uff0c<code>Queue<\/code>\u6a21\u5757\u63d0\u4f9bFIFO\u3001LIFO\u7b49\u591a\u79cd\u961f\u5217\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>\u56db\u3001\u7ebf\u7a0b\u6c60\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u7ba1\u7406\u5927\u91cf\u7ebf\u7a0b\u65f6\uff0c<code>concurrent.futures.ThreadPoolExecutor<\/code>\u53ef\u7b80\u5316\u7ba1\u7406\u5e76\u63d0\u9ad8\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def task(n):<\/p>\n<p>    print(f&quot;\u4efb\u52a1{n}\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    for i in range(10):<\/p>\n<p>        executor.submit(task, i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e94\u3001\u591a\u7ebf\u7a0b\u7684\u5e94\u7528\u573a\u666f\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><br \/>\u591a\u7ebf\u7a0b\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u7f51\u7edc\u722c\u866b\u3001\u6587\u4ef6\u5904\u7406\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u907f\u514d\u5168\u5c40\u53d8\u91cf\u5171\u4eab\u5e26\u6765\u7684\u7ade\u6001\u6761\u4ef6\u3002<\/li>\n<li>\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u53ef\u4f18\u5148\u8003\u8651\u591a\u8fdb\u7a0b\u6216\u5f02\u6b65\u7f16\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528Python\u7684<code>threading<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u591a\u7ebf\u7a0b\u7f16\u7a0b\uff0c\u63d0\u5347\u5e94\u7528\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u30109\u3011\u301010\u3011\u301011\u3011\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>\u4f7f\u7528\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\uff0c\u7279\u522b\u662f\u5728\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u7684\u540c\u65f6\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002\u6b64\u5916\uff0c\u7ebf\u7a0b\u5728\u5904\u7406\u5e76\u53d1\u64cd\u4f5c\u65f6\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5229\u7528\u7cfb\u7edf\u8d44\u6e90\uff0c\u51cf\u5c11\u7b49\u5f85\u65f6\u95f4\uff0c\u4ece\u800c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u7ebf\u7a0b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5e93\u662f<code>threading<\/code>\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u7528\u6237\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u9664\u4e86<code>threading<\/code>\uff0c\u8fd8\u6709<code>concurrent.futures<\/code>\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\u6765\u5904\u7406\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u7684\u6c60\uff0c\u4fbf\u4e8e\u4efb\u52a1\u7684\u5e76\u884c\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7ebf\u7a0b\u4e2d\u5904\u7406\u5f02\u5e38\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u6267\u884c\u4e0a\u4e0b\u6587\uff0c\u56e0\u6b64\u5728\u7ebf\u7a0b\u4e2d\u6355\u83b7\u5f02\u5e38\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002\u53ef\u4ee5\u5728\u76ee\u6807\u51fd\u6570\u4e2d\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u786e\u4fdd\u7ebf\u7a0b\u80fd\u591f\u5b89\u5168\u9000\u51fa\u5e76\u8fdb\u884c\u5fc5\u8981\u7684\u6e05\u7406\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>threading.Event<\/code>\u5bf9\u8c61\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\uff0c\u4ee5\u4fbf\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"search(&quot;python \u5982\u4f55\u5efa\u7acb\u7ebf\u7a0b&quot;) mclick([0, 1, 3, 4, 1 [&hellip;]","protected":false},"author":3,"featured_media":996493,"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\/996476"}],"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=996476"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996476\/revisions"}],"predecessor-version":[{"id":996500,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996476\/revisions\/996500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/996493"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=996476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=996476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=996476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}