{"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":"
\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 |