{"id":201,"date":"2023-03-14T12:05:58","date_gmt":"2023-03-14T04:05:58","guid":{"rendered":"https:\/\/www.road-trip.cc\/?p=201"},"modified":"2023-03-14T12:05:59","modified_gmt":"2023-03-14T04:05:59","slug":"%e4%b8%80%e4%b8%aapython%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%92%8ccpu%e8%b4%9f%e8%bd%bd%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.road-trip.cc\/?p=201","title":{"rendered":"\u4e00\u4e2aPython\u591a\u7ebf\u7a0b\u548cCPU\u8d1f\u8f7d\u7684\u95ee\u9898"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<p>\u5de5\u4f5c\u8fc7\u7a0b\u4e2d\u89c2\u5bdf\u5230\u8fd9\u4e48\u4e00\u4e2a\u73b0\u8c61\uff1apython+flask\u8fd9\u5bf9\u642d\u6863\u4e2d\uff0c\u5f53\u4e00\u4e2arequest\u8fdb\u6765\u89e6\u53d1\u4e00\u4e2a\u957f\u65f6\u95f4\u4efb\u52a1\u65f6\uff0c\u6211\u4e00\u822c\u90fd\u4f1a\u8d77\u4e00\u5230\u51e0\u4e2a\u7ebf\u7a0b\u53bb\u5f02\u6b65\u5904\u7406\u3002\u4f46\u662f\u6211\u53d1\u73b0\u4e0d\u7ba1\u8d77\u51e0\u4e2a\u7ebf\u7a0b\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u6240\u5728\u7684python\u8fdb\u7a0b\u6700\u591a\u53ea\u80fd\u8dd1\u6ee1\u4e00\u4e2aCPU\u6838\u5fc3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53cd\u601d<\/h2>\n\n\n\n<p>\u4e0a\u8ff0\u73b0\u8c61\u5176\u5b9e\u5df2\u7ecf\u89c2\u5bdf\u5230\u5f88\u591a\u6b21\u4e86\uff0c\u4e5f\u6709\u5f88\u957f\u65f6\u95f4\u4e86\uff0c\u4f46\u671f\u95f4\u4e00\u76f4\u6ca1\u6709\u82b1\u65f6\u95f4\u53bb\u8c03\u7814\u8fd9\u4e2a\u95ee\u9898\u3002\u662f\u5728\u4e0d\u5e94\u8be5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8c03\u67e5\u7ed3\u679c<\/h2>\n\n\n\n<p>Python\u91cc\u9762\u5f15\u5165\u4e00\u4e2a\u673a\u5236GIL\uff08<strong>global interpreter lock<\/strong>\uff09\uff0c\u8bb0\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff0c\u4ed6\u4f1a\u8ba9\u540c\u4e00\u65f6\u523b\uff0c\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cpython\u4ee3\u7801\u3002\u4e5f\u5c31\u662f\u5b83\u7ed9\u89e3\u91ca\u5668\u52a0\u4e86\u4e00\u628a\u9501\uff0c\u907f\u514d\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u51fa\u7684\u5404\u79cd\u95ee\u9898\u3002\u7b97\u662f\u4e00\u79cd\u8bbe\u8ba1\u4e0a\u7684\u5747\u8861\u8003\u91cf\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The mechanism used by the&nbsp;<a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-CPython\">CPython<\/a>&nbsp;interpreter to assure that only one thread executes Python&nbsp;<a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-bytecode\">bytecode<\/a>&nbsp;at a time. This simplifies the CPython implementation by making the object model (including critical built-in types such as&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#dict\"><code>dict<\/code><\/a>) implicitly safe against concurrent access. Locking the entire interpreter makes it easier for the interpreter to be multi-threaded, at the expense of much of the parallelism afforded by multi-processor machines.<\/p>\n\n\n\n<p>However, some extension modules, either standard or third-party, are designed so as to release the GIL when doing computationally intensive tasks such as compression or hashing. Also, the GIL is always released when doing I\/O.<\/p>\n\n\n\n<p>Past efforts to create a \u201cfree-threaded\u201d interpreter (one which locks shared data at a much finer granularity) have not been successful because performance suffered in the common single-processor case. It is believed that overcoming this performance issue would make the implementation much more complicated and therefore costlier to maintain.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u6446\u8131\u9650\u5236<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li> \u4f7f\u7528C\u7b49\u5b9e\u73b0\u5e93\uff0c\u6216\u8005\u54cd\u5e94\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u91ca\u653eGIL<\/li>\n\n\n\n<li>\u4f7f\u7528\u591a\u8fdb\u7a0b\u6a21\u578b<\/li>\n\n\n\n<li>\u4f7f\u7528\u7b2c\u4e09\u65b9\u89e3\u91ca\u5668\uff0c\u6bd4\u5982Jypthon\uff08\u8be5\u89e3\u91ca\u5668\u53ea\u67092.x\u7248\u672c\uff09<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u591a\u8fdb\u7a0b\u6a21\u578b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import concurrent.futures\nimport math\n\nPRIMES = &#91;\n    112272535095293,\n    112582705942171,\n    112272535095293,\n    115280095190773,\n    115797848077099,\n    1099726899285419]\n\ndef is_prime(n):\n    if n &lt; 2:\n        return False\n    if n == 2:\n        return True\n    if n % 2 == 0:\n        return False\n\n    sqrt_n = int(math.floor(math.sqrt(n)))\n    for i in range(3, sqrt_n + 1, 2):\n        if n % i == 0:\n            return False\n    return True\n\ndef main():\n    with concurrent.futures.ProcessPoolExecutor() as executor:\n        for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):\n            print('%d is prime: %s' % (number, prime))\n\nif __name__ == '__main__':\n    main()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-global-interpreter-lock\">Glossary \u2014 Python 3.11.2 documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/concurrent.futures.html?highlight=concurrent#module-concurrent.futures\">concurrent.futures \u2014 Launching parallel tasks \u2014 Python 3.11.2 documentation<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u5de5\u4f5c\u8fc7\u7a0b\u4e2d\u89c2\u5bdf\u5230\u8fd9\u4e48\u4e00\u4e2a\u73b0\u8c61\uff1apython+flask\u8fd9\u5bf9\u642d\u6863\u4e2d\uff0c\u5f53\u4e00\u4e2arequest\u8fdb\u6765\u89e6\u53d1\u4e00\u4e2a\u957f\u65f6\u95f4 &#8230; <a title=\"\u4e00\u4e2aPython\u591a\u7ebf\u7a0b\u548cCPU\u8d1f\u8f7d\u7684\u95ee\u9898\" class=\"read-more\" href=\"https:\/\/www.road-trip.cc\/?p=201\" aria-label=\"\u9605\u8bfb \u4e00\u4e2aPython\u591a\u7ebf\u7a0b\u548cCPU\u8d1f\u8f7d\u7684\u95ee\u9898\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[54,24],"class_list":["post-201","post","type-post","status-publish","format-standard","hentry","category-7","tag-gil","tag-python"],"_links":{"self":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=201"}],"version-history":[{"count":1,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/201\/revisions"}],"predecessor-version":[{"id":202,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/201\/revisions\/202"}],"wp:attachment":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}