首页 > 编程语言 > 详细

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

时间:2019-01-24 19:18:52      阅读:464      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/16795079a/p/10316321.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!