concurrent.futures模块

1.concurrent.futures模块介绍

2.ThreadPoolExecutor线程池使用

3.ProcessPoolExecutor进程池使用

4.其他方法使用

 

 

1.concurrent.futures模块介绍

 1 # 介绍
 2     concurrent.futures模块提供了高度封装的异步调用接口
 3     ThreadPoolExecutor:线程池,提供异步调用
 4     ProcessPoolExecutor: 进程池,提供异步调用
 5 
 6 # 基本方法
 7 # submit(fn, *args, **kwargs)  异步提交任务
 8 
 9 # map(func, *iterables, timeout=None, chunksize=1)  取代for循环submit的操作
10 
11 # shutdown(wait=True) 相当于进程池的pool.close()+pool.join()操作
12     wait=True,等待池内所有任务执行完毕回收完资源后才继续
13     wait=False,立即返回,并不会等待池内的任务执行完毕
14     但不管wait参数为何值,整个程序都会等到所有任务执行完毕
15     submit和map必须在shutdown之前
16 
17 # result(timeout=None) 取得结果
18 
19 # add_done_callback(fn) 回调函数

 

 

2.ThreadPoolExecutor线程池使用

 1 from concurrent.futures import ThreadPoolExecutor
 2 import time
 3 import random
 4 
 5 def task(n):
 6     print(n)
 7     time.sleep(random.randint(1, 3))
 8     return n ** 2
 9 
10 if __name__ == '__main__':
11     executor = ThreadPoolExecutor(max_workers=5)       # 最多5个线程
12     futures = []
13     for i in range(11):
14         future = executor.submit(task, i)
15         futures.append(future)
16     executor.shutdown(True)
17     print('+++>')
18     for future in futures:
19         print(future.result())

 

 

3.ProcessPoolExecutor进程池使用

 1 from concurrent.futures import ProcessPoolExecutor
 2 import os
 3 import time
 4 import random
 5 
 6 def task(n):
 7     print('%s is runing' % os.getpid())
 8     time.sleep(random.randint(1, 3))
 9     return n ** 2
10 
11 if __name__ == '__main__':
12     executor = ProcessPoolExecutor(max_workers=3)       # 最多3个进程
13     futures = []
14     for i in range(11):
15         future = executor.submit(task, i)
16         futures.append(future)
17     executor.shutdown(True)
18     print('+++>')
19     for future in futures:
20         print(future.result())

 

 

4.其他方法使用

 1 # map方法使用
 2 import time
 3 from concurrent.futures import ThreadPoolExecutor
 4 
 5 def func(n):
 6     time.sleep(2)
 7     print(n)
 8     return n * n
 9 
10 def call_back(m):
11     print('结果是 %s' % m.result())
12 
13 tpool = ThreadPoolExecutor(max_workers=5) 
14 tpool.map(func, range(20))  # 注意map方法拿不到返回值
 1 # 回调函数
 2 import time
 3 from concurrent.futures import ThreadPoolExecutor
 4 
 5 def func(n):
 6     time.sleep(2)
 7     print(n)
 8     return n * n
 9 
10 # 回调函数
11 def call_back(m):
12     print('结果是 %s' % m.result())
13 
14 tpool = ThreadPoolExecutor(max_workers=5)
15 for i in range(20):
16     tpool.submit(func, i).add_done_callback(call_back)

 

posted @ 2018-10-11 15:47  woz333333  阅读(148)  评论(0编辑  收藏  举报