{"id":1023683,"date":"2024-12-30T14:07:11","date_gmt":"2024-12-30T06:07:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1023683.html"},"modified":"2024-12-30T14:07:13","modified_gmt":"2024-12-30T06:07:13","slug":"python-%e5%a6%82%e4%bd%95%e5%a4%9a%e7%ba%bf%e7%a8%8b%e8%bf%90%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1023683.html","title":{"rendered":"python \u5982\u4f55\u591a\u7ebf\u7a0b\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/434ead54-536a-4922-9c71-2e13c157cd7f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python \u5982\u4f55\u591a\u7ebf\u7a0b\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>Python \u591a\u7ebf\u7a0b\u8fd0\u884c\u4e3b\u8981\u901a\u8fc7 threading \u6a21\u5757\u3001\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u3001\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3001\u4f7f\u7528\u9501\u673a\u5236\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python \u4e2d\u591a\u7ebf\u7a0b\u8fd0\u884c\u53ef\u4ee5\u901a\u8fc7 threading \u6a21\u5757\u6765\u5b9e\u73b0\u3002\u591a\u7ebf\u7a0b\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u53ef\u80fd\u4f1a\u5f71\u54cd\u591a\u7ebf\u7a0b\u7684\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u3002\u4e3a\u4e86\u907f\u514d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u5171\u4eab\u6570\u636e\u800c\u5bfc\u81f4\u7ade\u4e89\u6761\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9501\u673a\u5236\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728 Python \u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7406\u89e3\u591a\u7ebf\u7a0b\u548c GIL<\/h2>\n<\/p>\n<p><h3>1.1 \u4ec0\u4e48\u662f\u591a\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u662f\u6307\u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u8fdb\u884c\u8fd0\u7b97\u8c03\u5ea6\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u7ebf\u7a0b\u3002\u591a\u7ebf\u7a0b\u7684\u76ee\u7684\u662f\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u7279\u522b\u662f\u5728 I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1.2 GIL \u7684\u5f71\u54cd<\/h3>\n<\/p>\n<p><p>\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08Global Interpreter Lock\uff0c\u7b80\u79f0 GIL\uff09\u662f Python \u89e3\u91ca\u5668\u7528\u6765\u9650\u5236\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c Python \u5b57\u8282\u7801\u7684\u673a\u5236\u3002\u8fd9\u610f\u5473\u7740\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u4e0d\u80fd\u771f\u6b63\u5e76\u884c\u5730\u6267\u884c Python \u4ee3\u7801\u3002\u5728 I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0cGIL \u7684\u5f71\u54cd\u8f83\u5c0f\uff0c\u56e0\u4e3a\u7ebf\u7a0b\u4f1a\u9891\u7e41\u5730\u8fdb\u884c I\/O \u64cd\u4f5c\uff0c\u91ca\u653e GIL\uff1b\u4f46\u5728 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0cGIL \u53ef\u80fd\u4f1a\u9650\u5236\u591a\u7ebf\u7a0b\u7684\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 threading \u6a21\u5757\u5b9e\u73b0\u591a\u7ebf\u7a0b<\/h2>\n<\/p>\n<p><h3>2.1 \u521b\u5efa\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 threading \u6a21\u5757\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u521b\u5efa\u548c\u542f\u52a8\u591a\u4e2a\u7ebf\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def task(name):<\/p>\n<p>    print(f&#39;Thread {name} is running&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    t = threading.Thread(target=task, args=(i,))<\/p>\n<p>    threads.append(t)<\/p>\n<h2><strong>\u542f\u52a8\u6240\u6709\u7ebf\u7a0b<\/strong><\/h2>\n<p>for t in threads:<\/p>\n<p>    t.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p>print(&#39;All threads have finished&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4efb\u52a1\u51fd\u6570 <code>task<\/code>\uff0c\u7136\u540e\u521b\u5efa\u4e86 5 \u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6267\u884c <code>task<\/code> \u51fd\u6570\u3002\u6211\u4eec\u4f7f\u7528 <code>start<\/code> \u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\uff0c\u4f7f\u7528 <code>join<\/code> \u65b9\u6cd5\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u4f7f\u7528\u7ebf\u7a0b\u7c7b<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f <code>threading.Thread<\/code> \u7c7b\u6765\u521b\u5efa\u7ebf\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class MyThread(threading.Thread):<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.name = name<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&#39;Thread {self.name} is running&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    t = MyThread(name=i)<\/p>\n<p>    threads.append(t)<\/p>\n<h2><strong>\u542f\u52a8\u6240\u6709\u7ebf\u7a0b<\/strong><\/h2>\n<p>for t in threads:<\/p>\n<p>    t.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p>print(&#39;All threads have finished&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>MyThread<\/code> \u7c7b\uff0c\u7ee7\u627f\u81ea <code>threading.Thread<\/code> \u7c7b\uff0c\u5e76\u91cd\u5199\u4e86 <code>run<\/code> \u65b9\u6cd5\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u591a\u4e2a <code>MyThread<\/code> \u5bf9\u8c61\uff0c\u5e76\u542f\u52a8\u548c\u7b49\u5f85\u8fd9\u4e9b\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u7ebf\u7a0b\u540c\u6b65\u548c\u9501\u673a\u5236<\/h2>\n<\/p>\n<p><h3>3.1 \u7ebf\u7a0b\u540c\u6b65<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u548c\u4fee\u6539\u5171\u4eab\u6570\u636e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u4f8b\u5982\u9501\uff08Lock\uff09\u3002<\/p>\n<\/p>\n<p><h3>3.2 \u4f7f\u7528\u9501\u673a\u5236<\/h3>\n<\/p>\n<p><p><code>threading.Lock<\/code> \u662f\u4e00\u4e2a\u7b80\u5355\u7684\u9501\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u4e8e\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u6bb5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<h2><strong>\u5171\u4eab\u6570\u636e<\/strong><\/h2>\n<p>counter = 0<\/p>\n<p>counter_lock = threading.Lock()<\/p>\n<p>def increment_counter():<\/p>\n<p>    global counter<\/p>\n<p>    for _ in range(100000):<\/p>\n<p>        with counter_lock:<\/p>\n<p>            counter += 1<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    t = threading.Thread(target=increment_counter)<\/p>\n<p>    threads.append(t)<\/p>\n<h2><strong>\u542f\u52a8\u6240\u6709\u7ebf\u7a0b<\/strong><\/h2>\n<p>for t in threads:<\/p>\n<p>    t.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p>print(f&#39;Final counter value: {counter}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf <code>counter<\/code>\uff0c\u5e76\u4f7f\u7528 <code>counter_lock<\/code> \u6765\u4fdd\u62a4\u5bf9 <code>counter<\/code> \u7684\u4fee\u6539\u3002\u6bcf\u6b21\u4fee\u6539 <code>counter<\/code> \u65f6\uff0c\u7ebf\u7a0b\u4f1a\u5148\u83b7\u53d6\u9501\uff0c\u786e\u4fdd\u6ca1\u6709\u5176\u4ed6\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539 <code>counter<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u6700\u7ec8\u7684 <code>counter<\/code> \u503c\u662f\u6b63\u786e\u7684\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7ebf\u7a0b\u6c60\u548c concurrent.futures \u6a21\u5757<\/h2>\n<\/p>\n<p><h3>4.1 \u4f7f\u7528\u7ebf\u7a0b\u6c60<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u624b\u52a8\u7ba1\u7406\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u6bd4\u8f83\u7e41\u7410\u3002Python \u63d0\u4f9b\u4e86 <code>concurrent.futures<\/code> \u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u7ebf\u7a0b\u6c60\u3002\u7ebf\u7a0b\u6c60\u662f\u4e00\u7ec4\u9884\u5148\u521b\u5efa\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u590d\u7528\u8fd9\u4e9b\u7ebf\u7a0b\u6765\u6267\u884c\u4efb\u52a1\uff0c\u907f\u514d\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h3>4.2 \u4f7f\u7528 ThreadPoolExecutor<\/h3>\n<\/p>\n<p><p><code>concurrent.futures.ThreadPoolExecutor<\/code> \u662f\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u6267\u884c\u5668\uff0c\u53ef\u4ee5\u7528\u6765\u7ba1\u7406\u7ebf\u7a0b\u6c60\u5e76\u63d0\u4ea4\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor, as_completed<\/p>\n<p>def task(name):<\/p>\n<p>    print(f&#39;Task {name} is running&#39;)<\/p>\n<p>    return f&#39;Task {name} result&#39;<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60\u6267\u884c\u5668<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    # \u63d0\u4ea4\u591a\u4e2a\u4efb\u52a1<\/p>\n<p>    futures = [executor.submit(task, i) for i in range(5)]<\/p>\n<p>    # \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u5e76\u83b7\u53d6\u7ed3\u679c<\/p>\n<p>    for future in as_completed(futures):<\/p>\n<p>        result = future.result()<\/p>\n<p>        print(result)<\/p>\n<p>print(&#39;All tasks have finished&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a <code>ThreadPoolExecutor<\/code>\uff0c\u5e76\u63d0\u4ea4\u4e86\u591a\u4e2a\u4efb\u52a1\u3002\u4f7f\u7528 <code>submit<\/code> \u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\u540e\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a <code>Future<\/code> \u5bf9\u8c61\uff0c\u8868\u793a\u5f02\u6b65\u6267\u884c\u7684\u4efb\u52a1\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>as_completed<\/code> \u65b9\u6cd5\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\uff0c\u5e76\u83b7\u53d6\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528 Queue \u8fdb\u884c\u7ebf\u7a0b\u95f4\u901a\u4fe1<\/h2>\n<\/p>\n<p><h3>5.1 \u4ec0\u4e48\u662f Queue<\/h3>\n<\/p>\n<p><p><code>queue.Queue<\/code> \u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u751f\u4ea7\u8005\u7ebf\u7a0b\u53ef\u4ee5\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u53ef\u4ee5\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u961f\u5217\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><h3>5.2 \u4f7f\u7528 Queue \u5b9e\u73b0\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 <code>queue.Queue<\/code> \u5b9e\u73b0\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efa\u961f\u5217<\/strong><\/h2>\n<p>q = queue.Queue()<\/p>\n<h2><strong>\u751f\u4ea7\u8005\u7ebf\u7a0b<\/strong><\/h2>\n<p>def producer():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        item = f&#39;Item {i}&#39;<\/p>\n<p>        q.put(item)<\/p>\n<p>        print(f&#39;Produced {item}&#39;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u6d88\u8d39\u8005\u7ebf\u7a0b<\/strong><\/h2>\n<p>def consumer():<\/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&#39;Consumed {item}&#39;)<\/p>\n<p>        q.task_done()<\/p>\n<h2><strong>\u521b\u5efa\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b<\/strong><\/h2>\n<p>producer_thread = threading.Thread(target=producer)<\/p>\n<p>consumer_thread = threading.Thread(target=consumer)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>producer_thread.start()<\/p>\n<p>consumer_thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u751f\u4ea7\u8005\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>producer_thread.join()<\/p>\n<h2><strong>\u5411\u961f\u5217\u4e2d\u653e\u5165 None\uff0c\u8868\u793a\u7ed3\u675f<\/strong><\/h2>\n<p>q.put(None)<\/p>\n<h2><strong>\u7b49\u5f85\u6d88\u8d39\u8005\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>consumer_thread.join()<\/p>\n<p>print(&#39;All tasks have finished&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a <code>queue.Queue<\/code> \u5bf9\u8c61\uff0c\u751f\u4ea7\u8005\u7ebf\u7a0b\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5904\u7406\u3002\u6211\u4eec\u4f7f\u7528 <code>task_done<\/code> \u65b9\u6cd5\u901a\u77e5\u961f\u5217\u4efb\u52a1\u5df2\u5b8c\u6210\uff0c\u5e76\u4f7f\u7528 <code>put(None)<\/code> \u65b9\u6cd5\u5411\u961f\u5217\u4e2d\u653e\u5165\u4e00\u4e2a\u7279\u6b8a\u7684\u7ed3\u675f\u6807\u5fd7\uff0c\u901a\u77e5\u6d88\u8d39\u8005\u7ebf\u7a0b\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u7ebf\u7a0b\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><h3>6.1 \u7ebf\u7a0b\u7684\u4f18\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u5e76\u53d1\u6267\u884c\u4efb\u52a1<\/strong>\uff1a\u591a\u7ebf\u7a0b\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7279\u522b\u662f\u5728 I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u3002<\/li>\n<li><strong>\u8d44\u6e90\u5171\u4eab<\/strong>\uff1a\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u8d44\u6e90\uff0c\u4fbf\u4e8e\u7ebf\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u6362\u548c\u901a\u4fe1\u3002<\/li>\n<li><strong>\u54cd\u5e94\u6027\u9ad8<\/strong>\uff1a\u591a\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u54cd\u5e94\u6027\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u5728\u5904\u7406\u8017\u65f6\u4efb\u52a1\u65f6\u4ecd\u80fd\u54cd\u5e94\u7528\u6237\u8f93\u5165\u3002<\/li>\n<\/ul>\n<p><h3>6.2 \u7ebf\u7a0b\u7684\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>GIL \u9650\u5236<\/strong>\uff1a\u7531\u4e8e GIL \u7684\u5b58\u5728\uff0cPython \u7684\u591a\u7ebf\u7a0b\u5728 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u7684\u6027\u80fd\u63d0\u5347\u6709\u9650\u3002<\/li>\n<li><strong>\u590d\u6742\u6027\u9ad8<\/strong>\uff1a\u591a\u7ebf\u7a0b\u7f16\u7a0b\u9700\u8981\u5904\u7406\u7ebf\u7a0b\u540c\u6b65\u3001\u7ade\u4e89\u6761\u4ef6\u7b49\u95ee\u9898\uff0c\u589e\u52a0\u4e86\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u3002<\/li>\n<li><strong>\u8d44\u6e90\u5f00\u9500<\/strong>\uff1a\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u9700\u8981\u4e00\u5b9a\u7684\u8d44\u6e90\u5f00\u9500\uff0c\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u53ef\u80fd\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><h3>6.3 \u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<ul>\n<li><strong>I\/O \u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\uff1a\u591a\u7ebf\u7a0b\u9002\u7528\u4e8e I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u3002\u5728\u8fd9\u4e9b\u4efb\u52a1\u4e2d\uff0c\u7ebf\u7a0b\u4f1a\u9891\u7e41\u8fdb\u884c I\/O \u64cd\u4f5c\uff0c\u91ca\u653e GIL\uff0c\u63d0\u9ad8\u5e76\u53d1\u6027\u80fd\u3002<\/li>\n<li><strong>\u4efb\u52a1\u5e76\u53d1<\/strong>\uff1a\u591a\u7ebf\u7a0b\u9002\u7528\u4e8e\u9700\u8981\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u5e76\u884c\u4e0b\u8f7d\u6587\u4ef6\u3001\u5e76\u884c\u5904\u7406\u6570\u636e\u7b49\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u54cd\u5e94\u6027<\/strong>\uff1a\u591a\u7ebf\u7a0b\u9002\u7528\u4e8e\u9700\u8981\u63d0\u9ad8\u7a0b\u5e8f\u54cd\u5e94\u6027\u7684\u573a\u666f\uff0c\u4f8b\u5982 GUI \u7a0b\u5e8f\u3001\u5b9e\u65f6\u6570\u636e\u5904\u7406\u7b49\u3002<\/li>\n<\/ul>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python \u7684\u591a\u7ebf\u7a0b\u8fd0\u884c\u4e3b\u8981\u901a\u8fc7 <code>threading<\/code> \u6a21\u5757\u6765\u5b9e\u73b0\uff0c\u76ee\u7684\u662f\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f GIL \u7684\u5f71\u54cd\uff0c\u7279\u522b\u662f\u5728 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u3002\u4e3a\u4e86\u907f\u514d\u7ade\u4e89\u6761\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9501\u673a\u5236\u4fdd\u62a4\u5171\u4eab\u6570\u636e\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>concurrent.futures<\/code> \u6a21\u5757\u7ba1\u7406\u7ebf\u7a0b\u6c60\uff0c\u7b80\u5316\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002\u901a\u8fc7 <code>queue.Queue<\/code> \u53ef\u4ee5\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u5b89\u5168\u901a\u4fe1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u591a\u7ebf\u7a0b\u9002\u7528\u4e8e I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u3001\u4efb\u52a1\u5e76\u53d1\u548c\u63d0\u9ad8\u54cd\u5e94\u6027\u7684\u573a\u666f\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528 Python \u7684\u591a\u7ebf\u7a0b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u591a\u7ebf\u7a0b\u5728Python\u4e2d\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u591a\u7ebf\u7a0b\u5728Python\u4e2d\u4e3b\u8981\u7528\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\uff0c\u5c24\u5176\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u6216\u6587\u4ef6\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u65f6\uff0c\u8ba9\u5176\u4ed6\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\uff0c\u4ece\u800c\u63d0\u9ad8\u6574\u4f53\u7684\u6267\u884c\u6548\u7387\u3002\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u7531\u4e8ePython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\uff0c\u591a\u7ebf\u7a0b\u7684\u6548\u679c\u53ef\u80fd\u4e0d\u5982\u591a\u8fdb\u7a0b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>Thread<\/code>\u7c7b\u6216\u8005\u4f7f\u7528<code>threading.Thread<\/code>\u6765\u521b\u5efa\u65b0\u7ebf\u7a0b\u3002\u901a\u8fc7\u91cd\u5199<code>run()<\/code>\u65b9\u6cd5\u5b9a\u4e49\u7ebf\u7a0b\u7684\u4efb\u52a1\uff0c\u5e76\u8c03\u7528<code>start()<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u7ebf\u7a0b\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5b50\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<p><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u4f7f\u7528\u591a\u7ebf\u7a0b\u65f6\uff0c\u9700\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528<code>Lock<\/code>\u6216<code>Semaphore<\/code>\u7b49\u673a\u5236\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u3002\u6b64\u5916\uff0c\u5408\u7406\u8bbe\u8ba1\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\uff0c\u907f\u514d\u8fc7\u591a\u7684\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u6216\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u591a\u7ebf\u7a0b\u8fd0\u884c\u4e3b\u8981\u901a\u8fc7 threading \u6a21\u5757\u3001\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u3001\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3001\u4f7f\u7528\u9501\u673a\u5236\u907f\u514d\u7ade\u4e89\u6761\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":1023689,"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\/1023683"}],"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=1023683"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023683\/revisions"}],"predecessor-version":[{"id":1023693,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023683\/revisions\/1023693"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1023689"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1023683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1023683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1023683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}