由于python 多线程是无法在多核上发挥优势的,所以才用多进程的方式来折中将这个问题解决。

 

 

 

 1 from  multiprocessing import Pool
 2 import os
 3 def f(x):
 4     #打印变量和每个进程的pid这样在显示结果中可以看出是多进程
 5     print x,os.getpid()
 6     return x*x
 7 if __name__ == '__main__':
 8     #用pool 方法来生成一个进程池,每次可以执行5个进程
 9     p=Pool(5)
10     #这个map函数是 multiprocessing 包装后的并不是自带的map函数
11     print  p.map(f,range(5))
View Code

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2021-11-29
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-29
  • 2021-07-27
  • 2021-07-10
  • 2021-12-06
  • 2021-07-20
  • 2021-12-09
  • 2021-11-29
相关资源
相似解决方案