Python并发编程---concurrent.futures

Python并发编程—concurrent.futures

Executor

concurrent.futures.Executor: 一个抽象类,提供了异步执行调用的方法。一般都是通过调用子类来使用。

方法

submit(fn, *args, **kwargs):执行可调用对象,返回一个Future对象表示执行的可调用对象

map(func, *iterables, timeout=None, chunksize=1):使用时ProcessPoolExecutor,此方法将iterables切割 为多个块,并将其作为单独的任务提交给池。chunksize设置分块大小。使用时ThreadPoolExecutor时,chunksize无效

shutdown(wait=True):告executor执行者执行完毕,释放资源

ThreadPoolExecutor

ThreadPoolExecutor 是一个 Executor 子类,它使用一个线程池来异步执行调用。

concurrent.futures.ThreadPoolExecutor(max_workers=None, thread_name_prefix=’’)
参数

max_workers: 设置线程数量
thread_name_prefix: 以允许用户控制由池创建的工作线程的线程名

ProcessPoolExecutor

ProcessPoolExecutor 类是一个 Executor 子类,它使用进程池来异步执行调用

concurrent.futures.ProcessPoolExecutor(max_workers=None, mp_context=None, initializer=None, initargs=())
参数

max_workers:设置进程数量
mp_context:
initializer:initializer是一个可选的callable,在每个工作进程的开头调用; initargs是传递给初始化程序的参数元组。

Future

Future 类封装了一个可调用的异步执行。 Future 实例由 Executor.submit() 创建。

方法

cancel():尝试取消呼叫。如果呼叫当前正在执行并且不能被取消,则该方法将返回 False,否则呼叫将被取消,并且该方法将返回 True。

cancelled():如果呼叫成功取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值