{"id":4987,"date":"2021-01-25T14:35:06","date_gmt":"2021-01-25T06:35:06","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4987"},"modified":"2021-01-25T14:35:06","modified_gmt":"2021-01-25T06:35:06","slug":"python-gil%e5%85%a8%e5%b1%80%e8%a7%a3%e9%87%8a%e5%99%a8%e9%94%81%e8%af%a6%e8%a7%a3%ef%bc%88%e6%b7%b1%e5%ba%a6%e5%89%96%e6%9e%90%ef%bc%89","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4987","title":{"rendered":"Python GIL\u5168\u5c40\u89e3\u91ca\u5668\u9501\u8be6\u89e3\uff08\u6df1\u5ea6\u5256\u6790\uff09"},"content":{"rendered":"<div id=\"arc-body\">\n<div id=\"arc-body\">\n<div id=\"ad-arc-top\">\n<p class=\"text\"><a href=\"http:\/\/c.biancheng.net\/view\/5537.html\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div id=\"arc-body\">\u901a\u8fc7\u524d\u9762\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u4e86\u89e3\u4e86 Pyton \u5e76\u53d1\u7f16\u7a0b\u7684\u7279\u6027\u4ee5\u53ca\u4ec0\u4e48\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002\u5176\u5b9e\u9664\u6b64\u4e4b\u5916\uff0c<a href=\"http:\/\/c.biancheng.net\/python\/\" target=\"_blank\" rel=\"noopener\">Python<\/a>\u00a0\u591a\u7ebf\u7a0b\u8fd8\u6709\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\uff0c\u5c31\u662f\u672c\u8282\u8981\u8bb2\u7684 GIL\u3002<\/p>\n<p>GIL\uff0c\u4e2d\u6587\u8bd1\u4e3a\u5168\u5c40\u89e3\u91ca\u5668\u9501\u3002\u5728\u8bb2\u89e3 GIL \u4e4b\u524d\uff0c\u9996\u5148\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u76f4\u89c2\u611f\u53d7\u4e00\u4e0b GIL \u5728 Python \u591a\u7ebf\u7a0b\u7a0b\u5e8f\u8fd0\u884c\u7684\u5f71\u54cd\u3002<\/p>\n<p>\u9996\u5148\u8fd0\u884c\u5982\u4e0b\u7a0b\u5e8f\uff1a<\/p>\n<div class=\"snippet-container\">\n<div class=\"sh_bright snippet-wrap\">\n<ol class=\"snippet-num\">\n<li><span class=\"sh_preproc\">import<\/span> time<\/li>\n<li>start <span class=\"sh_symbol\">=<\/span> time<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">clock<\/span><span class=\"sh_symbol\">()<\/span><\/li>\n<li><span class=\"sh_keyword\">def<\/span> <span class=\"sh_function\">CountDown<\/span><span class=\"sh_symbol\">(<\/span>n<span class=\"sh_symbol\">):<\/span><\/li>\n<li><span class=\"sh_keyword\">while<\/span> n <span class=\"sh_symbol\">&gt;<\/span> <span class=\"sh_number\">0<\/span><span class=\"sh_symbol\">:<\/span><\/li>\n<li>n <span class=\"sh_symbol\">-=<\/span> <span class=\"sh_number\">1<\/span><\/li>\n<li><span class=\"sh_function\">CountDown<\/span><span class=\"sh_symbol\">(<\/span><span class=\"sh_number\">100000<\/span><span class=\"sh_symbol\">)<\/span><\/li>\n<li><span class=\"sh_keyword\">print<\/span><span class=\"sh_symbol\">(<\/span><span class=\"sh_string\">&#8220;Time used:&#8221;<\/span><span class=\"sh_symbol\">,(<\/span>time<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">clock<\/span><span class=\"sh_symbol\">()<\/span> <span class=\"sh_symbol\">&#8211;<\/span> start<span class=\"sh_symbol\">))<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<p class=\"info-box\">Time used: 0.0039529000000000005<\/p>\n<p>\u5728\u6211\u4eec\u7684\u5370\u8c61\u4e2d\uff0c\u4f7f\u7528\u591a\u4e2a\uff08\u9002\u91cf\uff09\u7ebf\u7a0b\u662f\u53ef\u4ee5\u52a0\u5feb\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u4e0a\u9762\u7a0b\u5e8f\u6539\u6210\u5982\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<div class=\"snippet-container\">\n<div class=\"sh_bright snippet-wrap\">\n<ol class=\"snippet-num\">\n<li><span class=\"sh_preproc\">import<\/span> time<\/li>\n<li><span class=\"sh_preproc\">from<\/span> threading <span class=\"sh_preproc\">import<\/span> Thread<\/li>\n<li>start <span class=\"sh_symbol\">=<\/span> time<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">clock<\/span><span class=\"sh_symbol\">()<\/span><\/li>\n<li><span class=\"sh_keyword\">def<\/span> <span class=\"sh_function\">CountDown<\/span><span class=\"sh_symbol\">(<\/span>n<span class=\"sh_symbol\">):<\/span><\/li>\n<li><span class=\"sh_keyword\">while<\/span> n <span class=\"sh_symbol\">&gt;<\/span> <span class=\"sh_number\">0<\/span><span class=\"sh_symbol\">:<\/span><\/li>\n<li>n <span class=\"sh_symbol\">-=<\/span> <span class=\"sh_number\">1<\/span><\/li>\n<li>t1 <span class=\"sh_symbol\">=<\/span> <span class=\"sh_function\">Thread<\/span><span class=\"sh_symbol\">(<\/span>target<span class=\"sh_symbol\">=<\/span>CountDown<span class=\"sh_symbol\">,<\/span> args<span class=\"sh_symbol\">=[<\/span><span class=\"sh_number\">100000<\/span> <span class=\"sh_symbol\">\/\/<\/span> <span class=\"sh_number\">2<\/span><span class=\"sh_symbol\">])<\/span><\/li>\n<li>t2 <span class=\"sh_symbol\">=<\/span> <span class=\"sh_function\">Thread<\/span><span class=\"sh_symbol\">(<\/span>target<span class=\"sh_symbol\">=<\/span>CountDown<span class=\"sh_symbol\">,<\/span> args<span class=\"sh_symbol\">=[<\/span><span class=\"sh_number\">100000<\/span> <span class=\"sh_symbol\">\/\/<\/span> <span class=\"sh_number\">2<\/span><span class=\"sh_symbol\">])<\/span><\/li>\n<li>t1<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">start<\/span><span class=\"sh_symbol\">()<\/span><\/li>\n<li>t2<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">start<\/span><span class=\"sh_symbol\">()<\/span><\/li>\n<li>t1<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">join<\/span><span class=\"sh_symbol\">()<\/span><\/li>\n<li>t2<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">join<\/span><span class=\"sh_symbol\">()<\/span><\/li>\n<li><span class=\"sh_keyword\">print<\/span><span class=\"sh_symbol\">(<\/span><span class=\"sh_string\">&#8220;Time used:&#8221;<\/span><span class=\"sh_symbol\">,(<\/span>time<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">clock<\/span><span class=\"sh_symbol\">()<\/span> <span class=\"sh_symbol\">&#8211;<\/span> start<span class=\"sh_symbol\">))<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<p class=\"info-box\">Time used: 0.006673<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6b64\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e86 2 \u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u548c\u4e0a\u9762\u4ee3\u7801\u76f8\u540c\u7684\u5de5\u4f5c\uff0c\u4f46\u4ece\u8f93\u51fa\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u8fd0\u884c\u6548\u7387\u975e\u4f46\u6ca1\u6709\u63d0\u9ad8\uff0c\u53cd\u800c\u964d\u4f4e\u4e86\u3002<br \/>\n\u5982\u679c\u4f7f\u7528\u66f4\u591a\u7ebf\u7a0b\u8fdb\u884c\u5c1d\u8bd5\uff0c\u4f1a\u53d1\u73b0\u5176\u8fd0\u884c\u6548\u7387\u548c 2 \u4e2a\u7ebf\u7a0b\u6548\u7387\u51e0\u4e4e\u4e00\u6837\uff08\u672c\u673a\u5668\u6d4b\u8bd5\u4f7f\u7528 4 \u4e2a\u7ebf\u7a0b\uff0c\u5176\u6267\u884c\u6548\u7387\u7ea6\u4e3a 0.005\uff09\u3002\u8fd9\u91cc\u4e0d\u518d\u7ed9\u51fa\u5177\u4f53\u6d4b\u8bd5\u4ee3\u7801\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u81ea\u884c\u6d4b\u8bd5\u3002<\/p>\n<p>\u662f\u4e0d\u662f\u548c\u4f60\u731c\u60f3\u7684\u7ed3\u679c\u4e0d\u4e00\u6837\uff1f\u4e8b\u5b9e\u4e0a\uff0c\u5f97\u5230\u8fd9\u6837\u7684\u7ed3\u679c\u662f\u80af\u5b9a\u7684\uff0c\u56e0\u4e3a GIL \u9650\u5236\u4e86 Python \u591a\u7ebf\u7a0b\u7684\u6027\u80fd\u4e0d\u4f1a\u50cf\u6211\u4eec\u9884\u671f\u7684\u90a3\u6837\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u4ec0\u4e48\u662f GIL \u5462\uff1fGIL \u662f\u6700\u6d41\u7a0b\u7684 CPython \u89e3\u91ca\u5668\uff08\u5e73\u5e38\u79f0\u4e3a Python\uff09\u4e2d\u7684\u4e00\u4e2a\u6280\u672f\u672f\u8bed\uff0c\u4e2d\u6587\u8bd1\u4e3a\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff0c\u5176\u672c\u8d28\u4e0a\u7c7b\u4f3c\u64cd\u4f5c\u7cfb\u7edf\u7684 Mutex\u3002GIL \u7684\u529f\u80fd\u662f\uff1a\u5728 CPython \u89e3\u91ca\u5668\u4e2d\u6267\u884c\u7684\u6bcf\u4e00\u4e2a Python \u7ebf\u7a0b\uff0c\u90fd\u4f1a\u5148\u9501\u4f4f\u81ea\u5df1\uff0c\u4ee5\u963b\u6b62\u522b\u7684\u7ebf\u7a0b\u6267\u884c\u3002<\/p>\n<p>\u5f53\u7136\uff0cCPython \u4e0d\u53ef\u80fd\u5bb9\u5fcd\u4e00\u4e2a\u7ebf\u7a0b\u4e00\u76f4\u72ec\u5360\u89e3\u91ca\u5668\uff0c\u5b83\u4f1a\u8f6e\u6d41\u6267\u884c Python \u7ebf\u7a0b\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7528\u6237\u770b\u5230\u7684\u5c31\u662f\u201c\u4f2a\u201d\u5e76\u884c\uff0c\u5373 Python \u7ebf\u7a0b\u5728\u4ea4\u66ff\u6267\u884c\uff0c\u6765\u6a21\u62df\u771f\u6b63\u5e76\u884c\u7684\u7ebf\u7a0b\u3002<\/p>\n<p>\u6709\u8bfb\u8005\u53ef\u80fd\u4f1a\u95ee\uff0c\u65e2\u7136 CPython \u80fd\u63a7\u5236\u7ebf\u7a0b\u4f2a\u5e76\u884c\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981 GIL \u5462\uff1f\u5176\u5b9e\uff0c\u8fd9\u548c CPython \u7684\u5e95\u5c42\u5185\u5b58\u7ba1\u7406\u6709\u5173\u3002<\/p>\n<p>CPython \u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u6765\u7ba1\u7406\u5185\u5bb9\uff0c\u6240\u6709 Python \u811a\u672c\u4e2d\u521b\u5efa\u7684\u5b9e\u4f8b\uff0c\u90fd\u4f1a\u914d\u5907\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\uff0c\u6765\u8bb0\u5f55\u6709\u591a\u5c11\u4e2a\u6307\u9488\u6765\u6307\u5411\u5b83\u3002\u5f53\u5b9e\u4f8b\u7684\u5f15\u7528\u8ba1\u6570\u7684\u503c\u4e3a 0 \u65f6\uff0c\u4f1a\u81ea\u52a8\u91ca\u653e\u5176\u6240\u5360\u7684\u5185\u5b58\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u770b\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p class=\"info-box\">&gt;&gt;&gt; import sys<br \/>\n&gt;&gt;&gt; a = []<br \/>\n&gt;&gt;&gt; b = a<br \/>\n&gt;&gt;&gt; sys.getrefcount(a)<br \/>\n3<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0ca \u7684\u5f15\u7528\u8ba1\u6570\u503c\u4e3a 3\uff0c\u56e0\u4e3a\u6709 a\u3001b \u548c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7684 getrefcount \u90fd\u5f15\u7528\u4e86\u4e00\u4e2a\u7a7a\u5217\u8868\u3002<\/p>\n<p>\u5047\u8bbe\u6709\u4e24\u4e2a Python \u7ebf\u7a0b\u540c\u65f6\u5f15\u7528 a\uff0c\u90a3\u4e48\u53cc\u65b9\u5c31\u90fd\u4f1a\u5c1d\u8bd5\u64cd\u4f5c\u8be5\u6570\u636e\uff0c\u5f88\u6709\u53ef\u80fd\u9020\u6210\u5f15\u7528\u8ba1\u6570\u7684\u6761\u4ef6\u7ade\u4e89\uff0c\u5bfc\u81f4\u5f15\u7528\u8ba1\u6570\u53ea\u589e\u52a0 1\uff08\u5b9e\u9645\u5e94\u589e\u52a0 2\uff09\uff0c\u8fd9\u9020\u6210\u7684\u540e\u679c\u662f\uff0c\u5f53\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u4f1a\u628a\u5f15\u7528\u8ba1\u6570\u51cf\u5c11 1\uff0c\u6b64\u65f6\u53ef\u80fd\u5df2\u7ecf\u8fbe\u5230\u91ca\u653e\u5185\u5b58\u7684\u6761\u4ef6\uff08\u5f15\u7528\u8ba1\u6570\u4e3a 0\uff09\uff0c\u5f53\u7b2c 2 \u4e2a\u7ebf\u7a0b\u518d\u6b21\u89c6\u56fe\u8bbf\u95ee a \u65f6\uff0c\u5c31\u65e0\u6cd5\u627e\u5230\u6709\u6548\u7684\u5185\u5b58\u4e86\u3002<\/p>\n<p>\u6240\u4ee5\uff0cCPython \u5f15\u8fdb GIL\uff0c\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4e0a\u89c4\u907f\u7c7b\u4f3c\u5185\u5b58\u7ba1\u7406\u8fd9\u6837\u590d\u6742\u7684\u7ade\u4e89\u98ce\u9669\u95ee\u9898\u3002<\/p>\n<h2>Python GIL\u5e95\u5c42\u5b9e\u73b0\u539f\u7406<\/h2>\n<div><img decoding=\"async\" src=\"http:\/\/c.biancheng.net\/uploads\/allimg\/190830\/2-1ZS012105L23.gif\" alt=\"GIL\u5de5\u4f5c\u6d41\u7a0b\u793a\u610f\u56fe\" \/><br \/>\n\u56fe 1 GIL \u5de5\u4f5c\u6d41\u7a0b\u793a\u610f\u56fe<\/div>\n<p>\u4e0a\u9762\u8fd9\u5f20\u56fe\uff0c\u5c31\u662f GIL \u5728 Python \u7a0b\u5e8f\u7684\u5de5\u4f5c\u793a\u4f8b\u3002\u5176\u4e2d\uff0cThread 1\u30012\u30013 \u8f6e\u6d41\u6267\u884c\uff0c\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u5728\u5f00\u59cb\u6267\u884c\u65f6\uff0c\u90fd\u4f1a\u9501\u4f4f GIL\uff0c\u4ee5\u963b\u6b62\u522b\u7684\u7ebf\u7a0b\u6267\u884c\uff1b\u540c\u6837\u7684\uff0c\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u4e00\u6bb5\u540e\uff0c\u4f1a\u91ca\u653e GIL\uff0c\u4ee5\u5141\u8bb8\u522b\u7684\u7ebf\u7a0b\u5f00\u59cb\u5229\u7528\u8d44\u6e90\u3002<\/p>\n<p>\u8bfb\u8005\u53ef\u80fd\u4f1a\u95ee\uff0c\u4e3a\u4ec0\u4e48 Python \u7ebf\u7a0b\u4f1a\u53bb\u4e3b\u52a8\u91ca\u653e GIL \u5462\uff1f\u6bd5\u7adf\uff0c\u5982\u679c\u4ec5\u4ec5\u8981\u6c42 Python \u7ebf\u7a0b\u5728\u5f00\u59cb\u6267\u884c\u65f6\u9501\u4f4f GIL\uff0c\u4e14\u6c38\u8fdc\u4e0d\u53bb\u91ca\u653e GIL\uff0c\u90a3\u522b\u7684\u7ebf\u7a0b\u5c31\u90fd\u6ca1\u6709\u8fd0\u884c\u7684\u673a\u4f1a\u3002\u5176\u5b9e\uff0cCPython \u4e2d\u8fd8\u6709\u53e6\u4e00\u4e2a\u673a\u5236\uff0c\u53eb\u505a\u95f4\u9694\u5f0f\u68c0\u67e5\uff08check_interval\uff09\uff0c\u610f\u601d\u662f CPython \u89e3\u91ca\u5668\u4f1a\u53bb\u8f6e\u8be2\u68c0\u67e5\u7ebf\u7a0b GIL \u7684\u9501\u4f4f\u60c5\u51b5\uff0c\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\uff0cPython \u89e3\u91ca\u5668\u5c31\u4f1a\u5f3a\u5236\u5f53\u524d\u7ebf\u7a0b\u53bb\u91ca\u653e GIL\uff0c\u8fd9\u6837\u522b\u7684\u7ebf\u7a0b\u624d\u80fd\u6709\u6267\u884c\u7684\u673a\u4f1a\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u4e0d\u540c\u7248\u672c\u7684 Python\uff0c\u5176\u95f4\u9694\u5f0f\u68c0\u67e5\u7684\u5b9e\u73b0\u65b9\u5f0f\u5e76\u4e0d\u4e00\u6837\u3002\u65e9\u671f\u7684 Python \u662f 100 \u4e2a\u523b\u5ea6\uff08\u5927\u81f4\u5bf9\u5e94\u4e86 1000 \u4e2a\u5b57\u8282\u7801\uff09\uff1b\u800c Python 3 \u4ee5\u540e\uff0c\u95f4\u9694\u65f6\u95f4\u5927\u81f4\u4e3a 15 \u6beb\u79d2\u3002\u5f53\u7136\uff0c\u6211\u4eec\u4e0d\u5fc5\u7ec6\u7a76\u5177\u4f53\u591a\u4e45\u4f1a\u5f3a\u5236\u91ca\u653e GIL\uff0c\u8bfb\u8005\u53ea\u9700\u8981\u660e\u767d\uff0cCPython \u89e3\u91ca\u5668\u4f1a\u5728\u4e00\u4e2a\u201c\u5408\u7406\u201d\u7684\u65f6\u95f4\u8303\u56f4\u5185\u91ca\u653e GIL \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u6574\u4f53\u6765\u8bf4\uff0c\u6bcf\u4e00\u4e2a Python \u7ebf\u7a0b\u90fd\u662f\u7c7b\u4f3c\u8fd9\u6837\u5faa\u73af\u7684\u5c01\u88c5\uff0c\u6765\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<div class=\"snippet-container\">\n<div class=\"sh_bright snippet-wrap\">\n<ol class=\"snippet-num\">\n<li><span class=\"sh_keyword\">for<\/span> <span class=\"sh_symbol\">(;;)<\/span> <span class=\"sh_symbol\">{<\/span><\/li>\n<li><span class=\"sh_keyword\">if<\/span> <span class=\"sh_symbol\">(&#8211;<\/span>ticker <span class=\"sh_symbol\">&lt;<\/span> <span class=\"sh_number\">0<\/span><span class=\"sh_symbol\">)<\/span> <span class=\"sh_symbol\">{<\/span><\/li>\n<li>ticker <span class=\"sh_symbol\">=<\/span> check_interval<span class=\"sh_symbol\">;<\/span><\/li>\n<li><span class=\"sh_symbol\">\/*<\/span> Give another thread a chance <span class=\"sh_symbol\">*\/<\/span><\/li>\n<li><span class=\"sh_function\">PyThread_release_lock<\/span><span class=\"sh_symbol\">(<\/span>interpreter_lock<span class=\"sh_symbol\">);<\/span><\/li>\n<li><span class=\"sh_symbol\">\/*<\/span> Other threads may run now <span class=\"sh_symbol\">*\/<\/span><\/li>\n<li><span class=\"sh_function\">PyThread_acquire_lock<\/span><span class=\"sh_symbol\">(<\/span>interpreter_lock<span class=\"sh_symbol\">,<\/span> <span class=\"sh_number\">1<\/span><span class=\"sh_symbol\">);<\/span><\/li>\n<li><span class=\"sh_symbol\">}<\/span><\/li>\n<li>bytecode <span class=\"sh_symbol\">=<\/span> <span class=\"sh_symbol\">*<\/span>next_instr<span class=\"sh_symbol\">++;<\/span><\/li>\n<li><span class=\"sh_function\">switch<\/span> <span class=\"sh_symbol\">(<\/span>bytecode<span class=\"sh_symbol\">)<\/span> <span class=\"sh_symbol\">{<\/span><\/li>\n<li><span class=\"sh_symbol\">\/*<\/span> execute the next instruction <span class=\"sh_symbol\">&#8230;<\/span> <span class=\"sh_symbol\">*\/<\/span><\/li>\n<li><span class=\"sh_symbol\">}<\/span><\/li>\n<li><span class=\"sh_symbol\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>\u4ece\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u6bcf\u4e2a Python \u7ebf\u7a0b\u90fd\u4f1a\u5148\u68c0\u67e5 ticker \u8ba1\u6570\u3002\u53ea\u6709\u5728 ticker \u5927\u4e8e 0 \u7684\u60c5\u51b5\u4e0b\uff0c\u7ebf\u7a0b\u624d\u4f1a\u53bb\u6267\u884c\u81ea\u5df1\u7684\u4ee3\u7801\u3002<\/p>\n<h2>Python GIL\u4e0d\u80fd\u7edd\u5bf9\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168<\/h2>\n<p>\u6ce8\u610f\uff0c\u6709\u4e86 GIL\uff0c\u5e76\u4e0d\u610f\u5473\u7740 Python \u7a0b\u5e8f\u5458\u5c31\u4e0d\u7528\u53bb\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u4e86\uff0c\u56e0\u4e3a\u5373\u4fbf GIL \u4ec5\u5141\u8bb8\u4e00\u4e2a Python \u7ebf\u7a0b\u6267\u884c\uff0c\u4f46\u522b\u5fd8\u4e86 Python \u8fd8\u6709 check interval \u8fd9\u6837\u7684\u62a2\u5360\u673a\u5236\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u8fd0\u884c\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<div class=\"snippet-container\">\n<div class=\"sh_bright snippet-wrap\">\n<div class=\"snippet-menu sh_sourceCode\"><a class=\"snippet-text sh_url\" href=\"http:\/\/c.biancheng.net\/view\/5537.html#\">\u7eaf\u6587\u672c<\/a><a class=\"snippet-window sh_url\" href=\"http:\/\/c.biancheng.net\/view\/5537.html#\">\u590d\u5236<\/a><\/div>\n<ol class=\"snippet-num\">\n<li><span class=\"sh_preproc\">import<\/span> threading<\/li>\n<li>n <span class=\"sh_symbol\">=<\/span> <span class=\"sh_number\">0<\/span><\/li>\n<li><span class=\"sh_keyword\">def<\/span> <span class=\"sh_function\">foo<\/span><span class=\"sh_symbol\">():<\/span><\/li>\n<li><span class=\"sh_keyword\">global<\/span> n<\/li>\n<li>n <span class=\"sh_symbol\">+=<\/span> <span class=\"sh_number\">1<\/span><\/li>\n<li>threads <span class=\"sh_symbol\">=<\/span> <span class=\"sh_symbol\">[]<\/span><\/li>\n<li><span class=\"sh_keyword\">for<\/span> i <span class=\"sh_keyword\">in<\/span> <span class=\"sh_function\">range<\/span><span class=\"sh_symbol\">(<\/span><span class=\"sh_number\">100<\/span><span class=\"sh_symbol\">):<\/span><\/li>\n<li>t <span class=\"sh_symbol\">=<\/span> threading<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">Thread<\/span><span class=\"sh_symbol\">(<\/span>target<span class=\"sh_symbol\">=<\/span>foo<span class=\"sh_symbol\">)<\/span><\/li>\n<li>threads<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">append<\/span><span class=\"sh_symbol\">(<\/span>t<span class=\"sh_symbol\">)<\/span><\/li>\n<li><span class=\"sh_keyword\">for<\/span> t <span class=\"sh_keyword\">in<\/span> threads<span class=\"sh_symbol\">:<\/span><\/li>\n<li>t<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">start<\/span><span class=\"sh_symbol\">()<\/span><\/li>\n<li><span class=\"sh_keyword\">for<\/span> t <span class=\"sh_keyword\">in<\/span> threads<span class=\"sh_symbol\">:<\/span><\/li>\n<li>t<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">join<\/span><span class=\"sh_symbol\">()<\/span><\/li>\n<li><span class=\"sh_keyword\">print<\/span><span class=\"sh_symbol\">(<\/span>n<span class=\"sh_symbol\">)<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>\u6267\u884c\u6b64\u4ee3\u7801\u4f1a\u53d1\u73b0\uff0c\u5176\u5927\u90e8\u5206\u65f6\u5019\u4f1a\u6253\u5370 100\uff0c\u4f46\u6709\u65f6\u4e5f\u4f1a\u6253\u5370 99 \u6216\u8005 98\uff0c\u539f\u56e0\u5728\u4e8e\u00a0n+=1 \u8fd9\u4e00\u53e5\u4ee3\u7801\u8ba9\u7ebf\u7a0b\u5e76\u4e0d\u5b89\u5168\u3002\u5982\u679c\u53bb\u7ffb\u8bd1 foo \u8fd9\u4e2a\u51fd\u6570\u7684\u5b57\u8282\u7801\u5c31\u4f1a\u53d1\u73b0\uff0c\u5b83\u5b9e\u9645\u4e0a\u662f\u7531\u4e0b\u9762\u56db\u884c\u5b57\u8282\u7801\u7ec4\u6210\uff1a<\/p>\n<p class=\"info-box\">&gt;&gt;&gt; import dis<br \/>\n&gt;&gt;&gt; dis.dis(foo)<br \/>\nLOAD_GLOBAL\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 (n)<br \/>\nLOAD_CONST\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 (1)<br \/>\nINPLACE_ADD<br \/>\nSTORE_GLOBAL\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 (n)<\/p>\n<p>\u800c\u8fd9\u56db\u884c\u5b57\u8282\u7801\u4e2d\u95f4\u90fd\u662f\u6709\u53ef\u80fd\u88ab\u6253\u65ad\u7684\uff01\u6240\u4ee5\uff0c\u5343\u4e07\u522b\u4ee5\u4e3a\u6709\u4e86 GIL \u7a0b\u5e8f\u5c31\u4e0d\u4f1a\u4ea7\u751f\u7ebf\u7a0b\u95ee\u9898\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u3002<\/p><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; \u901a\u8fc7\u524d\u9762\u7684\u5b66\u4e60\uff0c<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-4987","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4987"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4987\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}