1. 多进程
使用的是concurrent.future
from concurrent.futures import ThreadPoolExecutor, as_completed, ProcessPoolExecutor THREAD_POOL = ThreadPoolExecutor(4) # write为函数名,'打球为参数' def thread_write(): all_task = [] for _ in range(4): all_task.append(THREAD_POOL.submit(write, '打球')) for future in as_completed(all_task): future.result() def write(moive): time.sleep(3) print(moive) time.sleep(2)
2. 多进程
from concurrent.futures import ThreadPoolExecutor, as_completed, ProcessPoolExecutor PROCESS_POOL =ProcessPoolExecutor(4) # write为函数名,'打球为参数' def process_write(): all_task = [] for _ in range(4): all_task.append(PROCESS_POOL.submit(write, '打球')) for future in as_completed(all_task): future.result() def write(moive): time.sleep(3) print(moive) time.sleep(2)