{"id":1108001,"date":"2025-01-08T16:55:30","date_gmt":"2025-01-08T08:55:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108001.html"},"modified":"2025-01-08T16:55:33","modified_gmt":"2025-01-08T08:55:33","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e7%9a%84%e7%b1%bb%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108001.html","title":{"rendered":"\u5982\u4f55\u5728python\u7684\u7c7b\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071805\/ac7ed5d1-95b5-47e1-98ac-38ac0ffd057f.webp\" alt=\"\u5982\u4f55\u5728python\u7684\u7c7b\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\" \/><\/p>\n<p><p> <strong>\u5728Python\u7684\u7c7b\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b<\/strong>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002<strong>\u521b\u5efa\u7ebf\u7a0b\u7c7b\u3001\u7ee7\u627fThread\u7c7b\u3001\u5b9e\u73b0run\u65b9\u6cd5\u3001\u4f7f\u7528\u9501\u673a\u5236\u6765\u907f\u514d\u7ebf\u7a0b\u95f4\u7ade\u4e89<\/strong>\u662f\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u7684\u7c7b\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0c\u5e76\u5bf9\u5176\u4e2d\u4e00\u4e2a\u6838\u5fc3\u70b9\u2014\u2014\u4f7f\u7528\u9501\u673a\u5236\u6765\u907f\u514d\u7ebf\u7a0b\u95f4\u7ade\u4e89\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u9501\u673a\u5236\u6765\u907f\u514d\u7ebf\u7a0b\u95f4\u7ade\u4e89\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u70b9\u3002\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u8bbf\u95ee\u548c\u4fee\u6539\u5171\u4eab\u8d44\u6e90\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u51b2\u7a81\u3002\u901a\u8fc7\u4f7f\u7528\u9501\u673a\u5236\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002Python\u63d0\u4f9b\u4e86<code>threading.Lock<\/code>\u7c7b\u6765\u5b9e\u73b0\u9501\u673a\u5236\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u7ebf\u7a0b\u6267\u884c\u7684\u5173\u952e\u4ee3\u7801\u6bb5\u524d\u540e\u52a0\u4e0a\u9501\u7684\u83b7\u53d6\u548c\u91ca\u653e\u64cd\u4f5c\uff0c\u4ece\u800c\u786e\u4fdd\u7ebf\u7a0b\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u521b\u5efa\u7ebf\u7a0b\u7c7b<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Python\u7684\u7c7b\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u7c7b\u3002\u8fd9\u4e2a\u7ebf\u7a0b\u7c7b\u53ef\u4ee5\u7ee7\u627f\u81ea<code>threading.Thread<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199\u5176<code>run<\/code>\u65b9\u6cd5\u3002<code>run<\/code>\u65b9\u6cd5\u662f\u7ebf\u7a0b\u7684\u5165\u53e3\u70b9\uff0c\u5f53\u7ebf\u7a0b\u542f\u52a8\u65f6\uff0c<code>run<\/code>\u65b9\u6cd5\u4e2d\u7684\u4ee3\u7801\u5c06\u88ab\u6267\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u7a0b\u7c7b\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, thread_id, name, counter):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.thread_id = thread_id<\/p>\n<p>        self.name = name<\/p>\n<p>        self.counter = counter<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Starting thread: {self.name}&quot;)<\/p>\n<p>        # \u5728\u6b64\u5904\u6dfb\u52a0\u7ebf\u7a0b\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<p>        print(f&quot;Exiting thread: {self.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>MyThread<\/code>\u7684\u7c7b\uff0c\u5b83\u7ee7\u627f\u81ea<code>threading.Thread<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199\u4e86<code>run<\/code>\u65b9\u6cd5\u3002<code>run<\/code>\u65b9\u6cd5\u4e2d\u5305\u542b\u4e86\u7ebf\u7a0b\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efa\u7ebf\u7a0b\u5e76\u542f\u52a8<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6\u521b\u5efa\u4e86\u7ebf\u7a0b\u7c7b\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61\u5e76\u542f\u52a8\u7ebf\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u65b0\u7ebf\u7a0b<\/p>\n<p>thread1 = MyThread(1, &quot;Thread-1&quot;, 1)<\/p>\n<p>thread2 = MyThread(2, &quot;Thread-2&quot;, 2)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u7ebf\u7a0b\u5bf9\u8c61<code>thread1<\/code>\u548c<code>thread2<\/code>\uff0c\u5e76\u8c03\u7528\u5b83\u4eec\u7684<code>start<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u7ebf\u7a0b\u3002\u5f53\u7ebf\u7a0b\u542f\u52a8\u65f6\uff0c\u5b83\u4eec\u5c06\u6267\u884c<code>run<\/code>\u65b9\u6cd5\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u9501\u673a\u5236\u6765\u907f\u514d\u7ebf\u7a0b\u95f4\u7ade\u4e89<\/h2>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u8bbf\u95ee\u548c\u4fee\u6539\u5171\u4eab\u8d44\u6e90\uff0c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u51b2\u7a81\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9501\u673a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u9501\u673a\u5236\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\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, thread_id, name, counter, lock):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.thread_id = thread_id<\/p>\n<p>        self.name = name<\/p>\n<p>        self.counter = counter<\/p>\n<p>        self.lock = lock<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Starting thread: {self.name}&quot;)<\/p>\n<p>        # \u83b7\u53d6\u9501<\/p>\n<p>        self.lock.acquire()<\/p>\n<p>        try:<\/p>\n<p>            # \u5728\u6b64\u5904\u6dfb\u52a0\u7ebf\u7a0b\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<p>            print(f&quot;Thread {self.name} is running&quot;)<\/p>\n<p>        finally:<\/p>\n<p>            # \u91ca\u653e\u9501<\/p>\n<p>            self.lock.release()<\/p>\n<p>        print(f&quot;Exiting thread: {self.name}&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u9501\u5bf9\u8c61<\/strong><\/h2>\n<p>lock = threading.Lock()<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = MyThread(1, &quot;Thread-1&quot;, 1, lock)<\/p>\n<p>thread2 = MyThread(2, &quot;Thread-2&quot;, 2, lock)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u9501\u5bf9\u8c61<code>lock<\/code>\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u7ebf\u7a0b\u7c7b\u3002\u5728<code>run<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5728\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u4e4b\u524d\u83b7\u53d6\u9501\uff0c\u5e76\u5728\u8bbf\u95ee\u5b8c\u6210\u540e\u91ca\u653e\u9501\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5728\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7ebf\u7a0b\u540c\u6b65<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u786e\u4fdd\u591a\u4e2a\u7ebf\u7a0b\u6309\u7167\u7279\u5b9a\u7684\u987a\u5e8f\u6267\u884c\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u4f8b\u5982\u4e8b\u4ef6\u3001\u6761\u4ef6\u53d8\u91cf\u548c\u4fe1\u53f7\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u4e8b\u4ef6\u6765\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\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, thread_id, name, counter, event):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.thread_id = thread_id<\/p>\n<p>        self.name = name<\/p>\n<p>        self.counter = counter<\/p>\n<p>        self.event = event<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Starting thread: {self.name}&quot;)<\/p>\n<p>        # \u7b49\u5f85\u4e8b\u4ef6<\/p>\n<p>        self.event.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t()<\/p>\n<p>        # \u5728\u6b64\u5904\u6dfb\u52a0\u7ebf\u7a0b\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<p>        print(f&quot;Thread {self.name} is running&quot;)<\/p>\n<p>        print(f&quot;Exiting thread: {self.name}&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e8b\u4ef6\u5bf9\u8c61<\/strong><\/h2>\n<p>event = threading.Event()<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = MyThread(1, &quot;Thread-1&quot;, 1, event)<\/p>\n<p>thread2 = MyThread(2, &quot;Thread-2&quot;, 2, event)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e8b\u4ef6<\/strong><\/h2>\n<p>event.set()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5bf9\u8c61<code>event<\/code>\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u7ebf\u7a0b\u7c7b\u3002\u5728<code>run<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u8c03\u7528<code>event.wait<\/code>\u65b9\u6cd5\u6765\u7b49\u5f85\u4e8b\u4ef6\u88ab\u8bbe\u7f6e\u3002\u901a\u8fc7\u8c03\u7528<code>event.set<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u77e5\u7b49\u5f85\u7684\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u786e\u4fdd\u7ebf\u7a0b\u6309\u7167\u7279\u5b9a\u7684\u987a\u5e8f\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u7ebf\u7a0b\u6c60<\/h2>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u521b\u5efa\u548c\u7ba1\u7406\u5927\u91cf\u7684\u7ebf\u7a0b\u3002\u4e3a\u4e86\u7b80\u5316\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u6c60\u3002Python\u63d0\u4f9b\u4e86<code>concurrent.futures.ThreadPoolExecutor<\/code>\u7c7b\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u6c60\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>def task(name):<\/p>\n<p>    print(f&quot;Task {name} is running&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:<\/p>\n<p>    # \u63d0\u4ea4\u4efb\u52a1<\/p>\n<p>    future1 = executor.submit(task, &quot;Task-1&quot;)<\/p>\n<p>    future2 = executor.submit(task, &quot;Task-2&quot;)<\/p>\n<p>    # \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210<\/p>\n<p>    concurrent.futures.wait([future1, future2])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u5bf9\u8c61<code>executor<\/code>\uff0c\u5e76\u4f7f\u7528<code>submit<\/code>\u65b9\u6cd5\u63d0\u4ea4\u4efb\u52a1\u3002\u901a\u8fc7\u8c03\u7528<code>concurrent.futures.wait<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u3002\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6709\u6548\u5730\u7ba1\u7406\u548c\u8c03\u5ea6\u7ebf\u7a0b\uff0c\u907f\u514d\u521b\u5efa\u548c\u9500\u6bc1\u5927\u91cf\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5b88\u62a4\u7ebf\u7a0b<\/h2>\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\u4e3a\u4e86\u521b\u5efa\u5b88\u62a4\u7ebf\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u542f\u52a8\u7ebf\u7a0b\u4e4b\u524d\u8c03\u7528<code>setDaemon<\/code>\u65b9\u6cd5\u6216\u5c06<code>daemon<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3a<code>True<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>class MyThread(threading.Thread):<\/p>\n<p>    def __init__(self, thread_id, name, counter):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.thread_id = thread_id<\/p>\n<p>        self.name = name<\/p>\n<p>        self.counter = counter<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Starting thread: {self.name}&quot;)<\/p>\n<p>        while True:<\/p>\n<p>            print(f&quot;Thread {self.name} is running&quot;)<\/p>\n<p>            time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = MyThread(1, &quot;Thread-1&quot;, 1)<\/p>\n<p>thread1.setDaemon(True)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<p>print(&quot;Main thread exiting&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b<code>thread1<\/code>\uff0c\u5e76\u8c03\u7528<code>setDaemon<\/code>\u65b9\u6cd5\u5c06\u5176\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u3002\u5b88\u62a4\u7ebf\u7a0b\u5c06\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u81ea\u52a8\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784<\/h2>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\u6765\u7ba1\u7406\u5171\u4eab\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982<code>queue.Queue<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528<code>queue.Queue<\/code>\u6765\u7ba1\u7406\u7ebf\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>class Producer(threading.Thread):<\/p>\n<p>    def __init__(self, thread_id, name, data_queue):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.thread_id = thread_id<\/p>\n<p>        self.name = name<\/p>\n<p>        self.data_queue = data_queue<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Starting producer: {self.name}&quot;)<\/p>\n<p>        for i in range(5):<\/p>\n<p>            item = f&quot;Item-{i}&quot;<\/p>\n<p>            self.data_queue.put(item)<\/p>\n<p>            print(f&quot;Producer {self.name} produced {item}&quot;)<\/p>\n<p>        print(f&quot;Exiting producer: {self.name}&quot;)<\/p>\n<p>class Consumer(threading.Thread):<\/p>\n<p>    def __init__(self, thread_id, name, data_queue):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.thread_id = thread_id<\/p>\n<p>        self.name = name<\/p>\n<p>        self.data_queue = data_queue<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Starting consumer: {self.name}&quot;)<\/p>\n<p>        while True:<\/p>\n<p>            item = self.data_queue.get()<\/p>\n<p>            if item is None:<\/p>\n<p>                break<\/p>\n<p>            print(f&quot;Consumer {self.name} consumed {item}&quot;)<\/p>\n<p>        print(f&quot;Exiting consumer: {self.name}&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u961f\u5217\u5bf9\u8c61<\/strong><\/h2>\n<p>data_queue = queue.Queue()<\/p>\n<h2><strong>\u521b\u5efa\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b<\/strong><\/h2>\n<p>producer = Producer(1, &quot;Producer-1&quot;, data_queue)<\/p>\n<p>consumer = Consumer(2, &quot;Consumer-1&quot;, data_queue)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>producer.start()<\/p>\n<p>consumer.start()<\/p>\n<h2><strong>\u7b49\u5f85\u751f\u4ea7\u8005\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>producer.join()<\/p>\n<h2><strong>\u5411\u961f\u5217\u6dfb\u52a0None\uff0c\u4ee5\u901a\u77e5\u6d88\u8d39\u8005\u7ebf\u7a0b\u9000\u51fa<\/strong><\/h2>\n<p>data_queue.put(None)<\/p>\n<h2><strong>\u7b49\u5f85\u6d88\u8d39\u8005\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>consumer.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u5bf9\u8c61<code>data_queue<\/code>\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b\u3002\u751f\u4ea7\u8005\u7ebf\u7a0b\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u901a\u8fc7\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5316\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u6570\u636e\u7ba1\u7406\u548c\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u7ebf\u7a0b\u7684\u72b6\u6001\u7ba1\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u7ebf\u7a0b\u7684\u72b6\u6001\uff0c\u4f8b\u5982\u542f\u52a8\u3001\u6682\u505c\u3001\u6062\u590d\u548c\u7ec8\u6b62\u7ebf\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7ba1\u7406\u7ebf\u7a0b\u7684\u72b6\u6001\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>class MyThread(threading.Thread):<\/p>\n<p>    def __init__(self, thread_id, name, counter):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.thread_id = thread_id<\/p>\n<p>        self.name = name<\/p>\n<p>        self.counter = counter<\/p>\n<p>        self.paused = threading.Event()<\/p>\n<p>        self.paused.set()<\/p>\n<p>        self.stopped = threading.Event()<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Starting thread: {self.name}&quot;)<\/p>\n<p>        while not self.stopped.is_set():<\/p>\n<p>            self.paused.wait()<\/p>\n<p>            print(f&quot;Thread {self.name} is running&quot;)<\/p>\n<p>            time.sleep(1)<\/p>\n<p>        print(f&quot;Exiting thread: {self.name}&quot;)<\/p>\n<p>    def pause(self):<\/p>\n<p>        self.paused.clear()<\/p>\n<p>    def resume(self):<\/p>\n<p>        self.paused.set()<\/p>\n<p>    def stop(self):<\/p>\n<p>        self.stopped.set()<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = MyThread(1, &quot;Thread-1&quot;, 1)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<\/strong><\/h2>\n<p>time.sleep(3)<\/p>\n<p>thread1.pause()<\/p>\n<p>print(&quot;Thread paused&quot;)<\/p>\n<p>time.sleep(3)<\/p>\n<p>thread1.resume()<\/p>\n<p>print(&quot;Thread resumed&quot;)<\/p>\n<p>time.sleep(3)<\/p>\n<p>thread1.stop()<\/p>\n<p>print(&quot;Thread stopped&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>MyThread<\/code>\u7684\u7c7b\uff0c\u5e76\u6dfb\u52a0\u4e86<code>pause<\/code>\u3001<code>resume<\/code>\u548c<code>stop<\/code>\u65b9\u6cd5\u6765\u7ba1\u7406\u7ebf\u7a0b\u7684\u72b6\u6001\u3002\u901a\u8fc7\u4f7f\u7528\u4e8b\u4ef6\u5bf9\u8c61<code>paused<\/code>\u548c<code>stopped<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u7ebf\u7a0b\u7684\u6267\u884c\u3001\u6682\u505c\u548c\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u7ebf\u7a0b\u5f02\u5e38\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5728\u7ebf\u7a0b\u4e2d\u5904\u7406\u5f02\u5e38\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, thread_id, name, counter):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.thread_id = thread_id<\/p>\n<p>        self.name = name<\/p>\n<p>        self.counter = counter<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Starting thread: {self.name}&quot;)<\/p>\n<p>        try:<\/p>\n<p>            # \u5728\u6b64\u5904\u6dfb\u52a0\u7ebf\u7a0b\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<p>            raise ValueError(&quot;An error occurred&quot;)<\/p>\n<p>        except Exception as e:<\/p>\n<p>            print(f&quot;Exception in thread {self.name}: {e}&quot;)<\/p>\n<p>        print(f&quot;Exiting thread: {self.name}&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = MyThread(1, &quot;Thread-1&quot;, 1)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728<code>run<\/code>\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u4e86\u5f02\u5e38\u5904\u7406\u4ee3\u7801\uff0c\u901a\u8fc7\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u7684\u7c7b\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u521b\u5efa\u7ebf\u7a0b\u7c7b\u3001\u7ee7\u627f<code>Thread<\/code>\u7c7b\u3001\u5b9e\u73b0<code>run<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u9501\u673a\u5236\u6765\u907f\u514d\u7ebf\u7a0b\u95f4\u7ade\u4e89\u662f\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\u3001\u7ebf\u7a0b\u6c60\u3001\u5b88\u62a4\u7ebf\u7a0b\u3001\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\u3001\u7ebf\u7a0b\u72b6\u6001\u7ba1\u7406\u548c\u7ebf\u7a0b\u5f02\u5e38\u5904\u7406\u6765\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u548c\u5065\u58ee\u7684\u591a\u7ebf\u7a0b\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u4f18\u52bf\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u591a\u7ebf\u7a0b\u7f16\u7a0b\u6280\u672f\uff0c\u5e76\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u548c\u7ade\u4e89\u6761\u4ef6\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u7684\u7c7b\u4e2d\u4f7f\u7528\u591a\u7ebf\u7a0b\u7684\u597d\u5904\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u7684\u7c7b\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\u3002\u591a\u7ebf\u7a0b\u5141\u8bb8\u7a0b\u5e8f\u540c\u65f6\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff0c\u51cf\u5c11\u7b49\u5f85\u65f6\u95f4\uff0c\u4ece\u800c\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u7c7b\u7ed3\u6784\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\uff0c\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5b9e\u73b0\u591a\u7ebf\u7a0b\u65f6\uff0c\u9700\u5173\u6ce8\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u7a0b\u5e8f\u5d29\u6e83\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u9501\uff08\u5982<code>threading.Lock<\/code>\uff09\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u505a\u6cd5\u3002\u6b64\u5916\uff0c\u8fd8\u9700\u8003\u8651\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\uff0c\u786e\u4fdd\u7ebf\u7a0b\u5728\u5b8c\u6210\u4efb\u52a1\u540e\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u6570\u636e\u7684\u4e00\u81f4\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u9501\u673a\u5236\u6216\u5176\u4ed6\u540c\u6b65\u5de5\u5177\uff08\u5982<code>Semaphore<\/code>\u548c<code>Event<\/code>\uff09\u3002\u4f7f\u7528\u9501\u53ef\u4ee5\u5728\u67d0\u4e00\u65f6\u523b\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u7279\u5b9a\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u961f\u5217\uff08\u5982<code>queue.Queue<\/code>\uff09\u6765\u5728\u7ebf\u7a0b\u4e4b\u95f4\u5b89\u5168\u5730\u4f20\u9012\u6570\u636e\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6709\u5e8f\u5904\u7406\u548c\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7684\u7c7b\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528threading\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u521b\u5efa\u7ebf\u7a0b\u7c7b\u3001\u7ee7\u627fThrea [&hellip;]","protected":false},"author":3,"featured_media":1108010,"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\/1108001"}],"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=1108001"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108001\/revisions"}],"predecessor-version":[{"id":1108012,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108001\/revisions\/1108012"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108010"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}