多线程:
import threading
from concurrent import futures
import time
import logging
logging.basicConfig(format='%(thread)s %(threadName)s %(message)s',level=logging.INFO)
def work(n):
logging.info('working-{}'.format(n))
time.sleep(5)
logging.info('end work-{}'.format(n))
executor = futures.ThreadPoolExecutor(3)
fs = []
for i in range(3):
f = executor.submit(work,i)
fs.append(f)
for i in range(3,6):
f = executor.submit(work,i)
fs.append(f)
while True:
time.sleep(2)
logging.info(threading.enumerate())
flag = True
for f in fs:
flag = flag and f.done()
if flag:
executor.shutdown()
logging.info(threading.enumerate())
break
运行结果:
140342196143872 Thread-1 working-0
140342187751168 Thread-2 working-1
140342179358464 Thread-3 working-2
140342243120896 MainThread [<_MainThread(MainThrea