您好,登錄后才能下訂單哦!
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__' 代碼塊下
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。