Python之concurrent.futures

concurrent.futures

ProcessPoolExecutor

进程池,提供异步调用

基本方法
  • submit(fn, args, *kwargs) 异步提交任务

  • map(func, *iterables, timeout=None, chunksize=1) 取代for循环submit的操作

  • shutdown(wait=True) 相当于进程池的pool.close()+pool.join()操作

    wait=True,等待池内所有任务执行完毕回收完资源后才继续
    wait=False,立即返回,并不会等待池内的任务执行完毕
    但不管wait参数为何值,整个程序都会等到所有任务执行完毕
    submit和map必须在shutdown之前

  • result(timeout=None)取得结果

  • add_done_callback(fn)回调函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

import os, time, random


def task(n):
print('%s is runing' % os.getpid())
time.sleep(random.randint(1, 3))
return n ** 2


if __name__ == '__main__':

executor = ProcessPoolExecutor(max_workers=3)

futures = []
for i in range(11):
future = executor.submit(task, i)
futures.append(future)
executor.shutdown(True)
print('+++>')
for future in futures:
print(future.result())

ThreadPoolExecutor

线程池,提供异步调用,用法与ProcessPoolExecutor相同,只需要将ProcessPoolExecutor换成ThreadPoolExecutor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import time
from concurrent.futures import ThreadPoolExecutor


def func(n):
time.sleep(2)
print(n)
return n * n


def call_back(m):
print('结果是 %s' % m.result())


if __name__ == '__main__':
tpool = ThreadPoolExecutor(max_workers=5) # 默认 不要超过cpu个数*5
for i in range(20):
tpool.submit(func, i).add_done_callback(call_back)
-------------本文结束感谢您的阅读-------------

本文标题:Python之concurrent.futures

文章作者:GavinLiu

发布时间:2018年03月08日 - 23:03

最后更新:2018年03月08日 - 23:03

原始链接:http://gavinliu4011.github.io/post/b1bc006d.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

请博主吃个鸡腿吧
0%