python 线程池
python3线程池
将函数进行多线程处理方法一:
from concurrent.futures import ThreadPoolExecutor
'''
submit(多线程函数,函数运行需要的参数)
结果顺序输出
'''
with ThreadPoolExecutor(max_workers=10) as pool:
fus = [pool.submit(print,x.strip())for x in temps]
for fu in fus:
print(fu.result())
将函数进行多线程处理方法二:
from concurrent.futures import ThreadPoolExecutor,as_completed
with ThreadPoolExecutor(max_workers=10) as pool:
fus = [pool.submit(print,x.strip())for x in temps]
for fu in as_completed(fus):
print(fu.result())
将函数进行多线程处理方法三:
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=10) as pool:
results = pool.map(print,x)
for result in results:
print(result)
多线程函数中有print函数且输出结果混乱时可改写print函数来调整
_print = print
mutex = threading.Lock()
def print(text, *args, **kw):
with mutex:
_print(text, *args, **kw)
|