{"id":1180597,"date":"2025-01-15T18:39:08","date_gmt":"2025-01-15T10:39:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180597.html"},"modified":"2025-01-15T18:39:27","modified_gmt":"2025-01-15T10:39:27","slug":"python3-6%e5%a6%82%e4%bd%95%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1180597.html","title":{"rendered":"python3.6\u5982\u4f55\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\/25125249\/17bd4560-dd8b-4933-9625-479576ef684b.webp\" alt=\"python3.6\u5982\u4f55\u591a\u7ebf\u7a0b\" \/><\/p>\n<p><p> \u5728Python 3.6\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>threading<\/code>\u6a21\u5757\u3002<strong>\u7ebf\u7a0b\u5141\u8bb8\u7a0b\u5e8f\u5728\u591a\u4e2a\u4efb\u52a1\u4e4b\u95f4\u6765\u56de\u5207\u6362\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u6027\u80fd<\/strong>\u3001<strong>\u7279\u522b\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\u3001<strong>\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa<code>Thread<\/code>\u5bf9\u8c61\u5e76\u8c03\u7528\u5176<code>start<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u65b0\u7ebf\u7a0b<\/strong>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Python 3.6\u4e2d\u4f7f\u7528\u591a\u7ebf\u7a0b\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(10):<\/p>\n<p>        print(f&quot;Number: {i}&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def print_letters():<\/p>\n<p>    for letter in &#39;abcdefghij&#39;:<\/p>\n<p>        print(f&quot;Letter: {letter}&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_letters)<\/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\u5b8c\u6210<\/strong><\/h2>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p>print(&quot;Done!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>print_numbers<\/code>\u548c<code>print_letters<\/code>\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u51fd\u6570\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u7ebf\u7a0b<code>thread1<\/code>\u548c<code>thread2<\/code>\uff0c\u5206\u522b\u6267\u884c\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002\u901a\u8fc7\u8c03\u7528<code>start<\/code>\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\uff0c\u7136\u540e\u4f7f\u7528<code>join<\/code>\u65b9\u6cd5\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7ebf\u7a0b\u7684\u57fa\u7840\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u8fdb\u884c\u8fd0\u7b97\u8c03\u5ea6\u7684\u6700\u5c0f\u5355\u4f4d\u3002\u5b83\u88ab\u5305\u542b\u5728\u8fdb\u7a0b\u4e4b\u4e2d\uff0c\u662f\u8fdb\u7a0b\u4e2d\u7684\u5b9e\u9645\u8fd0\u4f5c\u5355\u4f4d\u3002\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u7ec4\u6210\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u8fdb\u7a0b\u7684\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u7ebf\u7a0b\u548c\u8fdb\u7a0b\u7684\u533a\u522b<\/h4>\n<\/p>\n<ul>\n<li><strong>\u8fdb\u7a0b<\/strong>\u662f\u8d44\u6e90\u5206\u914d\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/li>\n<li><strong>\u7ebf\u7a0b<\/strong>\u662fCPU\u8c03\u5ea6\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u4f46\u6709\u5404\u81ea\u7684\u6808\u7a7a\u95f4\u548c\u6307\u4ee4\u8ba1\u6570\u5668\u3002<\/li>\n<\/ul>\n<p><h4>1.2 \u7ebf\u7a0b\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7ebf\u7a0b\u4e4b\u95f4\u901a\u4fe1\u65b9\u4fbf\uff0c\u56e0\u4e3a\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u5730\u5740\u7a7a\u95f4\u3002<\/li>\n<li>\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500\u6bd4\u8fdb\u7a0b\u5c0f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u591a\u7ebf\u7a0b\u7f16\u7a0b\u6bd4\u5355\u7ebf\u7a0b\u590d\u6742\uff0c\u5bb9\u6613\u51fa\u73b0\u7ade\u6001\u6761\u4ef6\u548c\u6b7b\u9501\u7b49\u95ee\u9898\u3002<\/li>\n<li>\u7531\u4e8e\u7ebf\u7a0b\u5171\u4eab\u5185\u5b58\uff0c\u9700\u8981\u4f7f\u7528\u540c\u6b65\u673a\u5236\u9632\u6b62\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u548c\u542f\u52a8\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u521b\u5efa\u548c\u542f\u52a8\u7ebf\u7a0b\u4e3b\u8981\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528Thread\u7c7b\u521b\u5efa\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u7ebf\u7a0b\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528<code>threading.Thread<\/code>\u7c7b\u3002\u901a\u8fc7\u4f20\u9012\u76ee\u6807\u51fd\u6570\u548c\u53c2\u6570\u6765\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528<code>start<\/code>\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Worker thread is running&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=worker)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528\u7ee7\u627fThread\u7c7b\u521b\u5efa\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u521b\u5efa\u7ebf\u7a0b\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u7ee7\u627f<code>threading.Thread<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199\u5176<code>run<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class MyThread(threading.Thread):<\/p>\n<p>    def run(self):<\/p>\n<p>        print(&quot;MyThread is running&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = MyThread()<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ebf\u7a0b\u540c\u6b65\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u540c\u6b65\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u540c\u6b65\u673a\u5236\uff0c\u5982\u9501\u3001\u6761\u4ef6\u53d8\u91cf\u3001\u4e8b\u4ef6\u548c\u4fe1\u53f7\u91cf\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528\u9501\uff08Lock\uff09<\/h4>\n<\/p>\n<p><p>\u9501\u662f\u6700\u57fa\u672c\u7684\u540c\u6b65\u673a\u5236\uff0c\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u9632\u6b62\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>shared_resource = 0<\/p>\n<p>def increment():<\/p>\n<p>    global shared_resource<\/p>\n<p>    lock.acquire()<\/p>\n<p>    try:<\/p>\n<p>        for _ in range(1000000):<\/p>\n<p>            shared_resource += 1<\/p>\n<p>    finally:<\/p>\n<p>        lock.release()<\/p>\n<p>thread1 = threading.Thread(target=increment)<\/p>\n<p>thread2 = threading.Thread(target=increment)<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p>print(shared_resource)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u9501\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90<code>shared_resource<\/code>\uff0c\u9632\u6b62\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u5b83\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\uff08Condition\uff09<\/h4>\n<\/p>\n<p><p>\u6761\u4ef6\u53d8\u91cf\u5141\u8bb8\u7ebf\u7a0b\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u901a\u77e5\u5176\u4ed6\u7ebf\u7a0b\u3002\u5b83\u901a\u5e38\u4e0e\u9501\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>condition = threading.Condition()<\/p>\n<p>items = []<\/p>\n<p>def consumer():<\/p>\n<p>    with condition:<\/p>\n<p>        while not items:<\/p>\n<p>            condition.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t()<\/p>\n<p>        item = items.pop()<\/p>\n<p>        print(f&quot;Consumed: {item}&quot;)<\/p>\n<p>def producer():<\/p>\n<p>    with condition:<\/p>\n<p>        items.append(1)<\/p>\n<p>        condition.notify()<\/p>\n<p>thread1 = threading.Thread(target=consumer)<\/p>\n<p>thread2 = threading.Thread(target=producer)<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u7b49\u5f85\u6761\u4ef6\u53d8\u91cf\u6ee1\u8db3\uff08\u5373<code>items<\/code>\u5217\u8868\u4e0d\u4e3a\u7a7a\uff09\uff0c\u751f\u4ea7\u8005\u7ebf\u7a0b\u5728\u6dfb\u52a0\u9879\u76ee\u540e\u901a\u77e5\u6d88\u8d39\u8005\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ebf\u7a0b\u6c60<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u7ebf\u7a0b\u7ba1\u7406\u65b9\u5f0f\uff0c\u5b83\u7ef4\u62a4\u4e00\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\uff0c\u4efb\u52a1\u63d0\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u540e\uff0c\u7531\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u6267\u884c\u3002Python 3.6\u63d0\u4f9b\u4e86<code>concurrent.futures<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u7ebf\u7a0b\u6c60\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528ThreadPoolExecutor<\/h4>\n<\/p>\n<p><p><code>ThreadPoolExecutor<\/code>\u662f<code>concurrent.futures<\/code>\u6a21\u5757\u4e2d\u5b9e\u73b0\u7ebf\u7a0b\u6c60\u7684\u7c7b\u3002<\/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;Task {n} is running&quot;)<\/p>\n<p>    return n * 2<\/p>\n<p>with ThreadPoolExecutor(max_workers=4) as executor:<\/p>\n<p>    futures = [executor.submit(task, i) for i in range(10)]<\/p>\n<p>    results = [future.result() for future in futures]<\/p>\n<p>print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u6700\u5927\u7ebf\u7a0b\u6570\u4e3a4\u3002\u6211\u4eec\u63d0\u4ea4\u4e8610\u4e2a\u4efb\u52a1\u7ed9\u7ebf\u7a0b\u6c60\uff0c\u5e76\u83b7\u53d6\u4e86\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>5.1 GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09<\/h4>\n<\/p>\n<p><p>Python\u7684GIL\u9650\u5236\u4e86\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u6267\u884cPython\u5b57\u8282\u7801\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u964d\u4f4e\u591a\u7ebf\u7a0b\u7684\u6027\u80fd\u3002\u867d\u7136GIL\u5b58\u5728\uff0c\u4f46\u591a\u7ebf\u7a0b\u5728I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u4ecd\u7136\u6709\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u7ade\u6001\u6761\u4ef6\u548c\u6b7b\u9501<\/h4>\n<\/p>\n<p><p>\u7ade\u6001\u6761\u4ef6\u548c\u6b7b\u9501\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u3002\u4f7f\u7528\u9002\u5f53\u7684\u540c\u6b65\u673a\u5236\uff08\u5982\u9501\u3001\u6761\u4ef6\u53d8\u91cf\uff09\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock1 = threading.Lock()<\/p>\n<p>lock2 = threading.Lock()<\/p>\n<p>def task1():<\/p>\n<p>    with lock1:<\/p>\n<p>        print(&quot;Task 1 acquired lock1&quot;)<\/p>\n<p>        with lock2:<\/p>\n<p>            print(&quot;Task 1 acquired lock2&quot;)<\/p>\n<p>def task2():<\/p>\n<p>    with lock2:<\/p>\n<p>        print(&quot;Task 2 acquired lock2&quot;)<\/p>\n<p>        with lock1:<\/p>\n<p>            print(&quot;Task 2 acquired lock1&quot;)<\/p>\n<p>thread1 = threading.Thread(target=task1)<\/p>\n<p>thread2 = threading.Thread(target=task2)<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u6b7b\u9501\uff0c\u56e0\u4e3a\u4e24\u4e2a\u7ebf\u7a0b\u4e92\u76f8\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u9501\u3002\u907f\u514d\u6b7b\u9501\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u6309\u987a\u5e8f\u83b7\u53d6\u9501\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ebf\u7a0b\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><h4>6.1 \u5b88\u62a4\u7ebf\u7a0b<\/h4>\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\u7684<code>daemon<\/code>\u5c5e\u6027\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def daemon_task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Daemon thread is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>daemon_thread = threading.Thread(target=daemon_task)<\/p>\n<p>daemon_thread.daemon = True<\/p>\n<p>daemon_thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>print(&quot;Main thread is exiting&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u81ea\u52a8\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u7ebf\u7a0b\u95f4\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>\u7ebf\u7a0b\u4e4b\u95f4\u53ef\u4ee5\u901a\u8fc7\u961f\u5217\u8fdb\u884c\u901a\u4fe1\u3002Python\u63d0\u4f9b\u4e86<code>queue<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>def producer(q):<\/p>\n<p>    for i in range(5):<\/p>\n<p>        q.put(i)<\/p>\n<p>        print(f&quot;Produced: {i}&quot;)<\/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;Consumed: {item}&quot;)<\/p>\n<p>q = queue.Queue()<\/p>\n<p>thread1 = threading.Thread(target=producer, args=(q,))<\/p>\n<p>thread2 = threading.Thread(target=consumer, args=(q,))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>q.put(None)<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u751f\u4ea7\u8005\u7ebf\u7a0b\u5c06\u9879\u76ee\u653e\u5165\u961f\u5217\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u591a\u7ebf\u7a0b\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u901a\u4fe1\u7b49\u3002\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u591a\u7ebf\u7a0b\u53ef\u80fd\u4e0d\u4f1a\u5e26\u6765\u6027\u80fd\u63d0\u5347\uff0c\u751a\u81f3\u53ef\u80fd\u56e0\u4e3aGIL\u800c\u964d\u4f4e\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u6587\u4ef6\u8bfb\u5199<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u53ef\u4ee5\u52a0\u5feb\u6587\u4ef6\u7684\u8bfb\u5199\u901f\u5ea6\uff0c\u5c24\u5176\u662f\u591a\u4e2a\u6587\u4ef6\u540c\u65f6\u8bfb\u5199\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def read_file(filename):<\/p>\n<p>    with open(filename, &#39;r&#39;) as file:<\/p>\n<p>        data = file.read()<\/p>\n<p>        print(f&quot;Read {len(data)} bytes from {filename}&quot;)<\/p>\n<p>filenames = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<p>threads = [threading.Thread(target=read_file, args=(filename,)) for filename in filenames]<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u7f51\u7edc\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u7f51\u7edc\u901a\u4fe1\u7684\u5e76\u53d1\u6027\uff0c\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import socket<\/p>\n<p>def handle_client(client_socket):<\/p>\n<p>    request = client_socket.recv(1024)<\/p>\n<p>    print(f&quot;Received: {request}&quot;)<\/p>\n<p>    client_socket.send(b&quot;ACK&quot;)<\/p>\n<p>    client_socket.close()<\/p>\n<p>server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>server.bind((&quot;0.0.0.0&quot;, 9999))<\/p>\n<p>server.listen(5)<\/p>\n<p>while True:<\/p>\n<p>    client_socket, addr = server.accept()<\/p>\n<p>    print(f&quot;Accepted connection from {addr}&quot;)<\/p>\n<p>    client_handler = threading.Thread(target=handle_client, args=(client_socket,))<\/p>\n<p>    client_handler.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python 3.6\u4e2d\uff0c\u591a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7<code>threading<\/code>\u6a21\u5757\u8f7b\u677e\u5b9e\u73b0\u3002<strong>\u7ebf\u7a0b\u5141\u8bb8\u7a0b\u5e8f\u5728\u591a\u4e2a\u4efb\u52a1\u4e4b\u95f4\u6765\u56de\u5207\u6362\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u6027\u80fd<\/strong>\u3002\u591a\u7ebf\u7a0b\u7279\u522b\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u6587\u4ef6\u8bfb\u5199\u548c\u7f51\u7edc\u901a\u4fe1\u3002\u901a\u8fc7\u9002\u5f53\u7684\u540c\u6b65\u673a\u5236\uff08\u5982\u9501\u3001\u6761\u4ef6\u53d8\u91cf\uff09\u53ef\u4ee5\u9632\u6b62\u7ade\u6001\u6761\u4ef6\u548c\u6b7b\u9501\u95ee\u9898\u3002\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u80fd\u4e0d\u4f1a\u5e26\u6765\u6027\u80fd\u63d0\u5347\uff0c\u751a\u81f3\u53ef\u80fd\u56e0\u4e3aGIL\u800c\u964d\u4f4e\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u7f16\u7a0b\u867d\u7136\u590d\u6742\uff0c\u4f46\u901a\u8fc7\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u8c03\u8bd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python 3.6\u4e2d\u7684\u591a\u7ebf\u7a0b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python 3.6\u4e2d\uff0c\u5982\u4f55\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff1f<\/strong><br \/>\u5728Python 3.6\u4e2d\uff0c\u521b\u5efa\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>Thread<\/code>\u7c7b\u6216\u4f7f\u7528<code>Thread<\/code>\u7c7b\u7684\u5b9e\u4f8b\u6765\u521b\u5efa\u7ebf\u7a0b\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9<code>Thread<\/code>\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u4f7f\u7528<code>start()<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u7ebf\u7a0b\uff0c\u5e76\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u6765\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<p><strong>Python 3.6\u7684\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u662f\u4e24\u79cd\u5e76\u53d1\u5904\u7406\u7684\u65b9\u5f0f\u3002\u591a\u7ebf\u7a0b\u901a\u8fc7\u5728\u540c\u4e00\u8fdb\u7a0b\u5185\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u5e76\u53d1\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u5171\u4eab\u540c\u4e00\u5185\u5b58\u7a7a\u95f4\u3002\u800c\u591a\u8fdb\u7a0b\u5219\u662f\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u5728Python\u4e2d\uff0c\u7531\u4e8e\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u7684\u5b58\u5728\uff0c\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u53ef\u80fd\u4e0d\u5982\u591a\u8fdb\u7a0b\u8868\u73b0\u51fa\u8272\uff0c\u4f46\u5728I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u591a\u7ebf\u7a0b\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python 3.6\u4e2d\u591a\u7ebf\u7a0b\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u7684\u5904\u7406\u81f3\u5173\u91cd\u8981\u3002\u53ef\u4ee5\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4e3a\u4e86\u786e\u4fdd\u4e3b\u7ebf\u7a0b\u80fd\u591f\u83b7\u53d6\u5230\u5b50\u7ebf\u7a0b\u7684\u5f02\u5e38\u4fe1\u606f\uff0c\u53ef\u4ee5\u5c06\u5f02\u5e38\u4fe1\u606f\u5b58\u50a8\u5728\u4e00\u4e2a\u5171\u4eab\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u6216\u8005\u4f7f\u7528<code>queue<\/code>\u6a21\u5757\u6765\u4f20\u9012\u5f02\u5e38\u4fe1\u606f\u3002\u8fd9\u6837\uff0c\u4e3b\u7ebf\u7a0b\u5728\u7b49\u5f85\u6240\u6709\u5b50\u7ebf\u7a0b\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u68c0\u67e5\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 3.6\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684threading\u6a21\u5757\u3002\u7ebf\u7a0b\u5141\u8bb8\u7a0b\u5e8f\u5728\u591a\u4e2a\u4efb\u52a1\u4e4b\u95f4\u6765\u56de\u5207 [&hellip;]","protected":false},"author":3,"featured_media":1180618,"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\/1180597"}],"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=1180597"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180597\/revisions"}],"predecessor-version":[{"id":1180623,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180597\/revisions\/1180623"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1180618"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}