{"version":"1.0","provider_name":"Jui-Nan Lin&#039;s Blog","provider_url":"https:\/\/jnlin.org","author_name":"jnlin","author_url":"https:\/\/jnlin.org\/author\/jnlin\/","title":"Python \u7684 GIL (Global Interpreter Lock)","html":"<p>\u524d\u5e7e\u500b\u6708\u5728\u505a\u5c08\u6848\u7684\u6642\u5019\uff0c\u767c\u73fe\u7528 <a href=\"http:\/\/code.google.com\/p\/modwsgi\/\">mod_wsgi<\/a> + <a href=\"http:\/\/webpy.org\/\">web.py<\/a> \u5beb\u7684\u7a0b\u5f0f\uff0c\u7406\u8ad6\u4e0a\u652f\u63f4 multi-thread\uff0c\u61c9\u8a72\u53ef\u4ee5\u5b8c\u5168\u5229\u7528\u5728\u591a\u6838 CPU Server \u7684\u512a\u52e2\uff1b\u4f46\u5be6\u969b\u4e0a\u8dd1\u7684\u6642\u5019\uff0c\u6703\u6709\u6c92\u8fa6\u6cd5\u628a CPU Resource \u5403\u5b8c\u7684\u554f\u984c\u3002\u7576\u6642\u56e0\u70ba\u6642\u9593\u5f88\u8d95\uff0c\u60f3\u5230\u7684\u89e3\u6cd5\u5c31\u662f\u591a\u8dd1\u5e7e\u500b Process\uff1a     <br \/><code>     <br \/>&lt;VirtualHost *:80&gt;&#160; <br \/>&#160;&#160; DocumentRoot \u201c\/srv\/web\/webroot&quot;&#160; <br \/>&#160;&#160; ServerName python.example.com&#160; <br \/>&#160;&#160; WSGIProcessGroup pythonexample&#160; <br \/>&#160;&#160; WSGIDaemonProcess pythonexample processes=<font color=\"#ff0000\">16<\/font> threads=128 maximum-requests=1024 display-name=%{GROUP}       <br \/>&lt;\/VirtualHost&gt; <\/code><\/p>  <p>\u5728 <a href=\"http:\/\/coscup.org\/\">COSCUP<\/a> \u8ddf <a href=\"http:\/\/www.facebook.com\/lwhsu\">lwhsu<\/a> \u804a\u5230\u9019\u4ef6\u4e8b\uff0c\u4ed6\u8b1b\u5230 CPython \u7684 GIL\uff0c\u56de\u4f86\u67e5\u4e86\u4e00\u4e0b\u679c\u7136\u5c31\u662f\u9019\u500b\u539f\u56e0\u3002\u525b\u597d Web \u4e0d\u9700\u8981\u5728\u6bcf\u500b thread \u4e4b\u9593\u4e92\u76f8\u50b3\u8cc7\u6599\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u7528\u591a\u500b Process \u89e3\u6389\u9019\u500b\u554f\u984c\u3002<\/p>  <p>\u96d6\u7136 Python \u5728\u57f7\u884c\u524d\u5c31\u628a\u7a0b\u5f0f\u78bc\u8f49\u6210 byte code\u4e86\uff0c\u4f46\u56e0\u70ba\u9019\u500b Lock \u662f\u5be6\u505a\u5728 CPython \u7684 Bytecode Interpreter \u88e1\u9762\uff0c\u6240\u4ee5\u5c0e\u81f4\u4e86\u540c\u6642\u53ea\u6703\u6709\u4e00\u500b thread \u57f7\u884c Python bytecode\uff0c\u4e5f\u56e0\u6b64\u7121\u6cd5\u5229\u7528\u5230\u591a\u6838 CPU \u7684\u512a\u52e2 ((<a href=\"http:\/\/stackoverflow.com\/questions\/4748787\/python-threading-and-gil\">Stackoverflow: Python threading and gil<\/a>))\u3002<\/p>  <p>\u9664\u4e86\u591a\u8dd1\u5e7e\u500b Process \u4e4b\u5916\uff0c\u4e5f\u53ef\u4ee5\u7528 <a href=\"http:\/\/docs.python.org\/library\/ctypes.html\">ctypes<\/a> \u914d\u5408\u7528 C \u5beb\u7684 library \u4f86\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u4e0d\u904e\u4ee5 Web \u61c9\u7528\u4f86\u8aaa\uff0c\u5beb C \u7684\u6a5f\u6703\u4e0d\u9ad8\u5c31\u662f\u4e86\u3002<\/p>","type":"rich"}