{"id":135666,"date":"2020-07-21T13:39:26","date_gmt":"2020-07-21T05:39:26","guid":{"rendered":"http:\/\/4563.org\/?p=135666"},"modified":"2020-07-21T13:39:26","modified_gmt":"2020-07-21T05:39:26","slug":"%e7%94%a8-threadpoolexecutor-%e6%97%b6%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6-submit-%e5%92%8c-as_completed%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=135666","title":{"rendered":"\u7528 ThreadPoolExecutor \u65f6\u5982\u4f55\u540c\u65f6 submit \u548c as_completed\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7528 ThreadPoolExecutor \u65f6\u5982\u4f55\u540c\u65f6 submit \u548c as_completed\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Te11UA <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u501f\u7528\u5b98\u7f51\u4f8b\u5b50\u8bf4\u660e\u6211\u7684\u7591\u95ee\uff1a<\/p>\n<pre><code>with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:     # Start the load operations and mark each future with its URL     future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}     for future in concurrent.futures.as_completed(future_to_url):         ... <\/code><\/pre>\n<p>\u5b98\u7f51\u7684\u4f8b\u5b50\u90fd\u662f\u8fd9\u6837\uff0c\u5c06\u6240\u6709\u4efb\u52a1\u4e00\u6b21\u6027\u653e\u5230 executor \u540e\u624d\u4f7f\u7528 as_completed \u5224\u65ad\u3002<\/p>\n<p>\u5982\u679c\u6211\u9700\u8981\u4ece\u961f\u5217\u4e2d\u4e0d\u65ad\u53d6\u4efb\u52a1\u7684\u8bdd\uff0c\u8be5\u600e\u4e48\u4f7f\u7528 as_completed \u6bd4\u8f83\u5408\u9002\u5462\uff1f<\/p>\n<pre><code># \u7c7b\u4f3c\u4e8e\u8fd9\u4e2a\u610f\u601d while True:      executor.submit(load_url)      time.sleep(1)  while True:     for future in concurrent.futures.as_completed():         #do sth... <\/code><\/pre>\n<p>\u4f7f\u7528\u4e24\u4e2a\u7ebf\u7a0b\uff1f\u611f\u89c9\u5728\u5957\u5a03\u2026\u2026<\/p>\n<p>\u6216\u8005\u8bf4 ThreadPoolExecutor \u4e0d\u9002\u7528\u4e8e\u8be5\u573a\u666f\uff0c\u9700\u8981\u81ea\u5df1\u5199 threading \u65b9\u6cd5?<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2468023\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             executor.submit \u4f1a\u751f\u4ea7\u4e00\u4e2a\u5728\u8fd0\u884c\u7684 Future<\/p>\n<p>as_completed(futures) \u5c31\u662f\u4e00\u4e2a\u751f\u6210\u5668, \u6309\u7167 futures \u5217\u8868\u5b8c\u6210\u7684\u987a\u5e8f\u5f80\u5916\u5410\u7ed3\u679c, \u6240\u4ee5\u524d\u63d0\u662f\u5148\u6709\u8fd9\u4e2a futures list \u624d\u80fd\u6309\u987a\u5e8f\u5f80\u5916\u6458<\/p>\n<p>\u6240\u4ee5, \u7406\u8bba\u4e0a\u6765\u8bf4, \u4f60\u90a3\u4e2a\u60c5\u666f\u7c7b\u4f3c\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u4e86, \u8be5\u8d70\u7684\u662f\u961f\u5217\u800c\u4e0d\u662f as_completed                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2468024\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : laike9m <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e24\u4e2a\u7ebf\u7a0b\u6ca1\u5565\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 ThreadPoolExecu&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\/135666"}],"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=135666"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/135666\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=135666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=135666"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=135666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}