{"id":223800,"date":"2020-11-25T00:45:42","date_gmt":"2020-11-24T16:45:42","guid":{"rendered":"http:\/\/4563.org\/?p=223800"},"modified":"2020-11-25T00:45:42","modified_gmt":"2020-11-24T16:45:42","slug":"c-%e8%af%ad%e8%a8%80%e6%8f%92%e4%bb%b6%e5%8f%af%e4%bb%a5%e9%bb%98%e8%ae%a4%e6%91%86%e8%84%b1-python-%e7%9a%84-gil-%e7%ba%a6%e6%9d%9f%e5%90%97%ef%bc%9f%e4%b8%ba%e4%bb%80%e4%b9%88%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=223800","title":{"rendered":"C \u8bed\u8a00\u63d2\u4ef6\u53ef\u4ee5\u9ed8\u8ba4\u6446\u8131 Python \u7684 GIL \u7ea6\u675f\u5417\uff1f\u4e3a\u4ec0\u4e48\u6d4b\u8bd5\u7ed3\u679c\u4e0d\u662f\u5982\u6b64\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C \u8bed\u8a00\u63d2\u4ef6\u53ef\u4ee5\u9ed8\u8ba4\u6446\u8131 Python \u7684 GIL \u7ea6\u675f\u5417\uff1f\u4e3a\u4ec0\u4e48\u6d4b\u8bd5\u7ed3\u679c\u4e0d\u662f\u5982\u6b64\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : black11black <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5982\u9898\uff0c\u6700\u8fd1\u7528 cyhton \u5199\u4e86\u4e2a\u52a0\u901f\u63d2\u4ef6\uff0c\u7ed9 python \u4e2d\u7684\u67d0\u4e2a\u6a21\u5757\u52a0\u901f\u4e86\u4e24\u767e\u500d\u5de6\u53f3\uff0c\u6548\u679c\u5341\u5206\u6ee1\u610f\u3002<\/p>\n<p>\u4e8e\u662f\u53c8\u7ed9\u7b97\u6cd5\u590d\u6742\u5ea6\u52a0\u4e86\u5341\u500d\uff0c\u8fd9\u6b21 c \u4e5f\u9876\u4e0d\u4f4f\u4e86\uff0c\u5355\u4e2a\u51fd\u6570\u8c03\u7528\u65f6\u95f4\u4e00\u5206\u949f\u5de6\u53f3\u3002<\/p>\n<p>\u4e8e\u662f\u60f3\u5230\u4e86\u591a\u7ebf\u7a0b\uff0c\u56e0\u4e3a python \u8fdb\u5165 3.6 \u65f6\u4ee3\u540e\u5f02\u6b65\u9010\u6e10\u5b8c\u5584\uff0c\u65e5\u5e38\u91cc\u4f7f\u7528 python \u591a\u7ebf\u7a0b\u7684\u5730\u65b9\u4e3b\u8981\u8fd8\u662f\u5728 IO\uff0c\u901a\u8fc7\u5f02\u6b65 IO \u590d\u7528\u89e3\u51b3\u5f97\u5f88\u5f00\u5fc3\uff0c\u6240\u4ee5\u5f88\u4e45\u6ca1\u6709\u7528\u8fc7\u591a\u7ebf\u7a0b\u6a21\u5757\u4e86\u3002<\/p>\n<p>\u6309\u7167\u6211\u8bb0\u5fc6\u4e2d\u7684\u53ea\u662f\uff0cpython \u7684 GIL \u673a\u5236\u662f\uff0c\u5982\u679c\u4f60\u8c03\u7528 C \u63d2\u4ef6\u7684\u8bdd\u662f\u4f1a\u9ed8\u8ba4\u91ca\u653e GIL \u9501\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u5e94\u8be5\u662f\u539f\u751f\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u5e76\u4e14\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u8c03\u7528 c \u63d2\u4ef6\u7684\u8bdd\uff0c\u5b83\u5e94\u8be5\u662f\u53ef\u4ee5\u5229\u7528\u591a\u6838\u5fc3\u7684\uff0c\u4e8e\u662f\u5199\u4e86\u4e2a\u7b80\u5355\u5c0f\u6d4b\u8bd5\uff0c\u4f46\u662f\u7ed3\u679c\u4e0d\u592a\u5bf9\u3002\u6211\u6700\u7ec8\u7684\u7ed3\u679c\u8fd8\u662f\u987a\u5e8f\u6267\u884c\u7684\u65f6\u95f4\uff0c\u5e76\u6ca1\u6709\u5229\u7528\u591a\u6838\u5fc3\u52a0\u901f\uff0c\u662f\u54ea\u91cc\u505a\u9519\u4e86\u5417\uff0c\u8fd8\u662f\u6211\u8bb0\u9519\u4e86\uff0c\u5e26\u4f6c\u6765\u8bf4\u4e00\u4e0b\u3002<\/p>\n<p>\u7b80\u5355 demo \u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code># timer.py import zhishu import time from threading import Thread  thread_list = [Thread(target = zhishu , args = (150000 , )) for _ in range(8)]  st_time = time.time() for _ in thread_list:     _.start() for _ in thread_list:     _.join() print(time.time() - st_time) <\/code><\/pre>\n<p>\u8fd9\u4e2a\u7b80\u5355 demo \u521b\u5efa\u4e86 8 \u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u8c03\u7528\u4e00\u4e2a\u540d\u53eb zhishu \u7684\u7531 cython \u7f16\u5199\u7684\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u8ba1\u7b97 n \u4ee5\u5185\u8d28\u6570\u6570\u91cf\uff0c\u6211\u672c\u673a\u4e0a\u5355\u6b21\u8c03\u7528\u5927\u6982\u9700\u8981 2s\uff0c\u6211\u6709 8 \u9897\u6838\u5fc3\uff0c\u7406\u8bba\u4e0a\u6211\u671f\u671b\u4ed6\u5728 2s \u5185\u5b8c\u6210\u5168\u90e8\u8ba1\u7b97\uff0c\u4f46\u662f\u6700\u7ec8\u5f97\u5230\u7684\u7ed3\u679c\u5374\u662f 16s<\/p>\n<p>cython \u4ee3\u7801\uff1a \u4e00\u4e2a\u7b80\u5355\u7684\u8d28\u6570\u8ba1\u7b97\u5668<\/p>\n<pre><code># zhishu.pyx import cython  def zhishu(n):     cdef cython.int count = 0     cdef cython.int i , j , nn     nn = &lt;cython.int&gt; n      for i in range(2 , n):         for j in range(2 , i):             if (i%j == 0):                 break         else:             count += 1     return count <\/code><\/pre>\n<p>\u611f\u8c22\uff01<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4496223\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : agegcn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             cython \u6709 nogil \u7684\u9009\u9879\u3002\u56e0\u4e3a cython \u91cc\u4e5f\u4f1a\u6709 python object\uff0c\u6240\u4ee5\u9700\u8981\u624b\u52a8\u91ca\u653e gil \u9501                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4496224\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : westoy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6700\u7b80\u5355\u7684\u65b9\u6848\u5c31\u662f\u6700\u91cc\u9762\u7684 for j \u7684 range \u7528 cython.parallel.prange(&#8230;, nogil=True)\u66ff\u6362\u6389\uff0c \u80fd\u7528\u591a\u6838\uff0c \u4e0d\u8fc7\u6548\u679c\u4e5f\u6709\u9650<\/p>\n<p>\u5efa\u8bae\u5199\u4e2a\u539f\u751f c \u65b9\u6848\uff0c \u7136\u540e\u5957\u4e2a\u51fd\u6570\u5165\u53e3\uff0c \u628a\u53c2\u6570\u8f6c\u6210\u539f\u751f\u7c7b\u578b\u540e\u5957\u5230 with nogil \u91cc\u9762                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4496225\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : black11black <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @westoy <br \/>@agegcn \u6240\u4ee5\u662f cython \u7684\u95ee\u9898\uff0c\u7528 ctypes \u7684\u8bdd\u5c31\u53ef\u4ee5\u76f4\u63a5\u91ca\u653e gil \u4e86\u5417\uff1f\u6211\u8bb0\u5f97\u5f88\u4e45\u4ee5\u524d\u5728\u505a gil \u5b9e\u9a8c\u7684\u65f6\u5019\u7528 c \u5c31\u89e3\u51b3\u4e86\uff0c\u6ca1\u6709\u4ec0\u4e48\u505a\u4e86\u7279\u6b8a\u5904\u7406\u7684\u5370\u8c61\u3002\u5370\u8c61\u4e2d\u5f53\u521d ctypes \u6298\u817e c++stl \u5bb9\u5668\u641e\u4e86\u5f88\u957f\u65f6\u95f4\u6ca1\u641e\u660e\u767d\uff0c\u5c31\u653e\u5f03\u8fd9\u6761\u8def\u7ebf\u4e86\u3002\u73b0\u5728\u76f4\u63a5\u624b\u52a8\u7f16\u8bd1 dll \u7684\u95ee\u9898\u4e5f\u5728\u4e8e ctypes \u6298\u817e stl \u5bb9\u5668\uff0c\u4ee5\u53ca\u628a python \u7684 list \u6620\u5c04\u5230 vector \u4e4b\u7c7b\u7684\u8fd9\u7c7b\u8f93\u5165\u8f93\u51fa\u7684\u95ee\u9898\uff0c\u4e2d\u95f4\u5904\u7406\u5012\u662f\u5b8c\u5168\u53ef\u4ee5\u7528 c++\u91cd\u5199\u3002<\/p>\n<p>\u8bd5\u4e86\u8bd5 with nogil \u7f16\u8bd1\u4e0d\u901a\u8fc7\u554a\uff0c\u4e0d\u77e5\u9053\u95ee\u9898\u5728\u54ea\u91cc\uff0c\u6ca1\u611f\u89c9\u6709\u4ec0\u4e48\u975e C \u5185\u5bb9\u554a\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4496226\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jones2000 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             c \u5185\u90e8\u5f00\u7ebf\u7a0b\u6216\u5e76\u884c\u8ba1\u7b97. \u5916\u90e8 py \u53ea\u505a\u7b80\u5355\u7684\u8c03\u5ea6\u548c\u6570\u636e\u51c6\u5907\u5de5\u4f5c.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4496227\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : helloworld000 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u65e2\u7136\u901f\u5ea6\u771f\u7684\u5f88\u91cd\u8981\uff0c\u5efa\u8bae\u7528 c++\u91cd\u5199\u8fd9\u4e2a module\uff0c\u7136\u540e\u7528 pybind11 \u6765\u7ed9\u5176\u4ed6 python modules \u8c03\u7528                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C \u8bed\u8a00\u63d2\u4ef6\u53ef\u4ee5\u9ed8\u8ba4\u6446\u8131 Pyth&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/223800"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=223800"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/223800\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223800"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}