{"id":10021223,"date":"2026-02-01T02:39:12","date_gmt":"2026-01-31T18:39:12","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021223"},"modified":"2026-02-01T02:39:12","modified_gmt":"2026-01-31T18:39:12","slug":"python%e4%b8%ad%e7%9a%84%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e4%b8%8egil%e6%9c%ba%e5%88%b6%e4%bc%98%e5%8c%96%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021223","title":{"rendered":"Python\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u4e0eGIL\u673a\u5236\u4f18\u5316\u7b56\u7565"},"content":{"rendered":"<h2>Python\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u4e0eGIL\u673a\u5236\u4f18\u5316\u7b56\u7565<\/h2>\n<h3>1. \u5e76\u53d1\u7f16\u7a0b\u6982\u8ff0<\/h3>\n<h4>1.1 \u5e76\u53d1\u4e0e\u5e76\u884c\u7684\u533a\u522b<\/h4>\n<ul>\n<li><strong>\u5e76\u53d1<\/strong>\uff08Concurrency\uff09\uff1a\u6307\u4e24\u4e2a\u6216\u591a\u4e2a\u4efb\u52a1\u5728\u540c\u4e00\u65f6\u95f4\u6bb5\u5185\u4ea4\u66ff\u6267\u884c\uff0c\u901a\u8fc7\u4e0a\u4e0b\u6587\u5207\u6362\u5b9e\u73b0<\/li>\n<li><strong>\u5e76\u884c<\/strong>\uff08Parallelism\uff09\uff1a\u6307\u4e24\u4e2a\u6216\u591a\u4e2a\u4efb\u52a1\u5728\u540c\u4e00\u65f6\u523b\u540c\u65f6\u6267\u884c\uff0c\u9700\u8981\u591a\u6838CPU\u652f\u6301<\/li>\n<\/ul>\n<h4>1.2 Python\u4e2d\u7684\u5e76\u53d1\u6a21\u578b<\/h4>\n<p>Python\u652f\u6301\u591a\u79cd\u5e76\u53d1\u6a21\u578b\uff1a<\/p>\n<ul>\n<li><strong>\u591a\u7ebf\u7a0b<\/strong>\uff08Threading\uff09\uff1a\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/li>\n<li><strong>\u591a\u8fdb\u7a0b<\/strong>\uff08Multiprocessing\uff09\uff1a\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1<\/li>\n<li><strong>\u534f\u7a0b<\/strong>\uff08Coroutine\uff09\uff1a\u8f7b\u91cf\u7ea7\u5e76\u53d1\uff0c\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/li>\n<li><strong>\u5f02\u6b65I\/O<\/strong>\uff08Asyncio\uff09\uff1a\u57fa\u4e8e\u534f\u7a0b\u7684\u5f02\u6b65\u7f16\u7a0b\u6846\u67b6<\/li>\n<\/ul>\n<h4>1.3 \u5e76\u53d1\u7f16\u7a0b\u7684\u6311\u6218<\/h4>\n<ul>\n<li><strong>\u7ade\u6001\u6761\u4ef6<\/strong>\uff08Race Condition\uff09\uff1a\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u5bfc\u81f4\u7684\u6570\u636e\u4e0d\u4e00\u81f4<\/li>\n<li><strong>\u6b7b\u9501<\/strong>\uff08Deadlock\uff09\uff1a\u591a\u4e2a\u7ebf\u7a0b\u4e92\u76f8\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u8d44\u6e90<\/li>\n<li><strong>\u6d3b\u9501<\/strong>\uff08Livelock\uff09\uff1a\u7ebf\u7a0b\u4e0d\u65ad\u6539\u53d8\u72b6\u6001\u4f46\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c<\/li>\n<li><strong>\u8d44\u6e90\u4e89\u7528<\/strong>\uff1a\u7ebf\u7a0b\u7ade\u4e89\u6709\u9650\u8d44\u6e90\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d<\/li>\n<\/ul>\n<h3>2. GIL\u673a\u5236\u8be6\u89e3<\/h3>\n<h4>2.1 \u4ec0\u4e48\u662fGIL<\/h4>\n<p><strong>\u5168\u5c40\u89e3\u91ca\u5668\u9501<\/strong>\uff08Global Interpreter Lock\uff0cGIL\uff09\u662fPython\u89e3\u91ca\u5668\uff08CPython\uff09\u4e2d\u7684\u4e00\u4e2a\u673a\u5236\uff0c\u5b83\u786e\u4fdd\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u6267\u884cPython\u5b57\u8282\u7801\u3002<\/p>\n<h4>2.2 GIL\u7684\u5de5\u4f5c\u539f\u7406<\/h4>\n<ol>\n<li><strong>\u83b7\u53d6\u9501<\/strong>\uff1a\u7ebf\u7a0b\u6267\u884cPython\u4ee3\u7801\u524d\u5fc5\u987b\u83b7\u53d6GIL<\/li>\n<li><strong>\u6267\u884c\u4ee3\u7801<\/strong>\uff1a\u7ebf\u7a0b\u6267\u884c\u4e00\u6bb5\u65f6\u95f4\uff08\u7ea6100\u4e2a\u5b57\u8282\u7801\u6307\u4ee4\uff09<\/li>\n<li><strong>\u91ca\u653e\u9501<\/strong>\uff1a\u7ebf\u7a0b\u4e3b\u52a8\u91ca\u653eGIL\uff0c\u8ba9\u5176\u4ed6\u7ebf\u7a0b\u6709\u673a\u4f1a\u6267\u884c<\/li>\n<li><strong>\u91cd\u65b0\u7ade\u4e89<\/strong>\uff1a\u6240\u6709\u7ebf\u7a0b\u91cd\u65b0\u7ade\u4e89GIL<\/li>\n<\/ol>\n<h4>2.3 GIL\u7684\u5f71\u54cd<\/h4>\n<ul>\n<li><strong>CPU\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\uff1a\u591a\u7ebf\u7a0b\u65e0\u6cd5\u5229\u7528\u591a\u6838CPU\uff0c\u751a\u81f3\u53ef\u80fd\u6bd4\u5355\u7ebf\u7a0b\u6162<\/li>\n<li><strong>I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\uff1a\u7ebf\u7a0b\u5728I\/O\u64cd\u4f5c\u65f6\u91ca\u653eGIL\uff0c\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884c<\/li>\n<li><strong>\u5185\u5b58\u7ba1\u7406<\/strong>\uff1a\u7b80\u5316\u4e86\u5185\u5b58\u7ba1\u7406\uff0c\u907f\u514d\u4e86\u591a\u7ebf\u7a0b\u4e0b\u7684\u5185\u5b58\u7ade\u4e89<\/li>\n<\/ul>\n<h4>2.4 \u4e3a\u4ec0\u4e48\u5b58\u5728GIL<\/h4>\n<ul>\n<li><strong>\u5386\u53f2\u539f\u56e0<\/strong>\uff1a\u65e9\u671fPython\u8bbe\u8ba1\u65f6\u591a\u6838CPU\u4e0d\u666e\u53ca<\/li>\n<li><strong>\u7b80\u5316\u5b9e\u73b0<\/strong>\uff1a\u907f\u514d\u4e86\u590d\u6742\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/li>\n<li><strong>\u5185\u5b58\u7ba1\u7406<\/strong>\uff1a\u7b80\u5316\u4e86\u5783\u573e\u56de\u6536\u673a\u5236<\/li>\n<li><strong>\u7b2c\u4e09\u65b9\u5e93\u517c\u5bb9<\/strong>\uff1a\u8bb8\u591aC\u6269\u5c55\u4f9d\u8d56GIL\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168<\/li>\n<\/ul>\n<h3>3. \u591a\u7ebf\u7a0b\u7f16\u7a0b<\/h3>\n<h4>3.1 \u7ebf\u7a0b\u7684\u521b\u5efa\u4e0e\u4f7f\u7528<\/h4>\n<pre><code class=\"python\">import threading\nimport time\n\ndef worker(name, delay):\n    print(f&quot;Worker {name} started&quot;)\n    time.sleep(delay)\n    print(f&quot;Worker {name} finished&quot;)\n\n# \u521b\u5efa\u7ebf\u7a0b\nthread1 = threading.Thread(target=worker, args=(&quot;A&quot;, 2))\nthread2 = threading.Thread(target=worker, args=(&quot;B&quot;, 3))\n\n# \u542f\u52a8\u7ebf\u7a0b\nthread1.start()\nthread2.start()\n\n# \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\nthread1.join()\nthread2.join()\n\nprint(&quot;All workers finished&quot;)<\/code><\/pre>\n<h4>3.2 \u7ebf\u7a0b\u540c\u6b65\u673a\u5236<\/h4>\n<h5>3.2.1 \u9501\uff08Lock\uff09<\/h5>\n<pre><code class=\"python\">import threading\n\nlock = threading.Lock()\nshared_resource = 0\n\ndef increment():\n    global shared_resource\n    for _ in range(100000):\n        with lock:\n            shared_resource += 1\n\n# \u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\nthreads = []\nfor i in range(5):\n    t = threading.Thread(target=increment)\n    threads.append(t)\n    t.start()\n\n# \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\nfor t in threads:\n    t.join()\n\nprint(f&quot;Final value: {shared_resource}&quot;)  # \u5e94\u8f93\u51fa: 500000<\/code><\/pre>\n<h5>3.2.2 \u4fe1\u53f7\u91cf\uff08Semaphore\uff09<\/h5>\n<pre><code class=\"python\">import threading\nimport time\n\nsemaphore = threading.Semaphore(3)  # \u6700\u591a3\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\n\ndef worker(name):\n    print(f&quot;Worker {name} waiting&quot;)\n    with semaphore:\n        print(f&quot;Worker {name} acquired semaphore&quot;)\n        time.sleep(2)\n        print(f&quot;Worker {name} released semaphore&quot;)\n\n# \u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\nthreads = []\nfor i in range(10):\n    t = threading.Thread(target=worker, args=(i,))\n    threads.append(t)\n    t.start()\n\n# \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\nfor t in threads:\n    t.join()<\/code><\/pre>\n<h5>3.2.3 \u6761\u4ef6\u53d8\u91cf\uff08Condition\uff09<\/h5>\n<pre><code class=\"python\">import threading\nimport time\n\ncondition = threading.Condition()\nqueue = []\nMAX_ITEMS = 5\n\ndef producer():\n    for i in range(10):\n        with condition:\n            while len(queue) &gt;= MAX_ITEMS:\n                print(&quot;Queue full, producer waiting&quot;)\n                condition.wait()\n            queue.append(i)\n            print(f&quot;Produced: {i}&quot;)\n            condition.notify()\n        time.sleep(0.5)\n\ndef consumer():\n    for _ in range(10):\n        with condition:\n            while not queue:\n                print(&quot;Queue empty, consumer waiting&quot;)\n                condition.wait()\n            item = queue.pop(0)\n            print(f&quot;Consumed: {item}&quot;)\n            condition.notify()\n        time.sleep(1)\n\n# \u521b\u5efa\u7ebf\u7a0b\nproducer_thread = threading.Thread(target=producer)\nconsumer_thread = threading.Thread(target=consumer)\n\n# \u542f\u52a8\u7ebf\u7a0b\nproducer_thread.start()\nconsumer_thread.start()\n\n# \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\nproducer_thread.join()\nconsumer_thread.join()<\/code><\/pre>\n<h4>3.3 \u7ebf\u7a0b\u6c60<\/h4>\n<pre><code class=\"python\">from concurrent.futures import ThreadPoolExecutor\nimport time\n\ndef task(n):\n    print(f&quot;Processing {n}&quot;)\n    time.sleep(1)\n    return n * 2\n\n# \u521b\u5efa\u7ebf\u7a0b\u6c60\nwith ThreadPoolExecutor(max_workers=4) as executor:\n    # \u63d0\u4ea4\u4efb\u52a1\n    futures = [executor.submit(task, i) for i in range(10)]\n    \n    # \u83b7\u53d6\u7ed3\u679c\n    for future in futures:\n        result = future.result()\n        print(f&quot;Result: {result}&quot;)<\/code><\/pre>\n<h3>4. \u591a\u8fdb\u7a0b\u7f16\u7a0b<\/h3>\n<h4>4.1 \u8fdb\u7a0b\u7684\u521b\u5efa\u4e0e\u4f7f\u7528<\/h4>\n<pre><code class=\"python\">import multiprocessing\nimport time\n\ndef worker(name, delay):\n    print(f&quot;Worker {name} started&quot;)\n    time.sleep(delay)\n    print(f&quot;Worker {name} finished&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    # \u521b\u5efa\u8fdb\u7a0b\n    process1 = multiprocessing.Process(target=worker, args=(&quot;A&quot;, 2))\n    process2 = multiprocessing.Process(target=worker, args=(&quot;B&quot;, 3))\n\n    # \u542f\u52a8\u8fdb\u7a0b\n    process1.start()\n    process2.start()\n\n    # \u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\n    process1.join()\n    process2.join()\n\n    print(&quot;All workers finished&quot;)<\/code><\/pre>\n<h4>4.2 \u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h4>\n<h5>4.2.1 \u961f\u5217\uff08Queue\uff09<\/h5>\n<pre><code class=\"python\">import multiprocessing\nimport time\n\ndef producer(queue):\n    for i in range(5):\n        print(f&quot;Produced: {i}&quot;)\n        queue.put(i)\n        time.sleep(0.5)\n\ndef consumer(queue):\n    for _ in range(5):\n        item = queue.get()\n        print(f&quot;Consumed: {item}&quot;)\n        time.sleep(1)\n\nif __name__ == &quot;__main__&quot;:\n    queue = multiprocessing.Queue()\n    \n    # \u521b\u5efa\u8fdb\u7a0b\n    producer_process = multiprocessing.Process(target=producer, args=(queue,))\n    consumer_process = multiprocessing.Process(target=consumer, args=(queue,))\n    \n    # \u542f\u52a8\u8fdb\u7a0b\n    producer_process.start()\n    consumer_process.start()\n    \n    # \u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\n    producer_process.join()\n    consumer_process.join()<\/code><\/pre>\n<h5>4.2.2 \u7ba1\u9053\uff08Pipe\uff09<\/h5>\n<pre><code class=\"python\">import multiprocessing\nimport time\n\ndef sender(conn):\n    for i in range(5):\n        print(f&quot;Sending: {i}&quot;)\n        conn.send(i)\n        time.sleep(0.5)\n    conn.close()\n\ndef receiver(conn):\n    while True:\n        try:\n            item = conn.recv()\n            print(f&quot;Received: {item}&quot;)\n        except EOFError:\n            break\n\nif __name__ == &quot;__main__&quot;:\n    parent_conn, child_conn = multiprocessing.Pipe()\n    \n    # \u521b\u5efa\u8fdb\u7a0b\n    sender_process = multiprocessing.Process(target=sender, args=(child_conn,))\n    receiver_process = multiprocessing.Process(target=receiver, args=(parent_conn,))\n    \n    # \u542f\u52a8\u8fdb\u7a0b\n    sender_process.start()\n    receiver_process.start()\n    \n    # \u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\n    sender_process.join()\n    receiver_process.join()<\/code><\/pre>\n<h5>4.2.3 \u5171\u4eab\u5185\u5b58<\/h5>\n<pre><code class=\"python\">import multiprocessing\nimport time\n\ndef increment(counter, lock):\n    for _ in range(100000):\n        with lock:\n            counter.value += 1\n\nif __name__ == &quot;__main__&quot;:\n    counter = multiprocessing.Value('i', 0)  # \u5171\u4eab\u6574\u6570\n    lock = multiprocessing.Lock()  # \u8fdb\u7a0b\u9501\n    \n    # \u521b\u5efa\u8fdb\u7a0b\n    processes = []\n    for i in range(5):\n        p = multiprocessing.Process(target=increment, args=(counter, lock))\n        processes.append(p)\n        p.start()\n    \n    # \u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\n    for p in processes:\n        p.join()\n    \n    print(f&quot;Final value: {counter.value}&quot;)  # \u5e94\u8f93\u51fa: 500000<\/code><\/pre>\n<h4>4.3 \u8fdb\u7a0b\u6c60<\/h4>\n<pre><code class=\"python\">from concurrent.futures import ProcessPoolExecutor\nimport time\n\ndef task(n):\n    print(f&quot;Processing {n}&quot;)\n    time.sleep(1)\n    return n * 2\n\nif __name__ == &quot;__main__&quot;:\n    # \u521b\u5efa\u8fdb\u7a0b\u6c60\n    with ProcessPoolExecutor(max_workers=4) as executor:\n        # \u63d0\u4ea4\u4efb\u52a1\n        futures = [executor.submit(task, i) for i in range(10)]\n        \n        # \u83b7\u53d6\u7ed3\u679c\n        for future in futures:\n            result = future.result()\n            print(f&quot;Result: {result}&quot;)<\/code><\/pre>\n<h3>5. \u534f\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b<\/h3>\n<h4>5.1 \u534f\u7a0b\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u7531\u7a0b\u5e8f\u63a7\u5236\u8c03\u5ea6\uff0c\u800c\u975e\u64cd\u4f5c\u7cfb\u7edf\u3002Python 3.5+\u4f7f\u7528<code>async\/await<\/code>\u8bed\u6cd5\u652f\u6301\u534f\u7a0b\u3002<\/p>\n<h4>5.2 \u534f\u7a0b\u7684\u5b9e\u73b0<\/h4>\n<pre><code class=\"python\">import asyncio\n\nasync def say_hello(name):\n    print(f&quot;Hello, {name}!&quot;)\n    await asyncio.sleep(1)  # \u6a21\u62dfI\/O\u64cd\u4f5c\n    print(f&quot;Goodbye, {name}!&quot;)\n\nasync def main():\n    # \u5e76\u884c\u6267\u884c\u591a\u4e2a\u534f\u7a0b\n    await asyncio.gather(\n        say_hello(&quot;Alice&quot;),\n        say_hello(&quot;Bob&quot;),\n        say_hello(&quot;Charlie&quot;)\n    )\n\n# \u8fd0\u884c\u4e3b\u534f\u7a0b\nasyncio.run(main())<\/code><\/pre>\n<h4>5.3 \u5f02\u6b65I\/O<\/h4>\n<pre><code class=\"python\">import asyncio\nimport aiohttp\n\nasync def fetch_url(session, url):\n    async with session.get(url) as response:\n        return await response.text()\n\nasync def main():\n    async with aiohttp.ClientSession() as session:\n        html = await fetch_url(session, &quot;https:\/\/example.com&quot;)\n        print(html[:100])\n\n# \u8fd0\u884c\u4e3b\u534f\u7a0b\nasyncio.run(main())<\/code><\/pre>\n<h4>5.4 \u4e8b\u4ef6\u5faa\u73af<\/h4>\n<p>\u4e8b\u4ef6\u5faa\u73af\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u6838\u5fc3\uff0c\u8d1f\u8d23\u8c03\u5ea6\u534f\u7a0b\u7684\u6267\u884c\uff1a<\/p>\n<ol>\n<li><strong>\u6ce8\u518c\u534f\u7a0b<\/strong>\uff1a\u5c06\u534f\u7a0b\u6ce8\u518c\u5230\u4e8b\u4ef6\u5faa\u73af<\/li>\n<li><strong>\u6267\u884c\u534f\u7a0b<\/strong>\uff1a\u4e8b\u4ef6\u5faa\u73af\u6267\u884c\u534f\u7a0b\u76f4\u5230\u9047\u5230<code>await<\/code><\/li>\n<li><strong>\u6682\u505c\u534f\u7a0b<\/strong>\uff1a\u534f\u7a0b\u5728<code>await<\/code>\u5904\u6682\u505c\uff0c\u63a7\u5236\u6743\u8fd4\u56de\u4e8b\u4ef6\u5faa\u73af<\/li>\n<li><strong>\u8c03\u5ea6\u5176\u4ed6\u534f\u7a0b<\/strong>\uff1a\u4e8b\u4ef6\u5faa\u73af\u6267\u884c\u5176\u4ed6\u5c31\u7eea\u7684\u534f\u7a0b<\/li>\n<li><strong>\u6062\u590d\u534f\u7a0b<\/strong>\uff1a\u5f53<code>await<\/code>\u7684\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u534f\u7a0b\u88ab\u6062\u590d\u6267\u884c<\/li>\n<\/ol>\n<h3>6. GIL\u7684\u4f18\u5316\u7b56\u7565<\/h3>\n<h4>6.1 \u9488\u5bf9CPU\u5bc6\u96c6\u578b\u4efb\u52a1<\/h4>\n<ol>\n<li><strong>\u4f7f\u7528\u591a\u8fdb\u7a0b<\/strong>\uff1a\u7ed5\u8fc7GIL\uff0c\u5229\u7528\u591a\u6838CPU<\/li>\n<li><strong>\u4f7f\u7528C\u6269\u5c55<\/strong>\uff1a\u5728C\u6269\u5c55\u4e2d\u91ca\u653eGIL<\/li>\n<li><strong>\u4f7f\u7528PyPy<\/strong>\uff1aPyPy\u7684GIL\u5b9e\u73b0\u66f4\u9ad8\u6548\uff0c\u751a\u81f3\u6709GIL-free\u7248\u672c<\/li>\n<li><strong>\u4f7f\u7528Numba<\/strong>\uff1aNumba\u53ef\u4ee5\u7f16\u8bd1Python\u4ee3\u7801\u4e3a\u673a\u5668\u7801\uff0c\u7ed5\u8fc7GIL<\/li>\n<\/ol>\n<h4>6.2 \u9488\u5bf9I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/h4>\n<ol>\n<li><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b<\/strong>\uff1a\u7ebf\u7a0b\u5728I\/O\u64cd\u4f5c\u65f6\u91ca\u653eGIL<\/li>\n<li><strong>\u4f7f\u7528\u534f\u7a0b<\/strong>\uff1a\u534f\u7a0b\u662fI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u6700\u4f73\u9009\u62e9<\/li>\n<li><strong>\u4f7f\u7528\u5f02\u6b65I\/O<\/strong>\uff1a<code>asyncio<\/code>\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5f02\u6b65I\/O\u64cd\u4f5c<\/li>\n<\/ol>\n<h4>6.3 \u4ee3\u7801\u4f18\u5316\u6280\u5de7<\/h4>\n<ol>\n<li>\n<p><strong>\u51cf\u5c11GIL\u7ade\u4e89<\/strong>\uff1a<\/p>\n<ul>\n<li>\u51cf\u5c11\u9501\u7684\u6301\u6709\u65f6\u95f4<\/li>\n<li>\u907f\u514d\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u5faa\u73af<\/li>\n<li>\u4f7f\u7528<code>time.sleep(0)<\/code>\u4e3b\u52a8\u8ba9\u51faGIL<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>queue.Queue<\/code>\u8fdb\u884c\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u64cd\u4f5c<\/li>\n<li>\u4f7f\u7528<code>collections.deque<\/code>\u8fdb\u884c\u9ad8\u6548\u7684\u53cc\u7aef\u961f\u5217\u64cd\u4f5c<\/li>\n<li>\u4f7f\u7528<code>threading.local()<\/code>\u5b58\u50a8\u7ebf\u7a0b\u672c\u5730\u6570\u636e<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u4f20\u9012\u6570\u636e<\/li>\n<li>\u4f7f\u7528\u7c7b\u5b9e\u4f8b\u53d8\u91cf\u5b58\u50a8\u72b6\u6001<\/li>\n<li>\u4f7f\u7528\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>7. \u5e76\u53d1\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<h4>7.1 \u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u4efb\u52a1\u7c7b\u578b<\/th>\n<th>\u63a8\u8350\u6a21\u578b<\/th>\n<th>\u539f\u56e0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>CPU\u5bc6\u96c6\u578b<\/td>\n<td>\u591a\u8fdb\u7a0b<\/td>\n<td>\u7ed5\u8fc7GIL\uff0c\u5229\u7528\u591a\u6838<\/td>\n<\/tr>\n<tr>\n<td>I\/O\u5bc6\u96c6\u578b<\/td>\n<td>\u534f\u7a0b\/\u591a\u7ebf\u7a0b<\/td>\n<td>\u534f\u7a0b\u66f4\u8f7b\u91cf\uff0c\u591a\u7ebf\u7a0b\u66f4\u7b80\u5355<\/td>\n<\/tr>\n<tr>\n<td>\u6df7\u5408\u4efb\u52a1<\/td>\n<td>\u591a\u8fdb\u7a0b+\u534f\u7a0b<\/td>\n<td>\u8fdb\u7a0b\u5904\u7406CPU\u5bc6\u96c6\u578b\uff0c\u534f\u7a0b\u5904\u7406I\/O<\/td>\n<\/tr>\n<tr>\n<td>\u9ad8\u5e76\u53d1I\/O<\/td>\n<td>\u5f02\u6b65I\/O<\/td>\n<td>\u5355\u7ebf\u7a0b\u5904\u7406\u6570\u5343\u4e2a\u8fde\u63a5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>7.2 \u7ebf\u7a0b\u5b89\u5168\u7f16\u7a0b<\/h4>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff1a<\/p>\n<ul>\n<li><code>queue.Queue<\/code>\uff1a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217<\/li>\n<li><code>collections.deque<\/code>\uff1a\u7ebf\u7a0b\u5b89\u5168\u7684\u53cc\u7aef\u961f\u5217<\/li>\n<li><code>threading.local()<\/code>\uff1a\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6b63\u786e\u4f7f\u7528\u9501<\/strong>\uff1a<\/p>\n<ul>\n<li>\u53ea\u5728\u5fc5\u8981\u65f6\u4f7f\u7528\u9501<\/li>\n<li>\u51cf\u5c11\u9501\u7684\u4f5c\u7528\u8303\u56f4<\/li>\n<li>\u907f\u514d\u5d4c\u5957\u9501<\/li>\n<li>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u7ba1\u7406\u9501<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u7ade\u6001\u6761\u4ef6<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c<\/li>\n<li>\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u8ba1\u6570\u5668<\/li>\n<li>\u4f7f\u7528<code>threading.RLock<\/code>\u907f\u514d\u6b7b\u9501<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h4>7.3 \u6027\u80fd\u4f18\u5316<\/h4>\n<ol>\n<li>\n<p><strong>\u51cf\u5c11\u7ebf\u7a0b\/\u8fdb\u7a0b\u6570\u91cf<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7ebf\u7a0b\u6c60\u5927\u5c0f\uff1aI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u53ef\u8bbe\u7f6e\u8f83\u5927<\/li>\n<li>\u8fdb\u7a0b\u6c60\u5927\u5c0f\uff1a\u901a\u5e38\u8bbe\u7f6e\u4e3aCPU\u6838\u5fc3\u6570<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u9ad8\u5e76\u53d1I\/O\u4efb\u52a1\uff0c\u5f02\u6b65\u7f16\u7a0b\u6bd4\u591a\u7ebf\u7a0b\u66f4\u9ad8\u6548<\/li>\n<li>\u51cf\u5c11\u7ebf\u7a0b\u521b\u5efa\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u5f00\u9500<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u76d1\u63a7\u548c\u8c03\u4f18<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>psutil<\/code>\u76d1\u63a7\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u72b6\u6001<\/li>\n<li>\u4f7f\u7528<code>cProfile<\/code>\u5206\u6790\u6027\u80fd\u74f6\u9888<\/li>\n<li>\u4f7f\u7528<code>tracemalloc<\/code>\u8ddf\u8e2a\u5185\u5b58\u4f7f\u7528<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>8. \u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h3>\n<h4>8.1 \u591a\u7ebf\u7a0b\u722c\u866b<\/h4>\n<pre><code class=\"python\">import threading\nimport queue\nimport requests\nfrom bs4 import BeautifulSoup\n\nclass Spider:\n    def __init__(self, url, max_threads=4):\n        self.url = url\n        self.max_threads = max_threads\n        self.queue = queue.Queue()\n        self.visited = set()\n        self.lock = threading.Lock()\n    \n    def crawl(self):\n        self.queue.put(self.url)\n        \n        # \u521b\u5efa\u7ebf\u7a0b\u6c60\n        threads = []\n        for _ in range(self.max_threads):\n            t = threading.Thread(target=self._worker)\n            t.start()\n            threads.append(t)\n        \n        # \u7b49\u5f85\u961f\u5217\u6e05\u7a7a\n        self.queue.join()\n        \n        # \u505c\u6b62\u6240\u6709\u7ebf\u7a0b\n        for _ in range(self.max_threads):\n            self.queue.put(None)\n        for t in threads:\n            t.join()\n    \n    def _worker(self):\n        while True:\n            url = self.queue.get()\n            if url is None:\n                self.queue.task_done()\n                break\n            \n            if url in self.visited:\n                self.queue.task_done()\n                continue\n            \n            try:\n                response = requests.get(url, timeout=5)\n                soup = BeautifulSoup(response.text, 'html.parser')\n                \n                # \u63d0\u53d6\u94fe\u63a5\n                links = []\n                for a in soup.find_all('a', href=True):\n                    link = a['href']\n                    if link.startswith('http'):\n                        links.append(link)\n                \n                # \u6dfb\u52a0\u65b0\u94fe\u63a5\u5230\u961f\u5217\n                with self.lock:\n                    self.visited.add(url)\n                    for link in links:\n                        if link not in self.visited:\n                            self.queue.put(link)\n                \n                print(f&quot;Crawled: {url}, Found {len(links)} links&quot;)\n            except Exception as e:\n                print(f&quot;Error crawling {url}: {e}&quot;)\n            finally:\n                self.queue.task_done()\n\n# \u4f7f\u7528\u722c\u866b\nspider = Spider('https:\/\/example.com', max_threads=4)\nspider.crawl()<\/code><\/pre>\n<h4>8.2 \u591a\u8fdb\u7a0b\u6570\u636e\u5904\u7406<\/h4>\n<pre><code class=\"python\">import multiprocessing\nimport numpy as np\n\ndef process_chunk(chunk):\n    # \u5904\u7406\u6570\u636e\u5757\n    result = np.sum(chunk)\n    return result\n\ndef main():\n    # \u751f\u6210\u5927\u91cf\u6570\u636e\n    data = np.random.rand(10000000)  # \u7ea680MB\u6570\u636e\n    \n    # \u5206\u5272\u6570\u636e\n    chunks = np.array_split(data, multiprocessing.cpu_count())\n    \n    # \u4f7f\u7528\u8fdb\u7a0b\u6c60\u5904\u7406\u6570\u636e\n    with multiprocessing.Pool() as pool:\n        results = pool.map(process_chunk, chunks)\n    \n    # \u6c47\u603b\u7ed3\u679c\n    total = sum(results)\n    print(f&quot;Total sum: {total}&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    main()<\/code><\/pre>\n<h4>8.3 \u5f02\u6b65Web\u670d\u52a1\u5668<\/h4>\n<pre><code class=\"python\">import asyncio\nfrom aiohttp import web\n\nasync def handle(request):\n    # \u6a21\u62dfI\/O\u64cd\u4f5c\n    await asyncio.sleep(0.1)\n    return web.Response(text=&quot;Hello, World!&quot;)\n\nasync def main():\n    app = web.Application()\n    app.add_routes([web.get('\/', handle)])\n    runner = web.AppRunner(app)\n    await runner.setup()\n    site = web.TCPSite(runner, 'localhost', 8080)\n    await site.start()\n    print(&quot;Server started at http:\/\/localhost:8080&quot;)\n    # \u4fdd\u6301\u670d\u52a1\u5668\u8fd0\u884c\n    await asyncio.Event().wait()\n\n# \u8fd0\u884c\u670d\u52a1\u5668\nasyncio.run(main())<\/code><\/pre>\n<h3>9. \u5e76\u53d1\u7f16\u7a0b\u7684\u5de5\u5177\u4e0e\u5e93<\/h3>\n<h4>9.1 \u6807\u51c6\u5e93<\/h4>\n<ul>\n<li><strong>threading<\/strong>\uff1a\u591a\u7ebf\u7a0b\u7f16\u7a0b<\/li>\n<li><strong>multiprocessing<\/strong>\uff1a\u591a\u8fdb\u7a0b\u7f16\u7a0b<\/li>\n<li><strong>concurrent.futures<\/strong>\uff1a\u7ebf\u7a0b\u6c60\u548c\u8fdb\u7a0b\u6c60<\/li>\n<li><strong>asyncio<\/strong>\uff1a\u5f02\u6b65I\/O\u548c\u534f\u7a0b<\/li>\n<li><strong>queue<\/strong>\uff1a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217<\/li>\n<\/ul>\n<h4>9.2 \u7b2c\u4e09\u65b9\u5e93<\/h4>\n<ul>\n<li><strong>aiohttp<\/strong>\uff1a\u5f02\u6b65HTTP\u5ba2\u6237\u7aef\/\u670d\u52a1\u5668<\/li>\n<li><strong>asyncpg<\/strong>\uff1a\u5f02\u6b65PostgreSQL\u5ba2\u6237\u7aef<\/li>\n<li><strong>uvloop<\/strong>\uff1a\u66f4\u5feb\u7684\u4e8b\u4ef6\u5faa\u73af\u5b9e\u73b0<\/li>\n<li><strong>gunicorn<\/strong>\uff1aWSGI HTTP\u670d\u52a1\u5668\uff0c\u652f\u6301\u591a\u8fdb\u7a0b<\/li>\n<li><strong>gevent<\/strong>\uff1a\u57fa\u4e8e\u534f\u7a0b\u7684\u5e76\u53d1\u5e93<\/li>\n<\/ul>\n<h4>9.3 \u6027\u80fd\u5206\u6790\u5de5\u5177<\/h4>\n<ul>\n<li><strong>cProfile<\/strong>\uff1aPython\u7684\u6807\u51c6\u6027\u80fd\u5206\u6790\u5668<\/li>\n<li><strong>line_profiler<\/strong>\uff1a\u9010\u884c\u6027\u80fd\u5206\u6790<\/li>\n<li><strong>memory_profiler<\/strong>\uff1a\u5185\u5b58\u4f7f\u7528\u5206\u6790<\/li>\n<li><strong>psutil<\/strong>\uff1a\u7cfb\u7edf\u8d44\u6e90\u76d1\u63a7<\/li>\n<li><strong>py-spy<\/strong>\uff1a\u91c7\u6837\u5206\u6790\u5668\uff0c\u4f4e\u5f00\u9500<\/li>\n<\/ul>\n<h3>10. \u603b\u7ed3<\/h3>\n<p>Python\u7684\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u4e2a\u590d\u6742\u4f46\u5f3a\u5927\u7684\u9886\u57df\uff0cGIL\u7684\u5b58\u5728\u867d\u7136\u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u7684\u6027\u80fd\uff0c\u4f46\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b\u548c\u4f18\u5316\u7b56\u7565\uff0c\u53ef\u4ee5\u5145\u5206\u53d1\u6325Python\u7684\u5e76\u53d1\u80fd\u529b\u3002<\/p>\n<h4>\u5173\u952e\u8981\u70b9<\/h4>\n<ol>\n<li>\n<p><strong>GIL\u7684\u5f71\u54cd<\/strong>\uff1a<\/p>\n<ul>\n<li>CPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff1a\u591a\u7ebf\u7a0b\u65e0\u6cd5\u5229\u7528\u591a\u6838\uff0c\u63a8\u8350\u4f7f\u7528\u591a\u8fdb\u7a0b<\/li>\n<li>I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff1a\u591a\u7ebf\u7a0b\u548c\u534f\u7a0b\u90fd\u53ef\u4ee5\u9ad8\u6548\u5904\u7406<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5e76\u53d1\u6a21\u578b\u9009\u62e9<\/strong>\uff1a<\/p>\n<ul>\n<li>\u591a\u7ebf\u7a0b\uff1a\u9002\u5408I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u7b80\u5355\u6613\u7528<\/li>\n<li>\u591a\u8fdb\u7a0b\uff1a\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u7ed5\u8fc7GIL<\/li>\n<li>\u534f\u7a0b\uff1a\u9002\u5408\u9ad8\u5e76\u53d1I\/O\u4efb\u52a1\uff0c\u8f7b\u91cf\u9ad8\u6548<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u4efb\u52a1\u7c7b\u578b\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/li>\n<li>\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\u548c\u540c\u6b65\u539f\u8bed<\/li>\n<li>\u907f\u514d\u5168\u5c40\u53d8\u91cf\u548c\u7ade\u6001\u6761\u4ef6<\/li>\n<li>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u548c\u8fdb\u7a0b\u6c60\u7ba1\u7406\u5e76\u53d1\u4efb\u52a1<\/li>\n<li>\u76d1\u63a7\u548c\u8c03\u4f18\u5e76\u53d1\u6027\u80fd<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>GIL\u7684\u4f18\u5316<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u7528\u591a\u8fdb\u7a0b\u6216C\u6269\u5c55<\/li>\n<li>\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u534f\u7a0b<\/li>\n<li>\u51cf\u5c11GIL\u7ade\u4e89\uff0c\u4f18\u5316\u4ee3\u7801\u7ed3\u6784<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u638c\u63e1Python\u7684\u5e76\u53d1\u7f16\u7a0b\u6280\u672f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7f16\u5199\u66f4\u9ad8\u6548\u3001\u66f4\u54cd\u5e94\u8fc5\u901f\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u7279\u522b\u662f\u5728\u5904\u7406I\/O\u64cd\u4f5c\u3001\u7f51\u7edc\u8bf7\u6c42\u548c\u6570\u636e\u5904\u7406\u7b49\u573a\u666f\u4e2d\u3002\u867d\u7136GIL\u5e26\u6765\u4e86\u4e00\u4e9b\u9650\u5236\uff0c\u4f46\u901a\u8fc7\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0cPython\u4f9d\u7136\u662f\u4e00\u95e8\u5f3a\u5927\u7684\u5e76\u53d1\u7f16\u7a0b\u8bed\u8a00\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047585282\">https:\/\/segmentfault.com\/a\/1190000047585282<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u4e0eGIL\u673a\u5236\u4f18\u5316\u7b56\u7565 1. \u5e76\u53d1\u7f16\u7a0b\u6982\u8ff0 1.1 \u5e76\u53d1\u4e0e\u5e76\u884c\u7684\u533a\u522b \u5e76\u53d1\uff08Concur [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021223","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021223"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021223\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}