concurrent模块

发布时间:2020-06-15 20:04:03 作者:windcharger
来源:网络 阅读:190

ThreadPoolExecutor代码笔记

import threading
from concurrent import futures
import logging
import time

FORMAT = '%(processName)s %(threadName)s %(process)d %(thread)d %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)

def worker(n):
    logging.info('begin to work{}'.format(n))
    time.sleep(5)
    logging.info('finished {}'.format(n))

executor = futures.ThreadPoolExecutor(max_workers=3)
fs = []
for i in range(3):
    future = executor.submit(worker, i)
    fs.append(future)

for i in range(3, 6):
    future = executor.submit(worker, i)
    fs.append(future)

while True:
    time.sleep(2)
    logging.info(threading.enumerate())

    flag = True
    for f in fs:
        logging.info(f.done)
        flag = flag and f.done()

    if flag:
        executor.shutdown()   # 清理池
        logging.info(threading.enumerate())
        break

# 如果想改成进程,只需要将 ThreadPoolExecutor 改成 ProcessPoolExecutor
# 但是注意,一定要写在 __name__ == '__main__' 代码块下
推荐阅读:
  1. 43进程_multiprocessing_concurrent
  2. java.util.concurrent包的拆解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

concurrent threading threadpoolexecutor

上一篇:报错Missing artifact com.alibaba:dubbo:jar:2.8.4

下一篇:SP2-0110: Cannot create save file "afiedt.buf"

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》