{"id":1179710,"date":"2025-01-15T18:27:28","date_gmt":"2025-01-15T10:27:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179710.html"},"modified":"2025-01-15T18:27:31","modified_gmt":"2025-01-15T10:27:31","slug":"python3-%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179710.html","title":{"rendered":"python3 \u5982\u4f55\u904d\u5386\u7ebf\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114044\/7e2b88b0-4eed-4608-bb15-5827dd831759.webp\" alt=\"python3 \u5982\u4f55\u904d\u5386\u7ebf\u7a0b\" \/><\/p>\n<p><p> <strong>Python3\u904d\u5386\u7ebf\u7a0b\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528threading\u6a21\u5757\u7684enumerate()\u51fd\u6570\u3001\u901a\u8fc7threading.active_count()\u51fd\u6570\u83b7\u53d6\u6d3b\u52a8\u7ebf\u7a0b\u6570\u91cf\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7ba1\u7406\u7ebf\u7a0b\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6709\u6548\u5730\u76d1\u63a7\u548c\u7ba1\u7406\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u4f7f\u7528threading\u6a21\u5757\u7684enumerate()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528threading\u6a21\u5757\u7684enumerate()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>threading.enumerate()<\/code>\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u6240\u6709\u6d3b\u52a8\u7684Thread\u5bf9\u8c61\u5217\u8868\u3002\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u72b6\u6001\u3001\u540d\u79f0\u4ee5\u53ca\u662f\u5426\u5b88\u62a4\u7ebf\u7a0b\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def thread_function(name):<\/p>\n<p>    print(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    threads = []<\/p>\n<p>    for index in range(3):<\/p>\n<p>        x = threading.Thread(target=thread_function, args=(index,))<\/p>\n<p>        threads.append(x)<\/p>\n<p>        x.start()<\/p>\n<p>    for t in threading.enumerate():<\/p>\n<p>        print(f&quot;Thread {t.name} is alive: {t.is_alive()}&quot;)<\/p>\n<p>    for t in threads:<\/p>\n<p>        t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u4e2a\u7ebf\u7a0b\u5e76\u542f\u52a8\u5b83\u4eec\uff0c\u7136\u540e\u4f7f\u7528<code>threading.enumerate()<\/code>\u51fd\u6570\u904d\u5386\u5f53\u524d\u6240\u6709\u6d3b\u52a8\u7684\u7ebf\u7a0b\uff0c\u5e76\u8f93\u51fa\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u540d\u79f0\u548c\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528threading.active_count()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>threading.active_count()<\/code>\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u6d3b\u52a8\u7684\u7ebf\u7a0b\u6570\u3002\u8fd9\u5bf9\u4e86\u89e3\u5f53\u524d\u7ebf\u7a0b\u6570\u76ee\u975e\u5e38\u6709\u7528\uff0c\u867d\u7136\u65e0\u6cd5\u76f4\u63a5\u904d\u5386\u7ebf\u7a0b\u5bf9\u8c61\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408enumerate()\u51fd\u6570\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def thread_function(name):<\/p>\n<p>    print(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    threads = []<\/p>\n<p>    for index in range(3):<\/p>\n<p>        x = threading.Thread(target=thread_function, args=(index,))<\/p>\n<p>        threads.append(x)<\/p>\n<p>        x.start()<\/p>\n<p>    print(f&quot;Active threads count: {threading.active_count()}&quot;)<\/p>\n<p>    for t in threading.enumerate():<\/p>\n<p>        print(f&quot;Thread {t.name} is alive: {t.is_alive()}&quot;)<\/p>\n<p>    for t in threads:<\/p>\n<p>        t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8f93\u51fa\u5f53\u524d\u6d3b\u8dc3\u7ebf\u7a0b\u7684\u6570\u91cf\uff0c\u7136\u540e\u4f7f\u7528enumerate()\u51fd\u6570\u904d\u5386\u5e76\u8f93\u51fa\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7ba1\u7406\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\uff08ThreadPoolExecutor\uff09\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u63a5\u53e3\u6765\u7ba1\u7406\u7ebf\u7a0b\uff0c\u7b80\u5316\u4e86\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528concurrent.futures\u6a21\u5757\u4e2d\u7684ThreadPoolExecutor\u6765\u7ba1\u7406\u548c\u904d\u5386\u7ebf\u7a0b\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 thread_function(name):<\/p>\n<p>    print(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>        futures = [executor.submit(thread_function, index) for index in range(3)]<\/p>\n<p>    for future in futures:<\/p>\n<p>        print(f&quot;Thread {future} completed: {future.done()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ThreadPoolExecutor\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u63d0\u4ea4\u4e86\u4e09\u4e2a\u4efb\u52a1\uff0c\u5e76\u5728\u4efb\u52a1\u5b8c\u6210\u540e\u68c0\u67e5\u6bcf\u4e2a\u4efb\u52a1\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u76d1\u63a7\u548c\u7ba1\u7406\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9664\u4e86\u904d\u5386\u7ebf\u7a0b\uff0c\u6211\u4eec\u901a\u5e38\u8fd8\u9700\u8981\u76d1\u63a7\u548c\u7ba1\u7406\u7ebf\u7a0b\u7684\u72b6\u6001\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u76d1\u63a7\u548c\u7ba1\u7406\u7b56\u7565\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u7ebf\u7a0b\u540d\u79f0\u548c\u6807\u8bc6<\/strong><\/p>\n<\/p>\n<p><p>\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2a\u540d\u79f0\u548c\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u5c5e\u6027\u6765\u8bc6\u522b\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u5728\u521b\u5efa\u7ebf\u7a0b\u65f6\uff0c\u53ef\u4ee5\u4e3a\u5176\u8bbe\u7f6e\u4e00\u4e2a\u6709\u610f\u4e49\u7684\u540d\u79f0\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u76d1\u63a7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def thread_function(name):<\/p>\n<p>    print(f&quot;Thread {name} (ID: {threading.get_ident()}) starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Thread {name} (ID: {threading.get_ident()}) finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    threads = []<\/p>\n<p>    for index in range(3):<\/p>\n<p>        x = threading.Thread(target=thread_function, args=(f&quot;Thread-{index}&quot;,))<\/p>\n<p>        threads.append(x)<\/p>\n<p>        x.start()<\/p>\n<p>    for t in threading.enumerate():<\/p>\n<p>        print(f&quot;Thread {t.name} (ID: {t.ident}) is alive: {t.is_alive()}&quot;)<\/p>\n<p>    for t in threads:<\/p>\n<p>        t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u540d\u79f0\uff0c\u5e76\u5728\u8f93\u51fa\u4e2d\u5305\u542b\u4e86\u7ebf\u7a0b\u7684\u540d\u79f0\u548c\u6807\u8bc6\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u5b88\u62a4\u7ebf\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u5b88\u62a4\u7ebf\u7a0b\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u4f1a\u81ea\u52a8\u9000\u51fa\u3002\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u4e0d\u4f1a\u88ab\u963b\u585e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def thread_function(name):<\/p>\n<p>    print(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    threads = []<\/p>\n<p>    for index in range(3):<\/p>\n<p>        x = threading.Thread(target=thread_function, args=(index,))<\/p>\n<p>        x.daemon = True<\/p>\n<p>        threads.append(x)<\/p>\n<p>        x.start()<\/p>\n<p>    for t in threading.enumerate():<\/p>\n<p>        print(f&quot;Thread {t.name} is alive: {t.is_alive()}&quot;)<\/p>\n<p>    print(&quot;Main thread finishing&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7ebf\u7a0b\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u8fd9\u6837\u5f53\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u7ebf\u7a0b\u540c\u6b65<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u540c\u6b65\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\u3002\u901a\u8fc7\u4f7f\u7528\u9501\u3001\u6761\u4ef6\u53d8\u91cf\u3001\u4e8b\u4ef6\u7b49\u540c\u6b65\u539f\u8bed\uff0c\u53ef\u4ee5\u786e\u4fdd\u7ebf\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4e00\u81f4\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def thread_function(name):<\/p>\n<p>    with lock:<\/p>\n<p>        print(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>        time.sleep(2)<\/p>\n<p>        print(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    threads = []<\/p>\n<p>    for index in range(3):<\/p>\n<p>        x = threading.Thread(target=thread_function, args=(index,))<\/p>\n<p>        threads.append(x)<\/p>\n<p>        x.start()<\/p>\n<p>    for t in threading.enumerate():<\/p>\n<p>        print(f&quot;Thread {t.name} is alive: {t.is_alive()}&quot;)<\/p>\n<p>    for t in threads:<\/p>\n<p>        t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u9501\u6765\u786e\u4fdd\u7ebf\u7a0b\u5728\u6253\u5370\u6d88\u606f\u65f6\u4e0d\u4f1a\u53d1\u751f\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7ba1\u7406\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u5185\u7f6e\u7684threading\u6a21\u5757\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7b80\u5316\u7ebf\u7a0b\u7ba1\u7406\u3002\u4f8b\u5982\uff0c<code>concurrent.futures<\/code>\u6a21\u5757\u4e2d\u7684<code>ThreadPoolExecutor<\/code>\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\u6765\u7ba1\u7406\u7ebf\u7a0b\u6c60\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 thread_function(name):<\/p>\n<p>    print(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>        futures = [executor.submit(thread_function, index) for index in range(3)]<\/p>\n<p>    for future in futures:<\/p>\n<p>        print(f&quot;Thread {future} completed: {future.done()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u63d0\u4ea4\u4e86\u4e09\u4e2a\u4efb\u52a1\uff0c\u5e76\u5728\u4efb\u52a1\u5b8c\u6210\u540e\u68c0\u67e5\u6bcf\u4e2a\u4efb\u52a1\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u7b80\u5316\u7ebf\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u5171\u4eab\u3002\u4f8b\u5982\uff0c<code>queue.Queue<\/code>\u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>import time<\/p>\n<p>def producer(q, n):<\/p>\n<p>    for i in range(n):<\/p>\n<p>        print(f&quot;Producing {i}&quot;)<\/p>\n<p>        q.put(i)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def consumer(q):<\/p>\n<p>    while True:<\/p>\n<p>        item = q.get()<\/p>\n<p>        if item is None:<\/p>\n<p>            break<\/p>\n<p>        print(f&quot;Consuming {item}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    q = queue.Queue()<\/p>\n<p>    n = 5<\/p>\n<p>    producer_thread = threading.Thread(target=producer, args=(q, n))<\/p>\n<p>    consumer_thread = threading.Thread(target=consumer, args=(q,))<\/p>\n<p>    producer_thread.start()<\/p>\n<p>    consumer_thread.start()<\/p>\n<p>    producer_thread.join()<\/p>\n<p>    q.put(None)  # Signal the consumer to exit<\/p>\n<p>    consumer_thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u5728\u751f\u4ea7\u8005\u7ebf\u7a0b\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ebf\u7a0b\u8c03\u8bd5\u548c\u5206\u6790\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u8c03\u8bd5\u548c\u5206\u6790\u7ebf\u7a0b\u95ee\u9898\u662f\u4e00\u4e2a\u6311\u6218\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u8c03\u8bd5\u548c\u5206\u6790\u5de5\u5177\uff1a<\/p>\n<\/p>\n<p><p><strong>1. logging\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u53ef\u4ee5\u8bb0\u5f55\u7ebf\u7a0b\u7684\u6d3b\u52a8\u65e5\u5fd7\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import logging<\/p>\n<p>import time<\/p>\n<p>def thread_function(name):<\/p>\n<p>    logging.info(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    logging.info(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    logging.basicConfig(level=logging.INFO, format=&#39;%(asctime)s - %(threadName)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>    threads = []<\/p>\n<p>    for index in range(3):<\/p>\n<p>        x = threading.Thread(target=thread_function, args=(index,))<\/p>\n<p>        threads.append(x)<\/p>\n<p>        x.start()<\/p>\n<p>    for t in threads:<\/p>\n<p>        t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u6d3b\u52a8\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><p><strong>2. Debugger<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u8c03\u8bd5\u5668\uff08\u5982Python\u5185\u7f6e\u7684<code>pdb<\/code>\u6216IDE\u63d0\u4f9b\u7684\u8c03\u8bd5\u5de5\u5177\uff09\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u7ebf\u7a0b\u7684\u72b6\u6001\u548c\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import pdb<\/p>\n<p>import time<\/p>\n<p>def thread_function(name):<\/p>\n<p>    pdb.set_trace()<\/p>\n<p>    print(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    threads = []<\/p>\n<p>    for index in range(3):<\/p>\n<p>        x = threading.Thread(target=thread_function, args=(index,))<\/p>\n<p>        threads.append(x)<\/p>\n<p>        x.start()<\/p>\n<p>    for t in threads:<\/p>\n<p>        t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u7ebf\u7a0b\u51fd\u6570\u4e2d\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u65ad\u70b9\uff0c\u53ef\u4ee5\u5728\u8c03\u8bd5\u5668\u4e2d\u9010\u6b65\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>3. Profiling<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\uff08\u5982cProfile\uff09\u53ef\u4ee5\u5206\u6790\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import cProfile<\/p>\n<p>import time<\/p>\n<p>def thread_function(name):<\/p>\n<p>    print(f&quot;Thread {name}: starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Thread {name}: finishing&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    profiler = cProfile.Profile()<\/p>\n<p>    profiler.enable()<\/p>\n<p>    threads = []<\/p>\n<p>    for index in range(3):<\/p>\n<p>        x = threading.Thread(target=thread_function, args=(index,))<\/p>\n<p>        threads.append(x)<\/p>\n<p>        x.start()<\/p>\n<p>    for t in threads:<\/p>\n<p>        t.join()<\/p>\n<p>    profiler.disable()<\/p>\n<p>    profiler.print_stats()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528cProfile\u5bf9\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u5e76\u8f93\u51fa\u5206\u6790\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6700\u4f73\u5b9e\u8df5\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u907f\u514d\u5171\u4eab\u53ef\u53d8\u72b6\u6001<\/strong><\/p>\n<\/p>\n<p><p>\u5171\u4eab\u53ef\u53d8\u72b6\u6001\u53ef\u80fd\u5bfc\u81f4\u7ade\u4e89\u6761\u4ef6\u548c\u6570\u636e\u4e0d\u4e00\u81f4\u3002\u5c3d\u91cf\u907f\u514d\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u53ef\u53d8\u72b6\u6001\uff0c\u6216\u8005\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\u548c\u540c\u6b65\u539f\u8bed\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528\u7ebf\u7a0b\u6c60<\/strong><\/p>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u7b80\u5316\u7ebf\u7a0b\u7ba1\u7406\uff0c\u907f\u514d\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u5e26\u6765\u7684\u5f00\u9500\u3002\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u6216\u5176\u4ed6\u7ebf\u7a0b\u6c60\u5b9e\u73b0\u6765\u7ba1\u7406\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u907f\u514d\u6b7b\u9501<\/strong><\/p>\n<\/p>\n<p><p>\u6b7b\u9501\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u901a\u5e38\u7531\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6301\u6709\u591a\u4e2a\u9501\u5f15\u8d77\u3002\u907f\u514d\u6b7b\u9501\u7684\u7b56\u7565\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53ea\u5728\u9700\u8981\u65f6\u6301\u6709\u9501<\/li>\n<li>\u907f\u514d\u5d4c\u5957\u9501<\/li>\n<li>\u4f7f\u7528\u8d85\u65f6\u673a\u5236<\/li>\n<\/ul>\n<p><p><strong>4. \u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u5b88\u62a4\u7ebf\u7a0b\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u4f1a\u81ea\u52a8\u9000\u51fa\uff0c\u9002\u7528\u4e8e\u540e\u53f0\u4efb\u52a1\u3002\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u4e0d\u4f1a\u88ab\u963b\u585e\u3002<\/p>\n<\/p>\n<p><p><strong>5. \u76d1\u63a7\u548c\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u76d1\u63a7\u548c\u65e5\u5fd7\u8bb0\u5f55\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u8c03\u8bd5\u548c\u5206\u6790\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u3002\u8bb0\u5f55\u7ebf\u7a0b\u7684\u6d3b\u52a8\u65e5\u5fd7\uff0c\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\u548c\u8c03\u8bd5\u5668\u6765\u68c0\u67e5\u7ebf\u7a0b\u7684\u72b6\u6001\u548c\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/p>\n<p><p><strong>6. \u6d4b\u8bd5\u548c\u9a8c\u8bc1<\/strong><\/p>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u901a\u5e38\u66f4\u96be\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u3002\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6d4b\u8bd5\u5de5\u5177\u548c\u6846\u67b6\u6765\u9a8c\u8bc1\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u904d\u5386\u7ebf\u7a0b\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u4efb\u52a1\uff0c\u901a\u8fc7\u4f7f\u7528<code>threading.enumerate()<\/code>\u51fd\u6570\u3001<code>threading.active_count()<\/code>\u51fd\u6570\u3001\u7ebf\u7a0b\u6c60\u548c\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u76d1\u63a7\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u3001\u8c03\u8bd5\u5de5\u5177\u548c\u6027\u80fd\u5206\u6790\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u8c03\u8bd5\u548c\u5206\u6790\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u907f\u514d\u5171\u4eab\u53ef\u53d8\u72b6\u6001\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u3001\u907f\u514d\u6b7b\u9501\u3001\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\u3001\u76d1\u63a7\u548c\u65e5\u5fd7\u8bb0\u5f55\u3001\u6d4b\u8bd5\u548c\u9a8c\u8bc1\uff0c\u53ef\u4ee5\u63d0\u9ad8\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6027\u80fd\u3002\u591a\u7ebf\u7a0b\u7f16\u7a0b\u867d\u7136\u590d\u6742\uff0c\u4f46\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u9ad8\u6548\u3001\u7a33\u5b9a\u7684\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7ee7\u627f\u81ea<code>threading.Thread<\/code>\u7684\u7c7b\u6216\u4f7f\u7528<code>threading.Thread<\/code>\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\uff0c\u5e76\u91cd\u5199<code>run<\/code>\u65b9\u6cd5\u6765\u6307\u5b9a\u7ebf\u7a0b\u6267\u884c\u7684\u4efb\u52a1\u3002\u521b\u5efa\u7ebf\u7a0b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>start()<\/code>\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\uff0c\u901a\u8fc7<code>join()<\/code>\u65b9\u6cd5\u6765\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<p><strong>\u4f7f\u7528Python3\u7684\u591a\u7ebf\u7a0b\u6709\u54ea\u4e9b\u5e38\u89c1\u5e94\u7528\u573a\u666f\uff1f<\/strong><br \/>\u591a\u7ebf\u7a0b\u5728\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\u7279\u522b\u6709\u6548\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u3002\u5f53\u7a0b\u5e8f\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u4efb\u52a1\u800c\u4e0d\u5e0c\u671b\u56e0\u67d0\u4e00\u4efb\u52a1\u7684\u5ef6\u8fdf\u800c\u963b\u585e\u5176\u4ed6\u4efb\u52a1\u65f6\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002\u6b64\u5916\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u4e5f\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u5b9e\u73b0\u5e76\u884c\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002<\/p>\n<p><strong>\u5728Python3\u4e2d\u5982\u4f55\u5b89\u5168\u5730\u5171\u4eab\u7ebf\u7a0b\u95f4\u7684\u6570\u636e\uff1f<\/strong><br \/>\u4e3a\u4e86\u5b89\u5168\u5730\u5171\u4eab\u6570\u636e\uff0cPython3\u63d0\u4f9b\u4e86\u591a\u79cd\u540c\u6b65\u673a\u5236\uff0c\u5982<code>Lock<\/code>\u3001<code>Event<\/code>\u3001<code>Semaphore<\/code>\u7b49\u3002\u901a\u8fc7\u4f7f\u7528<code>Lock<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u67d0\u4e2a\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u6027\u3002\u4f7f\u7528<code>Queue<\/code>\u6a21\u5757\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u80fd\u591f\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5b89\u5168\u5730\u4f20\u9012\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u534f\u8c03\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python3\u904d\u5386\u7ebf\u7a0b\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528threading\u6a21\u5757\u7684enumerate()\u51fd\u6570\u3001\u901a\u8fc7threadi [&hellip;]","protected":false},"author":3,"featured_media":1179717,"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\/1179710"}],"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=1179710"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179710\/revisions"}],"predecessor-version":[{"id":1179718,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179710\/revisions\/1179718"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179717"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}