Python线程池ThreadPoolExecutor的使用

2019-05-18  本文已影响0人  alan2yang

ThreadPoolExecutor实际上是一个上下文管理器,可以设置池中线程数量,同时保证池中的每个线程都调用.join()方法。

它还有两个重要的方法submit和map,其中:
submit(func, *args, **kwargs)的传参效果是运行func( *args, **kwargs)函数,
map(func, *iterables)的传参效果是对iterables迭代器遍历执行func(iterables[i])函数,但要保证func函数要接收参数,否则出错了也不会报错,程序直接结束(坑)。

下面是两个实例,帮助理解,详细的介绍,看官方文档:https://docs.python.org/3/library/concurrent.futures.html

import concurrent.futures
import logging
import time

name=0

def thread_fuction():
    global name
    name+=1
    logging.info(f"thread {name} start")
    time.sleep(2)
    logging.info(f"thread {name} end")


if __name__ == "__main__":

    format="%(asctime)s:%(message)s"
    logging.basicConfig(format=format,level=logging.INFO,datefmt="%H:%M:%S")

    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as texecutor:
        texecutor.submit(thread_fuction)
        texecutor.submit(thread_fuction)
        texecutor.submit(thread_fuction)
        texecutor.submit(thread_fuction)
        texecutor.submit(thread_fuction)
        texecutor.submit(thread_fuction)

# 运行结果
11:50:00:thread 1 start
11:50:00:thread 2 start
11:50:00:thread 3 start
11:50:02:thread 3 end
11:50:02:thread 4 start
11:50:02:thread 4 end
11:50:02:thread 5 start
11:50:02:thread 5 end
11:50:02:thread 6 start
11:50:04:thread 6 end
11:50:04:thread 6 end
11:50:04:thread 6 end

import concurrent.futures
import logging
import time

def thread_fuction(name):
    logging.info(f"thread {name} start")
    time.sleep(2)
    logging.info(f"thread {name} end")

if __name__ == "__main__":

    format="%(asctime)s:%(message)s"
    logging.basicConfig(format=format,level=logging.INFO,datefmt="%H:%M:%S")

    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as texecutor:
        texecutor.map(thread_fuction,range(10))

# 运行结果
11:03:09:thread 0 start
11:03:09:thread 1 start
11:03:09:thread 2 start
11:03:11:thread 0 end
11:03:11:thread 3 start
11:03:11:thread 1 end
11:03:11:thread 2 end
11:03:11:thread 4 start
11:03:11:thread 5 start
11:03:13:thread 3 end
11:03:13:thread 6 start
11:03:13:thread 4 end
11:03:13:thread 7 start
11:03:13:thread 5 end
11:03:13:thread 8 start
11:03:15:thread 6 end
11:03:15:thread 9 start
11:03:15:thread 7 end
11:03:15:thread 8 end
11:03:17:thread 9 end
上一篇 下一篇

猜你喜欢

热点阅读