{"id":1169524,"date":"2025-01-15T16:11:40","date_gmt":"2025-01-15T08:11:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169524.html"},"modified":"2025-01-15T16:11:43","modified_gmt":"2025-01-15T08:11:43","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%b9%b6%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169524.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5e76\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070457\/fdcf29f9-8ad5-4199-8b6a-8f8b97e37960.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5e76\u884c\" \/><\/p>\n<p><p> <strong>Python\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5e76\u884c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u3001<code>concurrent.futures<\/code>\u6a21\u5757\u3001\u4ee5\u53ca<code>multiprocessing<\/code>\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c<strong><code>threading<\/code>\u6a21\u5757<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<strong><code>concurrent.futures<\/code>\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\uff0c\u7b80\u5316\u4e86\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7684\u4f7f\u7528\u3002<strong><code>multiprocessing<\/code>\u6a21\u5757<\/strong>\u5219\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u7ed5\u8fc7GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09\u7684\u9650\u5236\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u5c55\u5f00<code>threading<\/code>\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong><code>threading<\/code>\u6a21\u5757\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5e76\u884c<\/strong><\/p>\n<\/p>\n<p><p><code>threading<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u5b83\u5141\u8bb8\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u4e0b\u9762\u662f\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5e76\u884c\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u7840\u77e5\u8bc6<\/h3>\n<\/p>\n<p><h4>1\u3001\u7ebf\u7a0b\u4e0e\u8fdb\u7a0b\u7684\u533a\u522b<\/h4>\n<\/p>\n<p><p>\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u4e2d\u7684\u4e00\u4e2a\u6267\u884c\u5355\u5143\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u7ebf\u7a0b\u3002\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u5982\u5185\u5b58\u7a7a\u95f4\u548c\u6587\u4ef6\u53e5\u67c4\u3002\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u6bd4\u8fdb\u7a0b\u66f4\u8f7b\u91cf\u7ea7\uff0c\u56e0\u6b64\u9002\u7528\u4e8e\u9700\u8981\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09<\/h4>\n<\/p>\n<p><p>Python\u4e2d\u7684GIL\u9650\u5236\u4e86\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cPython\u5b57\u8282\u7801\uff0c\u5c3d\u7ba1\u8fd9\u4f7f\u5f97\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u7684\u6027\u80fd\u63d0\u5347\u6709\u9650\uff0c\u4f46\u5728I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u591a\u7ebf\u7a0b\u4ecd\u7136\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001<code>threading<\/code>\u6a21\u5757\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>threading.Thread<\/code>\u7c7b\u6216\u8005\u76f4\u63a5\u521b\u5efa<code>Thread<\/code>\u5bf9\u8c61\u6765\u521b\u5efa\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<h2><strong>\u65b9\u5f0f\u4e00\uff1a\u7ee7\u627fThread\u7c7b<\/strong><\/h2>\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>\u65b9\u5f0f\u4e8c\uff1a\u76f4\u63a5\u521b\u5efaThread\u5bf9\u8c61<\/strong><\/h2>\n<p>def thread_function(name):<\/p>\n<p>    print(f&#39;Thread {name} is running&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = MyThread(name=&#39;Thread-1&#39;)<\/p>\n<p>thread2 = threading.Thread(target=thread_function, args=(&#39;Thread-2&#39;,))<\/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(&#39;All threads are done&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ebf\u7a0b\u540c\u6b65<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u5171\u4eab\u540c\u4e00\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u7ade\u4e89\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>Lock<\/code>\u3001<code>RLock<\/code>\u3001<code>Semaphore<\/code>\u3001<code>Event<\/code>\u7b49\u540c\u6b65\u539f\u8bed\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<h2><strong>\u4f7f\u7528Lock\u8fdb\u884c\u7ebf\u7a0b\u540c\u6b65<\/strong><\/h2>\n<p>lock = threading.Lock()<\/p>\n<p>counter = 0<\/p>\n<p>def increment_counter():<\/p>\n<p>    global counter<\/p>\n<p>    for _ in range(100000):<\/p>\n<p>        lock.acquire()<\/p>\n<p>        counter += 1<\/p>\n<p>        lock.release()<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for _ in range(10):<\/p>\n<p>    thread = threading.Thread(target=increment_counter)<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>print(f&#39;Final counter value: {counter}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ebf\u7a0b\u6c60<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u53ef\u4ee5\u7b80\u5316\u7ebf\u7a0b\u7684\u7ba1\u7406\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u521b\u5efa\u5927\u91cf\u7ebf\u7a0b\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def thread_function(name):<\/p>\n<p>    print(f&#39;Thread {name} is running&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    futures = [executor.submit(thread_function, f&#39;Thread-{i}&#39;) for i in range(10)]<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for future in futures:<\/p>\n<p>    future.result()<\/p>\n<p>print(&#39;All threads are done&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u7b49\u3002\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u65f6\u5207\u6362\u5230\u5176\u4ed6\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import requests<\/p>\n<p>def fetch_url(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    print(f&#39;{url}: {response.status_code}&#39;)<\/p>\n<p>urls = [&#39;https:\/\/www.example.com&#39;, &#39;https:\/\/www.python.org&#39;, &#39;https:\/\/www.github.com&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = [threading.Thread(target=fetch_url, args=(url,)) for url in urls]<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>print(&#39;All URLs fetched&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u540e\u53f0\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u6765\u6267\u884c\u540e\u53f0\u4efb\u52a1\uff0c\u800c\u4e0d\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u6267\u884c\u3002\u4f8b\u5982\uff0c\u5728GUI\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u6765\u5904\u7406\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u4ee5\u4fdd\u6301\u754c\u9762\u7684\u54cd\u5e94\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>import tkinter as tk<\/p>\n<p>def long_running_task():<\/p>\n<p>    time.sleep(5)<\/p>\n<p>    print(&#39;Task completed&#39;)<\/p>\n<p>def start_task():<\/p>\n<p>    thread = threading.Thread(target=long_running_task)<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u521b\u5efaGUI<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>button = tk.Button(root, text=&#39;Start Task&#39;, command=start_task)<\/p>\n<p>button.pack()<\/p>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001<code>threading<\/code>\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u7ebf\u7a0b\u95f4\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>queue.Queue<\/code>\u7c7b\u5728\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\uff0c\u907f\u514d\u76f4\u63a5\u4f7f\u7528\u5171\u4eab\u53d8\u91cf\u5e26\u6765\u7684\u590d\u6742\u6027\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):<\/p>\n<p>    for i in range(5):<\/p>\n<p>        time.sleep(1)<\/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>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&#39;Consumed {item}&#39;)<\/p>\n<p>        q.task_done()<\/p>\n<p>q = queue.Queue()<\/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, args=(q,))<\/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<h2><strong>\u7b49\u5f85\u751f\u4ea7\u8005\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>producer_thread.join()<\/p>\n<h2><strong>\u53d1\u9001\u505c\u6b62\u4fe1\u53f7<\/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 are done&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9a\u65f6\u5668<\/h4>\n<\/p>\n<p><p><code>threading.Timer<\/code>\u7c7b\u7528\u4e8e\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u540e\u6267\u884c\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def delayed_task():<\/p>\n<p>    print(&#39;Task executed&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u5b9a\u65f6\u5668<\/strong><\/h2>\n<p>timer = threading.Timer(5, delayed_task)<\/p>\n<p>timer.start()<\/p>\n<p>print(&#39;Timer started&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001GIL\u7684\u5f71\u54cd<\/h4>\n<\/p>\n<p><p>\u7531\u4e8eGIL\u7684\u5b58\u5728\uff0c\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u7684\u6027\u80fd\u63d0\u5347\u6709\u9650\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u6b7b\u9501<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u9501\u8fdb\u884c\u7ebf\u7a0b\u540c\u6b65\u65f6\uff0c\u9700\u5c0f\u5fc3\u907f\u514d\u6b7b\u9501\u7684\u53d1\u751f\u3002\u53ef\u4ee5\u901a\u8fc7\u5c3d\u91cf\u51cf\u5c11\u9501\u7684\u4f7f\u7528\uff0c\u6216\u8005\u4f7f\u7528<code>RLock<\/code>\uff08\u53ef\u91cd\u5165\u9501\uff09\u6765\u964d\u4f4e\u6b7b\u9501\u7684\u98ce\u9669\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>    lock1.acquire()<\/p>\n<p>    time.sleep(1)<\/p>\n<p>    lock2.acquire()<\/p>\n<p>    print(&#39;Task 1 completed&#39;)<\/p>\n<p>    lock2.release()<\/p>\n<p>    lock1.release()<\/p>\n<p>def task2():<\/p>\n<p>    lock2.acquire()<\/p>\n<p>    time.sleep(1)<\/p>\n<p>    lock1.acquire()<\/p>\n<p>    print(&#39;Task 2 completed&#39;)<\/p>\n<p>    lock1.release()<\/p>\n<p>    lock2.release()<\/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>print(&#39;All tasks are done&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982<code>queue.Queue<\/code>\u3001<code>collections.deque<\/code>\u7b49\uff0c\u907f\u514d\u56e0\u5171\u4eab\u6570\u636e\u5f15\u53d1\u7684\u7ade\u4e89\u6761\u4ef6\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&#39;Produced {i}&#39;)<\/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&#39;Consumed {item}&#39;)<\/p>\n<p>        q.task_done()<\/p>\n<p>q = queue.Queue()<\/p>\n<p>producer_thread = threading.Thread(target=producer, args=(q,))<\/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)<\/p>\n<p>consumer_thread.join()<\/p>\n<p>print(&#39;All tasks are done&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5e76\u884c\uff0c\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u548c\u9700\u8981\u540e\u53f0\u6267\u884c\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u5408\u7406\u7684\u7ebf\u7a0b\u540c\u6b65\u548c\u7ebf\u7a0b\u95f4\u901a\u4fe1\uff0c\u53ef\u4ee5\u907f\u514d\u8d44\u6e90\u7ade\u4e89\u548c\u6b7b\u9501\u95ee\u9898\u3002\u6b64\u5916\uff0c\u9ad8\u7ea7\u7528\u6cd5\u5982\u7ebf\u7a0b\u6c60\u548c\u5b9a\u65f6\u5668\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7b80\u5316\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8eGIL\u7684\u5b58\u5728\uff0c\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u7684\u6027\u80fd\u63d0\u5347\u6709\u9650\uff0c\u6b64\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u591a\u7ebf\u7a0b\u5e76\u884c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u90fd\u662f\u5b9e\u73b0\u5e76\u884c\u5904\u7406\u7684\u6280\u672f\u3002\u591a\u7ebf\u7a0b\u662f\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u5185\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u5982\u5185\u5b58\u548c\u6587\u4ef6\u53e5\u67c4\uff0c\u9002\u7528\u4e8eIO\u5bc6\u96c6\u578b\u4efb\u52a1\u3002\u591a\u8fdb\u7a0b\u5219\u662f\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\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\u901a\u8fc7<code>threading.Thread<\/code>\u7c7b\uff0c\u53ef\u4ee5\u5b9a\u4e49\u7ebf\u7a0b\u7684\u76ee\u6807\u51fd\u6570\uff0c\u5e76\u901a\u8fc7<code>start()<\/code>\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\u3002\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u3002\u4e3a\u4e86\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Lock<\/code>\u7c7b\u6765\u63a7\u5236\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u5728Python\u4e2d\u5b58\u5728\u54ea\u4e9b\u9650\u5236\uff1f<\/strong><br \/>Python\u7684\u591a\u7ebf\u7a0b\u53d7\u5230\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u7684\u9650\u5236\uff0c\u8fd9\u610f\u5473\u7740\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u6267\u884cPython\u5b57\u8282\u7801\u3002\u8fd9\u4f1a\u5bfc\u81f4\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u591a\u7ebf\u7a0b\u7684\u6027\u80fd\u63d0\u5347\u4e0d\u660e\u663e\u3002\u7136\u800c\uff0c\u5bf9\u4e8eIO\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u548c\u6587\u4ef6\u64cd\u4f5c\uff0c\u591a\u7ebf\u7a0b\u4ecd\u7136\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002\u9009\u62e9\u5408\u9002\u7684\u4efb\u52a1\u7c7b\u578b\u5bf9\u4e8e\u6709\u6548\u5229\u7528\u591a\u7ebf\u7a0b\u81f3\u5173\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5e76\u884c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528threading\u6a21\u5757\u3001concurrent.futures\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1169532,"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\/1169524"}],"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=1169524"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169524\/revisions"}],"predecessor-version":[{"id":1169534,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169524\/revisions\/1169534"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169532"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}