{"id":40,"date":"2020-07-20T10:40:03","date_gmt":"2020-07-20T04:40:03","guid":{"rendered":"http:\/\/www.dreamflier.net\/wordpress\/?p=40"},"modified":"2020-07-20T10:40:03","modified_gmt":"2020-07-20T04:40:03","slug":"%e8%bf%99%e4%b8%aapython%e4%be%8b%e5%ad%90-%e7%ae%80%e5%8d%95%e5%bd%a2%e8%b1%a1%e7%9a%84%e6%bc%94%e7%a4%ba%e4%ba%86%e5%8d%95%e7%ba%bf%e7%a8%8b%e5%92%8cmap%e5%ae%9e%e7%8e%b0%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/www.jichangqing.net\/wordpress\/?p=40","title":{"rendered":"\u8fd9\u4e2aPython\u4f8b\u5b50 \u7b80\u5355\u5f62\u8c61\u7684\u6f14\u793a\u4e86\u5355\u7ebf\u7a0b\u548cMap\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u65f6\u95f4\u505a\u4e86\u4e00\u4e2a\u5bf9\u6bd4"},"content":{"rendered":"<div style=\"color: #000000;\">\n<div><span style=\"color: #0000ff;\">import<\/span> time<\/div>\n<div><span style=\"color: #0000ff;\">from<\/span> multiprocessing <span style=\"color: #0000ff;\">import<\/span> Pool<\/div>\n<div><span style=\"color: #0000ff;\">import<\/span> requests<\/div>\n<div><span style=\"color: #0000ff;\">def<\/span> decorator_timer(old_function):<\/div>\n<div><span style=\"color: #0000ff;\">def<\/span> new_function(*arg, **dict_arg):<\/div>\n<div>t1 = time.time()<\/div>\n<div>result = old_function(*arg, **dict_arg)<\/div>\n<div>t2 = time.time()<\/div>\n<div><span style=\"color: #0000ff;\">print<\/span>(<span style=\"color: #ff69b4;\">&#8220;<\/span><span style=\"color: #a31515;\">time:<\/span><span style=\"color: #ff69b4;\">&#8220;<\/span>, t2 &#8211; t1)<\/div>\n<div><span style=\"color: #0000ff;\">return<\/span> result<\/div>\n<div><span style=\"color: #0000ff;\">return<\/span> new_function<\/div>\n<div><span style=\"color: #0000ff;\">def<\/span> visit_once(i, address = <span style=\"color: #ff69b4;\">&#8220;<\/span><span style=\"color: #a31515;\">http:\/\/www.cnblogs.com<\/span><span style=\"color: #ff69b4;\">&#8220;<\/span>):<\/div>\n<div>r = requests.get(address)<\/div>\n<div><span style=\"color: #0000ff;\">return<\/span> r.status_code<\/div>\n<div><span style=\"color: #800000;\">@decorator_timer<\/span><\/div>\n<div><span style=\"color: #0000ff;\">def<\/span> single_thread(f, counts):<\/div>\n<div>result = <span style=\"color: #0000ff;\">map<\/span>(f, <span style=\"color: #0000ff;\">range<\/span>(counts))<\/div>\n<div><span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #0000ff;\">list<\/span>(result)<\/div>\n<div><span style=\"color: #800000;\">@decorator_timer<\/span><\/div>\n<div><span style=\"color: #0000ff;\">def<\/span> multiple_thread(f, counts, process_number = <span style=\"color: #09885a;\">10<\/span>):<\/div>\n<div>p = Pool(process_number)<\/div>\n<div><span style=\"color: #0000ff;\">return<\/span> p.<span style=\"color: #0000ff;\">map<\/span>(f, <span style=\"color: #0000ff;\">range<\/span>(counts))<\/div>\n<div><span style=\"color: #0000ff;\">if<\/span> __name__ == <span style=\"color: #ff69b4;\">&#8220;<\/span><span style=\"color: #a31515;\">__main__<\/span><span style=\"color: #ff69b4;\">&#8220;<\/span>:<\/div>\n<div>TOTAL = <span style=\"color: #09885a;\">100<\/span><\/div>\n<div><span style=\"color: #0000ff;\">print<\/span>(<span style=\"color: #ff69b4;\">&#8220;<\/span><span style=\"color: #a31515;\">single<\/span><span style=\"color: #ff69b4;\">&#8220;<\/span>)<\/div>\n<div><span style=\"color: #0000ff;\">print<\/span>(single_thread(visit_once, TOTAL))<\/div>\n<div><span style=\"color: #0000ff;\">print<\/span>(<span style=\"color: #ff69b4;\">&#8220;<\/span><span style=\"color: #a31515;\">multle<\/span><span style=\"color: #ff69b4;\">&#8220;<\/span>)<\/div>\n<div><span style=\"color: #0000ff;\">print<\/span>(multiple_thread(visit_once, TOTAL))<\/div>\n<div><\/div>\n<div>\u542f\u52a810\u4e2a\u8fdb\u7a0b, \u5e76\u884c\u5904\u7406100\u4e2a\u7f51\u7ad9\u4e0b\u8f7d\u8bf7\u6c42, \u5355\u4e2a\u4e0b\u8f7d\u8fdb\u7a0b\u63cf\u8ff0\u4e3avisit_once() \u591a\u7ebf\u7a0b\u7528\u4e86map\u65b9\u6cd5. \u65f6\u95f4\u5927\u4e3a\u7f29\u77ed.<br \/>\n\u5f15\u81ea \u300a\u4ecePython\u5f00\u59cb\u5b66\u7f16\u7a0b\u300b \u8fd9\u672c\u5165\u95e8\u4e66\u5199\u7684\u4e0d\u9519, \u53ef\u60dc\u4f5c\u8005\u82f1\u5e74\u65e9\u901d. \u53ef\u89c1\u5fc3\u7406\u5065\u5eb7\u591a\u4e48\u91cd\u8981, \u5c24\u5176\u5728\u5f53\u524d\u75ab\u60c5\u4e0b.<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>import time from multiprocessing import Pool import requests def decorator_timer(old_function): def new_function(*arg, * &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=40"}],"version-history":[{"count":1,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":41,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions\/41"}],"wp:attachment":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}