{"id":1583,"date":"2023-03-25T11:33:17","date_gmt":"2023-03-25T03:33:17","guid":{"rendered":""},"modified":"2023-03-25T11:33:17","modified_gmt":"2023-03-25T03:33:17","slug":"Python\u7ebf\u7a0b\u6c60","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1583.html","title":{"rendered":"Python\u7ebf\u7a0b\u6c60"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python\u7ebf\u7a0b\u6c60<\/h1>\n

Python\u7ebf\u7a0b\u6c60\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5047\u8bbe\u6211\u4eec\u5fc5\u987b\u4e3a\u591a\u7ebf\u7a0b\u4efb\u52a1\u521b\u5efa\u5927\u91cf\u7ebf\u7a0b\u3002\u7531\u4e8e\u7ebf\u7a0b\u8fc7\u591a\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u8bb8\u591a\u6027\u80fd\u95ee\u9898\uff0c\u56e0\u6b64\u8fd9\u5c06\u662f\u8ba1\u7b97\u4e0a\u6700\u6602\u8d35\u7684\u3002\u4e00\u4e2a\u4e3b\u8981\u95ee\u9898\u53ef\u80fd\u662f\u541e\u5410\u91cf\u53d7\u5230\u9650\u5236\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u7ebf\u7a0b\u6c60\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u5b9a\u4e49\u4e3a\u4e00\u7ec4\u9884\u521d\u59cb\u5316\u548c\u7a7a\u95f2\u7684\u7ebf\u7a0b\uff0c\u5b83\u4eec\u968f\u65f6\u53ef\u4ee5\u5de5\u4f5c\u3002\u5f53\u6211\u4eec\u9700\u8981\u6267\u884c\u5927\u91cf\u4efb\u52a1\u65f6\uff0c\u4e0e\u4e3a\u6bcf\u4e2a\u4efb\u52a1\u5b9e\u4f8b\u5316\u65b0\u7ebf\u7a0b\u76f8\u6bd4\uff0c\u521b\u5efa\u7ebf\u7a0b\u6c60\u66f4\u4e3a\u53ef\u53d6\u3002\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u7ba1\u7406\u5927\u91cf\u7ebf\u7a0b\u7684\u5e76\u53d1\u6267\u884c\uff0c\u5982\u4e0b\u6240\u793a\uff1a-\n <\/div>\n

\u5982\u679c\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u5b8c\u6210\u5176\u6267\u884c\uff0c\u5219\u53ef\u4ee5\u91cd\u7528\u8be5\u7ebf\u7a0b\u3002<\/span>
\n \u5982\u679c\u67d0\u4e2a\u7ebf\u7a0b\u7ec8\u6b62\uff0c\u5219\u5c06\u521b\u5efa\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6765\u66ff\u6362\u8be5\u7ebf\u7a0b\u3002<\/span> <\/p>\n

Python\u6a21\u5757\u2013 Concurrent.futures <\/h2>\n
\n Python\u6807\u51c6\u5e93\u5305\u542b
\n concurrent.futures <\/b>\u6a21\u5757\u3002\u5728Python 3.2\u4e2d\u6dfb\u52a0\u4e86\u6b64\u6a21\u5757\uff0c\u4ee5\u4e3a\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u542f\u52a8\u5f02\u6b65\u4efb\u52a1\u7684\u9ad8\u7ea7\u63a5\u53e3\u3002\u8fd9\u662fPython\u7ebf\u7a0b\u548c\u591a\u5904\u7406\u6a21\u5757\u4e4b\u4e0a\u7684\u62bd\u8c61\u5c42\uff0c\u7528\u4e8e\u63d0\u4f9b\u4f7f\u7528\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u6c60\u8fd0\u884c\u4efb\u52a1\u7684\u63a5\u53e3\u3002\n <\/div>\n
\n \u5728\u6211\u4eec\u968f\u540e\u7684\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5e76\u53d1\u6a21\u5757\u7684\u4e0d\u540c\u7c7b\u3002\n <\/div>\n

Executor\u7c7b<\/h2>\n
\n Executor<\/b>\u662f
\n concurrent.futures <\/b> Python\u6a21\u5757\u7684\u62bd\u8c61\u7c7b\u3002\u5b83\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b\u5177\u4f53\u5b50\u7c7b\u4e4b\u4e00-\n <\/div>\n

ThreadPoolExecutor <\/span>
\n ProcessPoolExecutor <\/span> <\/p>\n

ThreadPoolExecutor \u2013\u4e00\u4e2a\u5177\u4f53\u7684\u5b50\u7c7b<\/h3>\n
\n \u5b83\u662fExecutor\u7c7b\u7684\u5177\u4f53\u5b50\u7c7b\u4e4b\u4e00\u3002\u5b50\u7c7b\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u5e76\u4e14\u6211\u4eec\u83b7\u5f97\u4e86\u7528\u4e8e\u63d0\u4ea4\u4efb\u52a1\u7684\u7ebf\u7a0b\u6c60\u3002\u8be5\u6c60\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u53ef\u7528\u7ebf\u7a0b\uff0c\u5e76\u5b89\u6392\u5b83\u4eec\u8fd0\u884c\u3002\n <\/div>\n

\u5982\u4f55\u521b\u5efaThreadPoolExecutor\uff1f<\/h3>\n
\n \u501f\u52a9\u4e8e
\n concurrent.futures <\/b>\u6a21\u5757\u53ca\u5176\u5177\u4f53\u7684\u5b50\u7c7b
\n Executor <\/b>\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u7ebf\u7a0b\u6c60\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6784\u9020\u4e00\u4e2a
\n ThreadPoolExecutor <\/b>\uff0c\u5176\u4e2d\u5305\u542b\u6211\u4eec\u8981\u5728\u6c60\u4e2d\u4f7f\u7528\u7684\u7ebf\u7a0b\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8be5\u6570\u5b57\u4e3a5\u3002\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u52a1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\u3002\u5f53\u6211\u4eec
\n submit()<\/b>\u4e00\u4e2a\u4efb\u52a1\u65f6\uff0c\u6211\u4eec\u5f97\u5230\u4e00\u4e2a
\n Future <\/b>\u3002 Future\u5bf9\u8c61\u5177\u6709\u4e00\u4e2a\u79f0\u4e3a
\n done()<\/b>\u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u6307\u793a\u672a\u6765\u662f\u5426\u5df2\u89e3\u51b3\u3002\u8fd9\u6837\uff0c\u5df2\u4e3a\u8be5\u7279\u5b9a\u5c06\u6765\u7684\u5bf9\u8c61\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u503c\u3002\u4efb\u52a1\u5b8c\u6210\u540e\uff0c\u7ebf\u7a0b\u6c60\u6267\u884c\u7a0b\u5e8f\u5c06\u503c\u8bbe\u7f6e\u4e3a\u5c06\u6765\u7684\u5bf9\u8c61\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
from <\/span>concurrent.futures import <\/span>ThreadPoolExecutor
from <\/span>time import <\/span>sleep
def <\/span>task(message):
   sleep(2)
   return <\/span>message
def <\/span>main():
   executor <\/span>= ThreadPoolExecutor(5)
   future = executor.submit<\/span>(task, (\"Completed\"<\/span>))
   print(future.done<\/span>())
   sleep(2)
   print(future.done<\/span>())
   print(future.result<\/span>())
if <\/span>__name__ == '__main__'<\/span>:
main()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
False<\/span>
True
Completed
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5df2\u4f7f\u75285\u4e2a\u7ebf\u7a0b\u6784\u9020\u4e86
\n ThreadPoolExecutor <\/b>\u3002\u7136\u540e\uff0c\u5c06\u5728\u53d1\u51fa\u6d88\u606f\u4e4b\u524d\u7b49\u5f852\u79d2\u949f\u7684\u4efb\u52a1\u88ab\u63d0\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u6267\u884c\u7a0b\u5e8f\u3002\u4ece\u8f93\u51fa\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u8be5\u4efb\u52a1\u8981\u7b49\u52302\u79d2\u540e\u624d\u80fd\u5b8c\u6210\uff0c\u56e0\u6b64\u7b2c\u4e00\u6b21\u8c03\u7528
\n done()<\/b>\u5c06\u8fd4\u56deFalse\u3002 2\u79d2\u540e\uff0c\u4efb\u52a1\u5b8c\u6210\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528
\n result()<\/b>\u65b9\u6cd5\u83b7\u5f97\u672a\u6765\u7684\u7ed3\u679c\u3002\n <\/div>\n

\u5b9e\u4f8b\u5316ThreadPoolExecutor \u2013\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n
\n \u5b9e\u4f8b\u5316
\n ThreadPoolExecutor <\/b>\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u5e2e\u52a9\u4e0b\u3002\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u4ee5\u4e0a\u793a\u4f8b\u4e2d\u4f7f\u7528\u7684\u65b9\u6cd5\u7c7b\u4f3c\u3002\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5b83\u5728\u8bed\u6cd5\u4e0a\u770b\u8d77\u6765\u4e0d\u9519\u3002\u53ef\u4ee5\u5728\u4ee5\u4e0b\u4ee3\u7801\u7684\u5e2e\u52a9\u4e0b\u5b8c\u6210\u5b9e\u4f8b\u5316-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
with <\/span>ThreadPoolExecutor(max_workers = 5) as <\/span>executor
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0b\u793a\u4f8b\u662f\u4ecePython\u6587\u6863\u4e2d\u501f\u6765\u7684\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u5fc5\u987b\u5bfc\u5165
\n concurrent.futures <\/b>\u6a21\u5757\u3002\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a
\n load_url()<\/b>\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u52a0\u8f7d\u8bf7\u6c42\u7684URL\u3002\u7136\u540e\uff0c\u8be5\u51fd\u6570\u4f7f\u7528\u6c60\u4e2d\u76845\u4e2a\u7ebf\u7a0b\u521b\u5efa
\n ThreadPoolExecutor <\/b>\u3002
\n ThreadPoolExecutor <\/b>\u5df2\u88ab\u7528\u4f5c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528
\n result()<\/b>\u65b9\u6cd5\u83b7\u5f97\u672a\u6765\u7684\u7ed3\u679c\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>concurrent.futures
import <\/span>urllib.request
URLS = ['http:\/\/www.foxnews.com\/'<\/span>,
   'http:\/\/www.cnn.com\/'<\/span>,
   'http:\/\/europe.wsj.com\/'<\/span>,
   'http:\/\/www.bbc.co.uk\/'<\/span>,
   'http:\/\/some-made-up-domain.com\/'<\/span>]
def <\/span>load_url(url, timeout):
   with <\/span>urllib.request.urlopen<\/span>(url, timeout = timeout) as <\/span>conn:
   return <\/span>conn.read<\/span>()
with <\/span>concurrent.futures.ThreadPoolExecutor<\/span>(max_workers = 5) as <\/span>executor:
   future_to_url = {executor.submit<\/span>(load_url, url, 60): url for <\/span><\/span>url in <\/span>URLS}
   for <\/span><\/span>future in <\/span>concurrent.futures.as_completed<\/span>(future_to_url):
   url = future_to_url[future]
   try:
      data = future.result<\/span>()
   except <\/span>Exception as <\/span>exc:
      print('%r generated an exception: %s'<\/span> % (url, exc))
   else:<\/span>
      print('%r page is <\/span>%d bytes'<\/span> % (url, len(data)))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n \u4ee5\u4e0b\u5c06\u662f\u4e0a\u8ff0Python\u811a\u672c\u7684\u8f93\u51fa-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
'http:\/\/some-made-up-domain.com\/'<\/span> generated an exception: <urlopen error <\/span>[Errno 11004] getaddrinfo failed>
'http:\/\/www.foxnews.com\/'<\/span> page is <\/span>229313 bytes
'http:\/\/www.cnn.com\/'<\/span> page is <\/span>168933 bytes
'http:\/\/www.bbc.co.uk\/'<\/span> page is <\/span>283893 bytes
'http:\/\/europe.wsj.com\/'<\/span> page is <\/span>938109 bytes
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u4f7f\u7528Executor.map()\u51fd\u6570<\/h3>\n
\n Python
\n map()<\/b>\u51fd\u6570\u88ab\u5e7f\u6cdb\u7528\u4e8e\u8bb8\u591a\u4efb\u52a1\u3002\u8fd9\u6837\u7684\u4efb\u52a1\u4e4b\u4e00\u662f\u5c06\u7279\u5b9a\u529f\u80fd\u5e94\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fed\u4ee3\u5668\u7684\u6240\u6709\u5143\u7d20\u6620\u5c04\u5230\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a\u72ec\u7acb\u7684\u4f5c\u4e1a\u63d0\u4ea4\u7ed9
\n ThreadPoolExecutor <\/b>\u3002\u8003\u8651\u4e0b\u9762\u7684Python\u811a\u672c\u793a\u4f8b\uff0c\u4ee5\u4e86\u89e3\u8be5\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0cmap\u51fd\u6570\u7528\u4e8e\u5c06
\n square()<\/b>\u51fd\u6570\u5e94\u7528\u4e8evalues\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u503c\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
from <\/span>concurrent.futures import <\/span>ThreadPoolExecutor
from <\/span>concurrent.futures import <\/span>as_completed
values = [2,3,4,5]
def <\/span>square(n):
   return <\/span>n * n
def <\/span>main():
   with <\/span>ThreadPoolExecutor(max_workers = 3) as <\/span>executor:
      results = executor.map<\/span>(square, values)
for <\/span><\/span>result in <\/span>results:
      print(result)
if <\/span>__name__ == '__main__'<\/span>:
   main()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n \u4e0a\u9762\u7684Python\u811a\u672c\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
4
9
16
25
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ebf\u7a0b\u6c60zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[147],"tags":[],"class_list":["post-1583","post","type-post","status-publish","format-standard","hentry","category-python-dxc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1583"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1583"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1583\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}