{"id":181091,"date":"2020-05-14T10:32:11","date_gmt":"2020-05-14T02:32:11","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=181091"},"modified":"2020-05-02T17:34:34","modified_gmt":"2020-05-02T09:34:34","slug":"python-multi-threading","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-multi-threading.html","title":{"rendered":"\u521d\u5b66\u8005\u770b\u8fc7\u6765\uff1aPython\u4e2d\u591a\u7ebf\u7a0b\u548c\u591a\u5904\u7406\u7684\u6307\u5357"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4f7f\u7528Python\u5206\u6790\u6570\u636e\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u6b63\u786e\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u6709\u65f6\u53ef\u4ee5\u5927\u91cf\u63d0\u9ad8\u7a0b\u5e8f\u7684\u901f\u5ea6\u3002\u5b9e\u73b0\u6b64\u76ee\u7684\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528Muiltithreading(\u591a\u7ebf\u7a0b)\u6216Multiprocessing(\u591a\u91cd\u5904\u7406)\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4f7f\u7528Python\u5206\u6790\u6570\u636e\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u6b63\u786e\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u6709\u65f6\u53ef\u4ee5\u5927\u91cf\u63d0\u9ad8\u7a0b\u5e8f\u7684\u901f\u5ea6\u3002\u5b9e\u73b0\u6b64\u76ee\u7684\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528Muiltithreading(\u591a\u7ebf\u7a0b)\u6216Multiprocessing(\u591a\u91cd\u5904\u7406)\u3002<\/p>\n

\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4e0d\u4f1a\u8be6\u7ec6\u8ba8\u8bba\u591a\u7ebf\u7a0b\u6216\u591a\u5904\u7406\u7684\u5185\u90e8\u539f\u7406\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7f16\u5199\u4e00\u4e2a\u5c0f\u7684Python\u811a\u672c\u4eceUnsplash\u4e0b\u8f7d\u56fe\u50cf\u3002\u6211\u4eec\u5c06\u4ece\u4e00\u6b21\u4e0b\u8f7d\u4e00\u4e2a\u56fe\u50cf\u7684\u7248\u672c\u5f00\u59cb\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u7ebf\u7a0b\u6765\u63d0\u9ad8\u6267\u884c\u901f\u5ea6\u3002<\/p>\n

\"\"<\/p>\n

Python\u4e2d\u591a\u7ebf\u7a0b\u548c\u591a\u5904\u7406\u7684\u521d\u5b66\u8005\u6307\u5357<\/p>\n

\u591a\u7ebf\u7a0b<\/strong><\/div>\n

\u7b80\u5355\u5730\u8bf4\uff0c\u7ebf\u7a0b\u5141\u8bb8\u60a8\u5e76\u884c\u5730\u8fd0\u884c\u7a0b\u5e8f\u3002\u82b1\u8d39\u5927\u91cf\u65f6\u95f4\u7b49\u5f85\u5916\u90e8\u4e8b\u4ef6\u7684\u4efb\u52a1\u901a\u5e38\u9002\u5408\u7ebf\u7a0b\u5316\u3002\u5b83\u4eec\u4e5f\u79f0\u4e3aI\/O Bound\u4efb\u52a1\u4f8b\u5982\u4ece\u6587\u4ef6\u4e2d\u8bfb\u5199\uff0c\u7f51\u7edc\u64cd\u4f5c\u6216\u4f7f\u7528API\u5728\u7ebf\u4e0b\u8f7d\u3002\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u793a\u4f8b\uff0c\u5b83\u5c55\u793a\u4e86\u4f7f\u7528\u7ebf\u7a0b\u7684\u597d\u5904\u3002<\/p>\n

1. \u6ca1\u6709\u7ebf\u7a0b<\/strong><\/span><\/div>\n

\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u901a\u8fc7\u987a\u5e8f\u8fd0\u884c\u7a0b\u5e8f\u6765\u67e5\u770b\u4eceUnsplash API\u4e0b\u8f7d15\u5f20\u56fe\u50cf\u9700\u8981\u591a\u957f\u65f6\u95f4\uff1a<\/p>\n

import requests \r\nimport time \r\nimg_urls = [ \r\n    'https:\/\/images.unsplash.com\/photo-1516117172878-fd2c41f4a759', \r\n    'https:\/\/images.unsplash.com\/photo-1532009324734-20a7a5813719', \r\n    'https:\/\/images.unsplash.com\/photo-1524429656589-6633a470097c', \r\n    'https:\/\/images.unsplash.com\/photo-1530224264768-7ff8c1789d79', \r\n    'https:\/\/images.unsplash.com\/photo-1564135624576-c5c88640f235', \r\n    'https:\/\/images.unsplash.com\/photo-1541698444083-023c97d3f4b6', \r\n    'https:\/\/images.unsplash.com\/photo-1522364723953-452d3431c267', \r\n    'https:\/\/images.unsplash.com\/photo-1513938709626-033611b8cc03', \r\n    'https:\/\/images.unsplash.com\/photo-1507143550189-fed454f93097', \r\n    'https:\/\/images.unsplash.com\/photo-1493976040374-85c8e12f0c0e', \r\n    'https:\/\/images.unsplash.com\/photo-1504198453319-5ce911bafcde', \r\n    'https:\/\/images.unsplash.com\/photo-1530122037265-a5f1f91d3b99', \r\n    'https:\/\/images.unsplash.com\/photo-1516972810927-80185027ca84', \r\n    'https:\/\/images.unsplash.com\/photo-1550439062-609e1531270e', \r\n    'https:\/\/images.unsplash.com\/photo-1549692520-acc6669e2f0c' \r\n] \r\n \r\nstart = time.perf_counter() #start timer \r\nfor img_url in img_urls: \r\n    img_name = img_url.split('\/')[3] #get image name from url \r\n    img_bytes = requests.get(img_url).content \r\nwith open(img_name, 'wb') as img_file: \r\n     img_file.write(img_bytes) #save image to disk  \r\n \r\nfinish = time.perf_counter() #end timer \r\nprint(f\"Finished in {round(finish-start,2)} seconds\")  \r\n \r\n#results \r\nFinished in 23.101926751 seconds \r\n<\/pre>\n

\u4e00\u5171\u7528\u65f6\u200b23\u79d2\u3002<\/p>\n

2. \u591a\u7ebf\u7a0b<\/strong><\/span><\/div>\n

\u8ba9\u6211\u4eec\u770b\u770bPyhton\u4e2d\u7684\u7ebf\u7a0b\u6a21\u5757\u5982\u4f55\u663e\u8457\u5730\u6539\u8fdb\u6211\u4eec\u7684\u7a0b\u5e8f\u6267\u884c\uff1a<\/p>\n

import time \r\nfrom concurrent.futures import ThreadPoolExecutor \r\n \r\ndef download_images(url): \r\n    img_name = img_url.split('\/')[3] \r\n    img_bytes = requests.get(img_url).content \r\n    with open(img_name, 'wb') as img_file: \r\n         img_file.write(img_bytes) \r\n         print(f\"{img_name} was downloaded\") \r\n \r\nstart = time.perf_counter() #start timer \r\nwith ThreadPoolExecutor() as executor: \r\n    results = executor.map(download_images,img_urls) #this is Similar to map(func, *iterables) \r\nfinish = time.perf_counter() #end timer \r\nprint(f\"Finished in {round(finish-start,2)} seconds\") \r\n \r\n#results  \r\nFinished in 5.544147536 seconds\r\n<\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4e0e\u4e0d\u4f7f\u7528\u7ebf\u7a0b\u4ee3\u7801\u76f8\u6bd4\uff0c\u4f7f\u7528\u7ebf\u7a0b\u4ee3\u7801\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u901f\u5ea6\u3002\u4ece23\u79d2\u52305\u79d2\u3002<\/p>\n

\u5bf9\u4e8e\u672c\u4f8b\uff0c\u8bf7\u6ce8\u610f\u5728\u521b\u5efa\u7ebf\u7a0b\u65f6\u5b58\u5728\u5f00\u9500\uff0c\u56e0\u6b64\u5c06\u7ebf\u7a0b\u7528\u4e8e\u591a\u4e2aAPI\u8c03\u7528\u662f\u6709\u610f\u4e49\u7684\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5355\u4e2a\u8c03\u7528\u3002<\/p>\n

\u6b64\u5916\uff0c\u5bf9\u4e8e\u5bc6\u96c6\u7684\u8ba1\u7b97\uff0c\u5982\u6570\u636e\u5904\u7406\uff0c\u56fe\u50cf\u5904\u7406\u591a\u5904\u7406\u6bd4\u7ebf\u7a0b\u6267\u884c\u5f97\u66f4\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4f7f\u7528Python\u5206\u6790\u6570\u636e\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u6b63\u786e\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u6709\u65f6\u53ef\u4ee5\u5927\u91cf\u63d0\u9ad8\u7a0b\u5e8f\u7684\u901f\u5ea6\u3002\u5b9e\u73b0\u6b64\u76ee\u7684\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f […]<\/p>\n","protected":false},"author":1898,"featured_media":181093,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[553],"class_list":["post-181091","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/181091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=181091"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/181091\/revisions"}],"predecessor-version":[{"id":181094,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/181091\/revisions\/181094"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/181093"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=181091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=181091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=181091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}