1. 程式人生 > >線程ThreadPoolExecutor與進程ProcessPoolExecutor

線程ThreadPoolExecutor與進程ProcessPoolExecutor

clas pre 進程池 rom __main__ res proc sleep from

###################### 線程池 ############
from concurrent.futures import ThreadPoolExecutor
import time
def task(arg):
    print(arg)
    time.sleep(1)

pool = ThreadPoolExecutor(5)

for i in range(50):
    pool.submit(task,i)

#################################################

############################# 進程池 #################
from concurrent.futures import ProcessPoolExecutor import time def task(arg): print(arg) time.sleep(1) if __name__ == __main__: pool = ProcessPoolExecutor(3) for i in range(50): pool.submit(task,i)

線程ThreadPoolExecutor與進程ProcessPoolExecutor