Python concurrent 包

本文介绍了Python concurrent包,重点讲解了其提供的高级异步可执行接口,包括线程池和进程池的Executor,以及对上下文管理的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、concurrent.futures

异步并行任务编程模块,提供一个高级的异步可执行的便利接口。

提供了两个池执行器:

  • ThreadPoolExecutor 异步调用的线程池的Executor
  • ProcessPoolExecutor 异步调用的进程池的Executor

2、ThreadPoolExecutor

ThreadPoolExecutor(max_workers=None)
# 池中至多创建max-workers个线程的池来同时异步执行,返回Executor实例

submit(*args, **kwargs)
# 提交执行的函数及其参数,返回Future类的实例

shutdown(wait=True)
# 清理池

在这里插入图片描述

# concurrent.futures
# ThreadPoolExecutor

from concurrent.futures import ThreadPoolExecutor
import time
import logging
import threading


FORMAT = "%(asctime)s %(threadName)22s %(thread)8d %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)

def worker(n):
    logging.info('enter thread ~~~~~~~~~~~~~~')
    time.sleep(n)
    logging.info('finished ~~~~~~~~~~~~')
    return n


executor = ThreadPoolExecutor(max_workers=3)

fs = []
for i in range(6):
    future = executor.submit(worker, i)  # 异步,非阻塞,执行完毕后立马执行下一条命令
    logging.info(future)
    fs.append(future)
# logging.info('-' * 55)
# for i in range(3):
#     future = executor.submit(worker, 5)  # 异步,非阻塞,执行完毕后立马执行下一条命令
#     logging.info(future)

# logging.info('=' * 55)
# executor.shutdown()
# logging.info('*' * 55)
while True:
    flag = True
    for f in fs:
        logging.info(f.done())
        flag = flag and f.done()

    if flag:
        executor.shutdown()
        logging.info(threading.enumerate())
        break

    # if threading.active_count() == 1:
    #     logging.info(threading.enumerate())
    #     logging.info(future)
    #     break
    time.sleep(1)
    logging.info(threading.enumerate())

for f in fs:
    logging.info(f.result())
2022-04-21 16:01:17,590 ThreadPoolExecutor-0_0     5212 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:17,590             MainThread     7416 <Future at 0x1fb22ca8730 state=running>
2022-04-21 16:01:17,590 ThreadPoolExecutor-0_0     5212 finished ~~~~~~~~~~~~
2022-04-21 16:01:17,590 ThreadPoolExecutor-0_0     5212 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:17,590             MainThread     7416 <Future at 0x1fb22ca8c70 state=running>
2022-04-21 16:01:17,590             MainThread     7416 <Future at 0x1fb22ca8fa0 state=pending>
2022-04-21 16:01:17,591 ThreadPoolExecutor-0_1    31892 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:17,591 ThreadPoolExecutor-0_2    49012 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:17,591             MainThread     7416 <Future at 0x1fb22cb6040 state=running>
2022-04-21 16:01:17,591             MainThread     7416 <Future at 0x1fb22cb6400 state=pending>
2022-04-21 16:01:17,591             MainThread     7416 <Future at 0x1fb22cb64f0 state=pending>
2022-04-21 16:01:17,591             MainThread     7416 True
2022-04-21 16:01:17,591             MainThread     7416 False
2022-04-21 16:01:17,591             MainThread     7416 False
2022-04-21 16:01:17,591             MainThread     7416 False
2022-04-21 16:01:17,591             MainThread     7416 False
2022-04-21 16:01:17,591             MainThread     7416 False
2022-04-21 16:01:18,593 ThreadPoolExecutor-0_0     5212 finished ~~~~~~~~~~~~
2022-04-21 16:01:18,593             MainThread     7416 [<_MainThread(MainThread, started 7416)>, <Thread(ThreadPoolExecutor-0_0, started daemon 5212)>, <Thread(ThreadPoolExecutor-0_1, started daemon 31892)>, <Thread(ThreadPoolExecutor-0_2, started daemon 49012)>]
2022-04-21 16:01:18,593 ThreadPoolExecutor-0_0     5212 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:18,593             MainThread     7416 True
2022-04-21 16:01:18,594             MainThread     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值