进程池和线程池 concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor

Posted 16795079a

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了进程池和线程池 concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

开启线程池和进程池

Python进程池和线程池

python中的进程池和线程池

Java线程池工具类

线程池和进程池

进程池和线程池