Python thread pool usage analysis module ThreadPoolExecutor

This article describes the Python module ThreadPoolExecutor thread pool usage, analysis of the Python thread pool module ThreadPoolExecutor import and use basic form with an example, a friend in need can refer to the
article about the example Python module ThreadPoolExecutor thread pool usage. Share to you for your reference, as follows:

python3 built there Threadingpool and ThreadPoolExecutor modules, both can do the thread pool, of course, would be better to use some ThreadPoolExecutor, but also ProcessPoolExecutor process pool module, use basically the same.

First import module

from concurrent.futures import ThreadPoolExecutor

Very simple to use, the most commonly used method is probably the map and submit + as_completed

Note, be sure to use with, rather than for, if you must use for, then it must be done manually executor.shutdown, and you use with the method, then inside again with the method has been achieved wait(), after after use can close the thread pool reduce waste of resources.

Use map

with ThreadPoolExecutor(max_workers=2) as executor:
  result = executor.map(map_fun, itr_arg)
  '''map_fun:你传入的要执行的map函数
    itr_arg:一个可迭代的参数,可以是列表字典等可迭代的对象
    基本上和python的map函数一样
    注意result并不是你map_fun返回的结果,而是一个生成器,如果要从中去结果,你可以使用列表生成式或者其他你想使用的方法
  '''
  for res in result:
    print(res) #这个res就是你map_fun返回的结果,你可以在这里做进一步处理

Use submit + as_completed can also be very flexible

with ThreadPoolExecutor(max_workers=2) as executor:
  future= executor.submit(fun, args)
  '''
  在这里你可以使用for循环来做,返回的是一个future对象
  future_list=[]
  for i in range(max_workers):
    future= executor.submit(fun, args[i])
    future_list.append(future)
  '''
  for res in ac_completed(futrue_list): #这个futrure_list是你future对象的列表
    print(res.result())        #循环遍历时用.result()来取返回值

Almost, can be well implemented in two ways multi-threaded task, we must remember to use with!
the recommended our learning Python buckle qun: 774711191, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering

发布了35 篇原创文章 · 获赞 9 · 访问量 1万+

Guess you like

Origin blog.csdn.net/haoxun02/article/details/104254423