{"id":743,"date":"2017-11-10T14:36:14","date_gmt":"2017-11-10T06:36:14","guid":{"rendered":"http:\/\/www.mrtblog.cn\/?p=743"},"modified":"2023-03-06T14:08:33","modified_gmt":"2023-03-06T06:08:33","slug":"python%e8%a3%85%e9%a5%b0%e5%99%a8%e4%b8%8e%e8%bf%9b%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/www.mrtblog.cn\/?p=743","title":{"rendered":"[Python]\u88c5\u9970\u5668\u4e0e\u8fdb\u7a0b"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,646<\/span><span class='epvc-label'> Views<\/span><\/div><p><strong>1.\u88c5\u9970\u5668<\/strong><\/p>\n<p>\u5982\u679c\u591a\u4e2a\u51fd\u6570\u90fd\u9700\u8981\u6267\u884c\u76f8\u540c\u6216\u7c7b\u4f3c\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u60f3\u8981\u77e5\u9053\u5f53\u524d\u8fd0\u884c\u7684\u51fd\u6570\u540d\uff0c\u8be5\u51fd\u6570\u7684\u8fd0\u884c\u65f6\u95f4\u7b49\uff0c\u88c5\u9970\u5668\u662f\u4e2a\u5f88\u597d\u7684\u529e\u6cd5\u3002<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">#\u88c5\u9970\u5668\uff0c\u4e3a\u6bcf\u4e2a\u51fd\u6570\u589e\u52a0\u6253\u5370\u51fd\u6570\u540d\u548c\u663e\u793a\u8fd0\u884c\u65f6\u95f4\u7684\u529f\u80fd\ndef prt_log(func):\n    def wrapper(*args, **kwargs):\n        print(&quot;Run Function:[%s]... &quot; % func.__name__)\n        e1=time.time()\n        ret=func(*args, **kwargs)\n        e2=time.time()\n        print(&quot;RunTime: %f&quot; % float(e2-e1))\n        return ret\n    return wrapper\n<\/code><\/pre>\n<p>\u8c03\u7528\u65f6\u53ea\u8981\u5728\u51fd\u6570\u5b9a\u4e49\u524d\u8ffd\u52a0\u4e00\u884c<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">@prt_log<\/code><\/pre>\n<p>\u6d4b\u8bd5\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">@prt_log\ndef a():\n    print (&quot;B&quot;)\n\nif __name__==&quot;__main__&quot;:\n    a()<\/code><\/pre>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/11\/py_zsq_jc_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-744\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/11\/py_zsq_jc_1.png\" alt=\"\" width=\"206\" height=\"95\" \/><\/a><\/p>\n<p><strong>2.\u591a\u8fdb\u7a0b<\/strong><\/p>\n<p>\u56e0\u4e3apython\u7684\u5168\u5c40\u9501\uff08GIL\uff09\u4f5c\u7528\uff0c\u591a\u7ebf\u7a0b\u4f3c\u4e4e\u5e76\u4e0d\u80fd\u8d77\u5230\u5f88\u597d\u7684\u6548\u679c\uff0c\u4f46\u662f\u5bf9\u4e8e\u591a\u6838CPU\uff0c\u591a\u8fdb\u7a0b\u8fd8\u662f\u80fd\u6709\u52a0\u901f\u8fd0\u884c\u7684\u4f5c\u7528<br \/>\n\u5c1d\u8bd5\u5bf9\u4e24\u4e2atxt\u6587\u4ef6\u6267\u884c\u591a\u8fdb\u7a0b\u8bfb\u5199\u64cd\u4f5c\u3002src.txt\u662f\u4e00\u4e2a\u670950\u884c\uff0c\u5206\u522b\u662f\u4ece1~50\u6570\u5b57\u7684\u6587\u672c\u3002des\u662f\u7a7a\u7684\u76ee\u6807\u6587\u672c\u3002<br \/>\n\u521b\u5efa10\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u8bfb\u53d6src.txt\u76845\u884c\u6570\u5b57\uff0c\u5bf9\u6bcf\u4e2a\u6570\u5b57*2\u540e\u5199\u5165des.txt\uff0c\u800c\u4e14\u8981\u987a\u5e8f\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">#coding=utf-8\nfrom multiprocessing import Pool\nimport time\nimport Conf\n\ndef writeFile(x):\n    with open(&#039;des.txt&#039;,&#039;a+&#039;) as f:\n        for i in x:\n            f.write(str(int(i)*2)+&quot;\\n&quot;)\n\ndef readFile():\n    a=[]\n    with open(&#039;src.txt&#039;,encoding=&quot;UTF-8&quot;) as f:\n        for line in f:\n            a.append(line)\n    return a\n\ndef readStr(L,id,num):\n    print(id,&quot;-&gt;&quot;,end=&#039; &#039;)\n    length=len(L)\n    each_len=int(length\/num+1)\n    start=id*each_len\n    end=(id+1)*each_len\n\n    if end&gt;=length:\n        return L[start:]\n    else:\n        return L[start:end]\n\nif __name__==&quot;__main__&quot;:\n    L=readFile()\n\n    pool=Pool()\n    Max_Pro_Num=10\n\n    for id in range(Max_Pro_Num):\n        pool.apply_async(readStr,(L,id,Max_Pro_Num,),callback=writeFile)\n\n    pool.close()\n    pool.join()\n    print(&quot;end&quot;)\n<\/code><\/pre>\n<p>\u867d\u7136\u8fdb\u7a0b\u76f8\u4e92\u5408\u4f5c\u5b8c\u6210\u4e58\u6cd5\u64cd\u4f5c\uff0c\u4f46\u662f\u56e0\u4e3a\u6267\u884c\u987a\u5e8f\u548c\u5b8c\u6210\u65f6\u95f4\u7b49\u539f\u56e0\uff0c\u6bcf\u6b21\u8fd0\u884c\u7684\u987a\u5e8f\u4e0d\u4e00\u5b9a\u662f\u6b63\u5e8f\u7684<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/11\/py_zsq_jc_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-746\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/11\/py_zsq_jc_2.png\" alt=\"\" width=\"584\" height=\"79\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/11\/py_zsq_jc_2.png 584w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/11\/py_zsq_jc_2-300x41.png 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><\/p>\n<p>\u6700\u540e\uff0c\u88c5\u9970\u5668\u4f3c\u4e4e\u4e0d\u80fd\u7528\u5728\u8fdb\u7a0b\u4e0a\uff0c\u53c2\u8003<br \/>\n<a href=\"http:\/\/ralph-wang.github.io\/blog\/2015\/02\/15\/zhuang-shi-qi-yu-duo-jin-cheng-yi-ji-pickle\/\">http:\/\/ralph-wang.github.io\/blog\/2015\/02\/15\/zhuang-shi-qi-yu-duo-jin-cheng-yi-ji-pickle\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,646 Views1.\u88c5\u9970\u5668 \u5982\u679c\u591a\u4e2a\u51fd\u6570\u90fd\u9700\u8981\u6267\u884c\u76f8\u540c\u6216\u7c7b\u4f3c\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u60f3\u8981\u77e5\u9053\u5f53\u524d\u8fd0\u884c\u7684\u51fd\u6570\u540d\uff0c\u8be5\u51fd\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[107],"tags":[],"class_list":["post-743","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=743"}],"version-history":[{"count":3,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions"}],"predecessor-version":[{"id":748,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions\/748"}],"wp:attachment":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}