1. 程式人生 > >進程池和線程池 concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor

進程池和線程池 concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor

nbsp thread 線程池 map append __name__ executor shu def

import time#線程池可以用shutdown submit

from threading import current_thread

from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor

def f1(n):

  print(n)

  time.seelp(1)

  return n*n

if __name__ =="__main__":
  tp = ThreadPoolExecutor(4)

  lst = []

  for i in range(10):

    res = tp.submit(f1,i)

    lst.append(res)

  tp.shutdown()

  for i in lst:

    print(i.result())

import time#進程池 要用map

from threading import current_thread

from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor

def f1(n):

  print(n)

  time.seelp(1)

  return n*n

if __name__ =="__main__":
  tp = ProcessPoolExecutor(4)

  res = tp.map(f1,rangr(10))

  print(res)

進程池和線程池 concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor