{"id":1399,"date":"2024-10-10T22:54:07","date_gmt":"2024-10-10T14:54:07","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1399"},"modified":"2024-10-13T16:27:15","modified_gmt":"2024-10-13T08:27:15","slug":"python-%e7%9a%84%e5%b9%b6%e5%8f%91%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1399","title":{"rendered":"python \u7684\u5e76\u53d1\u6a21\u578b"},"content":{"rendered":"<blockquote>\n<p>\u5e76\u53d1\u6307\u540c\u65f6\u5904\u7406\u591a\u4ef6\u4e8b<br \/>\n\u5e76\u884c\u6307\u540c\u65f6\u505a\u591a\u4ef6\u4e8b<br \/>\n\u4e8c\u8005\u4e0d\u540c, \u4f46\u6709\u8054\u7cfb<br \/>\n\u4e00\u4e2a\u5173\u4e8e\u7ed3\u6784, \u4e00\u4e2a\u5173\u4e8e\u6267\u884c<br \/>\n\u5e76\u53d1\u7528\u4e8e\u5236\u5b9a\u65b9\u6848, \u7528\u6765\u89e3\u51b3\u53ef\u80fd\u4f46\u672a\u5fc5\u5e76\u884c\u7684\u95ee\u9898<\/p>\n<ul>\n<li>Rob Pike<br \/>\nGo \u8bed\u8a00\u521b\u9020\u8005\u4e4b\u4e00<\/li>\n<\/ul>\n<\/blockquote>\n<p>python \u89e3\u91ca\u5668\u4ec5\u4f7f\u7528\u4e00\u4e2a\u7ebf\u7a0b\u8fd0\u884c\u7528\u6237\u7684\u7a0b\u5e8f\u548c\u5185\u5b58\u5783\u573e\u56de\u6536\u7a0b\u5e8f, \u4f7f\u7528 threading \u6216 current.future \u5e93\u53ef\u4ee5\u542f\u52a8\u989d\u5916\u7684 python \u7ebf\u7a0b, \u4f46\u662f python \u6709\u4e00\u4e2a GIL(Global Interpreter Lock ) \u5168\u5c40\u89e3\u91ca\u5668\u9501, \u4efb\u610f\u65f6\u95f4\u70b9\u4e0a\u53ea\u80fd\u6709\u4e00\u4e2a python \u7ebf\u7a0b\u53ef\u4ee5\u6301\u6709  GIL , \u8fd9\u4e2a\u9501\u63a7\u5236\u5bf9\u8c61\u5f15\u7528\u8ba1\u6570\u548c\u89e3\u91ca\u5668\u7684\u5185\u90e8\u72b6\u6001, \u4efb\u610f\u65f6\u95f4\u70b9\u4e0a\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u624d\u80fd\u6267\u884c python \u4ee3\u7801, \u54ea\u6015\u4f60\u6709\u591a\u4e2a CPU \u6216\u8005 CPU \u6709\u591a\u4e2a\u6838.<\/p>\n<p>\u4e3a\u9632\u6b62\u4e00\u4e2a Python \u7ebf\u7a0b\u65e0\u9650\u671f\u6301\u6709 GIL, pyhon \u89e3\u91ca\u5668\u6bcf 5 \u6beb\u79d2\u6682\u505c\u5f53\u524d\u7ebf\u7a0b, \u91ca\u653e GIL , \u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\u7a0b\u5e8f\u6765\u6311\u9009\u4e00\u4e2a\u7b49\u5f85\u7684\u7ebf\u7a0b<\/p>\n<p>Python \u6807\u51c6\u5e93\u4e2d\u53d1\u8d77\u7cfb\u7edf\u8c03\u7528\u7684\u51fd\u6570\u90fd\u53ef\u4ee5\u91ca\u653e GIL, \u4f8b\u5982\u6267\u884c\u78c1\u76d8 IO, \u7f51\u7edc IO, \u4ee5\u53ca time.sleep(), \u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u4ee5 C \u8bed\u8a00\u6765\u5b9e\u73b0 python \u6269\u5c55\u6765\u91ca\u653e  GIL<\/p>\n<p>\u6240\u4ee5 IO \u5bc6\u96c6\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u591a\u7528 python \u7ebf\u7a0b\u6216\u8005\u534f\u7a0b, \u800c\u8ba1\u7b97\u5bc6\u96c6\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\u6700\u597d\u4f7f\u7528\u591a\u8fdb\u7a0b. \u800c\u534f\u7a0b\u662f\u6307\u53ef\u4ee5\u6682\u65f6\u6267\u884c\u6302\u8d77\u81ea\u8eab\u5e76\u5728\u4ee5\u540e\u518d\u6062\u590d\u6267\u884c\u7684\u51fd\u6570.<\/p>\n<h2>\u4ec0\u4e48\u662f\u5e76\u53d1\u4e0e\u5e76\u884c\uff1f<\/h2>\n<p>\u5e76\u53d1\u4e0e\u5e76\u884c\u662f\u9ad8\u6027\u80fd\u7f16\u7a0b\u4e2d\u7684\u4e24\u4e2a\u5e38\u7528\u672f\u8bed\u3002\u5c3d\u7ba1\u5b83\u4eec\u5e38\u5e38\u88ab\u6df7\u6dc6\uff0c\u4f46\u6709\u7ec6\u5fae\u5dee\u522b\u3002<\/p>\n<ul>\n<li>\n<p>\u5e76\u53d1\uff1a\u7a0b\u5e8f\u901a\u8fc7\u5207\u6362\u4efb\u52a1\u6765\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u800c\u8fd9\u4e9b\u4efb\u52a1\u53ef\u80fd\u5e76\u4e0d\u540c\u65f6\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u4e00\u8fb9\u4e0b\u8f7d\u6587\u4ef6\u4e00\u8fb9\u5904\u7406\u5176\u4ed6\u6570\u636e\uff0c\u4f46\u8fd9\u4e9b\u4efb\u52a1\u662f\u4ea4\u66ff\u6267\u884c\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e76\u884c\uff1a\u591a\u4e2a\u4efb\u52a1\u540c\u65f6\u6267\u884c\uff0c\u901a\u5e38\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u8fd0\u884c\u5728\u4e0d\u540c\u7684\u6838\u5fc3\u4e0a\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>Python \u5bf9\u5e76\u53d1\u7684\u652f\u6301<\/h2>\n<ol>\n<li>threading \u6a21\u5757\uff1a\u4f7f\u7528\u7ebf\u7a0b\u5b9e\u73b0\u5e76\u53d1\uff0c\u9002\u7528\u4e8e I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/li>\n<li>asyncio \u6a21\u5757\uff1a\u63d0\u4f9b\u534f\u7a0b\u652f\u6301\uff0c\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff0c\u9002\u5408\u5927\u91cf I\/O \u64cd\u4f5c\u3002<\/li>\n<li>multiprocessing \u6a21\u5757\uff1a\u7528\u4e8e\u591a\u8fdb\u7a0b\uff0c\u9002\u7528\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/li>\n<\/ol>\n<h3>\u793a\u4f8b 1\uff1a\u4f7f\u7528 threading \u6a21\u5757<\/h3>\n<p>threading \u6a21\u5757\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u4efb\u52a1\uff0c\u7ebf\u7a0b\u9002\u7528\u4e8e I\/O \u5bc6\u96c6\u578b\u64cd\u4f5c\uff0c\u5982\u6587\u4ef6\u8bfb\u5199\u6216\u7f51\u7edc\u8bf7\u6c42\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import threading\nimport time\n\ndef task(name):\n    print(f&quot;\u7ebf\u7a0b {name} \u5f00\u59cb&quot;)\n    time.sleep(2)  # \u6a21\u62df I\/O \u64cd\u4f5c\n    print(f&quot;\u7ebf\u7a0b {name} \u7ed3\u675f&quot;)\n\n# \u521b\u5efa\u7ebf\u7a0b\nthreads = []\nfor i in range(5):\n    t = threading.Thread(target=task, 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()\n\nprint(&quot;\u6240\u6709\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5&quot;)<\/code><\/pre>\n<p>\u89e3\u91ca\uff1a<\/p>\n<p>\u2022 \u6211\u4eec\u521b\u5efa\u4e86 5 \u4e2a\u7ebf\u7a0b\uff0c\u5e76\u8ba9\u5b83\u4eec\u5e76\u53d1\u6267\u884c\u4efb\u52a1 task\u3002<br \/>\n\u2022 time.sleep(2) \u6a21\u62df\u4e00\u4e2a\u8017\u65f6\u7684 I\/O \u64cd\u4f5c\u3002<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u7ebf\u7a0b 0 \u5f00\u59cb\n\u7ebf\u7a0b 1 \u5f00\u59cb\n\u7ebf\u7a0b 2 \u5f00\u59cb\n\u7ebf\u7a0b 3 \u5f00\u59cb\n\u7ebf\u7a0b 4 \u5f00\u59cb\n\u7ebf\u7a0b 0 \u7ed3\u675f\n\u7ebf\u7a0b 1 \u7ed3\u675f\n\u7ebf\u7a0b 2 \u7ed3\u675f\n\u7ebf\u7a0b 3 \u7ed3\u675f\n\u7ebf\u7a0b 4 \u7ed3\u675f\n\u6240\u6709\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5<\/code><\/pre>\n<h3>\u793a\u4f8b 2\uff1a\u4f7f\u7528 asyncio \u6a21\u5757<\/h3>\n<p>asyncio \u6a21\u5757\u63d0\u4f9b\u4e86\u534f\u7a0b\u652f\u6301\uff0c\u5141\u8bb8\u6211\u4eec\u4ee5\u5f02\u6b65\u65b9\u5f0f\u6267\u884c\u4efb\u52a1\uff0c\u9002\u7528\u4e8e I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u3002\u5b83\u4e0e threading \u7684\u533a\u522b\u5728\u4e8e\uff0c\u5b83\u5e76\u4e0d\u4f9d\u8d56\u591a\u7ebf\u7a0b\uff0c\u800c\u662f\u4f9d\u8d56\u4e8e\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import asyncio\n\nasync def async_task(name):\n    print(f&quot;\u4efb\u52a1 {name} \u5f00\u59cb&quot;)\n    await asyncio.sleep(2)  # \u6a21\u62df\u5f02\u6b65 I\/O \u64cd\u4f5c\n    print(f&quot;\u4efb\u52a1 {name} \u7ed3\u675f&quot;)\n\nasync def main():\n    tasks = [async_task(i) for i in range(5)]\n    await asyncio.gather(*tasks)\n\n# \u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af\nawait main()<\/code><\/pre>\n<p>\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li>\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1 async_task\uff0c\u5e76\u4f7f\u7528 asyncio.gather \u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u4efb\u52a1\u3002<\/li>\n<li>await asyncio.sleep(2) \u6a21\u62df\u4e86\u4e00\u4e2a\u5f02\u6b65\u7684 I\/O \u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u4efb\u52a1 0 \u5f00\u59cb\n\u4efb\u52a1 1 \u5f00\u59cb\n\u4efb\u52a1 2 \u5f00\u59cb\n\u4efb\u52a1 3 \u5f00\u59cb\n\u4efb\u52a1 4 \u5f00\u59cb\n\u4efb\u52a1 0 \u7ed3\u675f\n\u4efb\u52a1 1 \u7ed3\u675f\n\u4efb\u52a1 2 \u7ed3\u675f\n\u4efb\u52a1 3 \u7ed3\u675f\n\u4efb\u52a1 4 \u7ed3\u675f<\/code><\/pre>\n<h3>\u793a\u4f8b 3\uff1a\u4f7f\u7528 multiprocessing \u6a21\u5757<\/h3>\n<p>multiprocessing \u6a21\u5757\u5141\u8bb8\u6211\u4eec\u5728\u591a\u4e2a\u8fdb\u7a0b\u4e0a\u6267\u884c\u4efb\u52a1\uff0c\u9002\u7528\u4e8e CPU \u5bc6\u96c6\u578b\u64cd\u4f5c\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import multiprocessing\nimport time\n\ndef cpu_task(name):\n    print(f&quot;\u8fdb\u7a0b {name} \u5f00\u59cb&quot;)\n    time.sleep(2)  # \u6a21\u62df CPU \u5bc6\u96c6\u578b\u64cd\u4f5c\n    print(f&quot;\u8fdb\u7a0b {name} \u7ed3\u675f&quot;)\n\n# \u521b\u5efa\u8fdb\u7a0b\nprocesses = []\nfor i in range(5):\n    p = multiprocessing.Process(target=cpu_task, args=(i,))\n    processes.append(p)\n    p.start()\n\n# \u7b49\u5f85\u6240\u6709\u8fdb\u7a0b\u5b8c\u6210\nfor p in processes:\n    p.join()\n\nprint(&quot;\u6240\u6709\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5&quot;)<\/code><\/pre>\n<p>\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528 multiprocessing.Process \u6765\u521b\u5efa 5 \u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u6267\u884c cpu_task\u3002<\/li>\n<li>\u8fdb\u7a0b\u95f4\u4e0d\u4f1a\u5171\u4eab\u5185\u5b58\uff0c\u9002\u5408 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u8fdb\u7a0b 0 \u5f00\u59cb\n\u8fdb\u7a0b 1 \u5f00\u59cb\n\u8fdb\u7a0b 2 \u5f00\u59cb\n\u8fdb\u7a0b 3 \u5f00\u59cb\n\u8fdb\u7a0b 4 \u5f00\u59cb\n\u8fdb\u7a0b 0 \u7ed3\u675f\n\u8fdb\u7a0b 1 \u7ed3\u675f\n\u8fdb\u7a0b 2 \u7ed3\u675f\n\u8fdb\u7a0b 3 \u7ed3\u675f\n\u8fdb\u7a0b 4 \u7ed3\u675f\n\u6240\u6709\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5<\/code><\/pre>\n<h3>\u7ed3\u8bba<\/h3>\n<ul>\n<li>threading \u9002\u7528\u4e8e I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002<\/li>\n<li>asyncio \u9002\u5408\u5927\u91cf I\/O \u4efb\u52a1\uff0c\u5e76\u4e14\u5b83\u4f7f\u7528\u534f\u7a0b\u4ee3\u66ff\u7ebf\u7a0b\uff0c\u51cf\u5c11\u4e86\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<\/li>\n<li>multiprocessing \u66f4\u9002\u5408 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u5229\u7528\u4e86\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684 CPU \u6838\u5fc3\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5e76\u53d1\u6307\u540c\u65f6\u5904\u7406\u591a\u4ef6\u4e8b \u5e76\u884c\u6307\u540c\u65f6\u505a\u591a\u4ef6\u4e8b \u4e8c\u8005\u4e0d\u540c, \u4f46\u6709\u8054\u7cfb \u4e00\u4e2a\u5173\u4e8e\u7ed3\u6784, \u4e00\u4e2a\u5173\u4e8e\u6267\u884c \u5e76\u53d1\u7528\u4e8e\u5236\u5b9a\u65b9\u6848, \u7528\u6765\u89e3\u51b3\u53ef\u80fd\u4f46\u672a\u5fc5\u5e76\u884c\u7684\u95ee\u9898 Rob Pike Go \u8bed\u8a00\u521b\u9020\u8005\u4e4b\u4e00 python \u89e3\u91ca\u5668\u4ec5\u4f7f\u7528\u4e00\u4e2a\u7ebf\u7a0b\u8fd0\u884c\u7528\u6237\u7684\u7a0b\u5e8f\u548c\u5185\u5b58\u5783\u573e\u56de\u6536\u7a0b\u5e8f, \u4f7f\u7528 threading \u6216 current.future \u5e93\u53ef\u4ee5\u542f\u52a8\u989d\u5916\u7684 python \u7ebf\u7a0b, \u4f46\u662f python \u6709\u4e00\u4e2a GIL(Global Interpreter Lock ) \u5168\u5c40\u89e3\u91ca\u5668\u9501, \u4efb\u610f\u65f6\u95f4\u70b9\u4e0a\u53ea\u80fd\u6709\u4e00\u4e2a python \u7ebf\u7a0b\u53ef\u4ee5\u6301\u6709 GIL , \u8fd9\u4e2a\u9501\u63a7\u5236\u5bf9\u8c61\u5f15\u7528\u8ba1\u6570\u548c\u89e3\u91ca\u5668\u7684\u5185\u90e8\u72b6\u6001, \u4efb\u610f\u65f6\u95f4\u70b9\u4e0a\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u624d\u80fd\u6267\u884c python \u4ee3\u7801, \u54ea\u6015\u4f60\u6709\u591a\u4e2a CPU \u6216\u8005 CPU \u6709\u591a\u4e2a\u6838. \u4e3a\u9632\u6b62\u4e00\u4e2a Python \u7ebf\u7a0b\u65e0\u9650\u671f\u6301\u6709 GIL, pyhon \u89e3\u91ca\u5668\u6bcf 5 \u6beb\u79d2\u6682\u505c\u5f53\u524d\u7ebf\u7a0b, \u91ca\u653e GIL , \u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\u7a0b\u5e8f\u6765\u6311\u9009\u4e00\u4e2a\u7b49\u5f85\u7684\u7ebf\u7a0b [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1399\" title=\"Permanent Link to: python \u7684\u5e76\u53d1\u6a21\u578b\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1399","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1399"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1399"}],"version-history":[{"count":5,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1399\/revisions"}],"predecessor-version":[{"id":1409,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1399\/revisions\/1409"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}