写在最前面:

linux下可使用 fork 函数

通常使用 multiprocessing更常见

我们分别使用单进程和多进程处理run函数

# -*- coding: utf-8 -*-
import time,os
from multiprocessing import Pooldef run(n):time.sleep(1)print('Run child process %s (%s)...' % (n*n, os.getpid()))if __name__ == "__main__":testFL = [1,2,3,4,5,6]print('顺序执行:')    # 顺序执行(也就是串行执行,单进程)starttime = time.time()for n in testFL:run(n)t = time.time()print("顺序执行时间:", int(t - starttime))print ('concurrent:') # 创建多个进程,并行执行pool = Pool(10)       # 创建拥有10个进程数量的进程池pool.map(run, testFL)pool.close()          # 关闭进程池,不再接受新的进程pool.join()           # 主进程阻塞等待子进程的退出endtime = time.time()print("并行执行时间:", int(endtime-t))

执行结果如下:

顺序执行:
Run child process 1 (32669)...
Run child process 4 (32669)...
Run child process 9 (32669)...
Run child process 16 (32669)...
Run child process 25 (32669)...
Run child process 36 (32669)...
顺序执行时间: 6
concurrent:
Run child process 4 (32671)...
Run child process 1 (32670)...
Run child process 16 (32673)...
Run child process 9 (32672)...
Run child process 25 (32674)...
Run child process 36 (32675)...
并行执行时间: 1Process finished with exit code 0

如果不用map,一般采用apply_async(func[, args[, kwds[, callback]]]),这里是非阻塞的且支持结果返回后进行回调,

而apply用于传递不定参数,同python中的apply函数一致(不过内置的apply函数从2.3以后就不建议使用了),主进程会阻塞于函数。

# -*- coding: utf-8 -*-
import time,os
from multiprocessing import Pooldef run(n):time.sleep(1)print('Run child process %s (%s)...' % (n*n, os.getpid()))if __name__ == "__main__":testFL = [1,2,3,4,5,6]print('顺序执行:')    # 顺序执行(也就是串行执行,单进程)starttime = time.time()for n in testFL:run(n)t = time.time()print("顺序执行时间:", int(t - starttime))print ('concurrent:') # 创建多个进程,并行执行pool = Pool(10)       # 创建拥有10个进程数量的进程池for i in range(1,7):pool.apply_async(run,(i,))pool.close()          # 关闭进程池,不再接受新的进程pool.join()           # 主进程阻塞等待子进程的退出endtime = time.time()print("并行执行时间:", int(endtime-t))

执行结果如下:

顺序执行:
Run child process 1 (32880)...
Run child process 4 (32880)...
Run child process 9 (32880)...
Run child process 16 (32880)...
Run child process 25 (32880)...
Run child process 36 (32880)...
顺序执行时间: 6
concurrent:
Run child process 9 (32885)...
Run child process 1 (32883)...
Run child process 25 (32887)...
Run child process 16 (32886)...
Run child process 4 (32884)...
Run child process 36 (32888)...
并行执行时间: 1Process finished with exit code 0

python多进程和进程池相关推荐

  1. Python 多进程的进程池pool运行时报错:ValueError: Pool not running

    本文仅供学习交流使用,如侵立删!demo下载见文末 Python 多进程的进程池pool运行时报错:ValueError: Pool not running def main(self, num):& ...

  2. Python多进程4 进程池——Pool

    原文地址 学习来源 分类目录--多进程 是multiprocessing模块下的一个类,是一种创建多进程的更加简便的方式,可以更加方便的分配任务与传递参数. pool = mp.Pool(proces ...

  3. python 多进程——使用进程池,多进程消费的数据)是一个队列的时候,他会自动去队列里依次取数据...

    我的mac 4核,因此每次执行的时候同时开启4个线程处理: # coding: utf-8import time from multiprocessing import Pooldef long_ti ...

  4. python—多进程之进程池

    一.进程池 1.进程池定义: 进程池:可以提供指定数量的进程给用户使用,即当有新的请求提交到进程池中时,如果池未满,则会创建一个新的进程用来执行该请求;反之,如果池中的进程数已经达到规定最大值,那么该 ...

  5. python 多进程 multiprocessing 进程池 pool apply_async()函数与apply()函数的用法

    apply函数主要用于传递不定参数,主进程会被阻塞到函数执行结束.也就是说只有apply里面的内容被执行完了,才会进行执行主函数的内容. 参考文章1:python进程池Pool的apply与apply ...

  6. python 多进程 multiprocessing 进程池pool报错 in join assert self._state in (CLOSE, TERMINATE) AssertionError

    原因:pool.close()关闭进程池,不再接受新的任务.join之前必须加上close(),否则会报错 在进程池操作join()时.在前面加上pool.close()即可 参考文章1:Python ...

  7. python 多进程multiprocessing进程池pool tensorflow-yolov3 报错 MemoryError

    进程数设置为1-9个都能正常运行,设置成10个就开始报错,怪事! D:\20191031_tensorflow_yolov3\python\python.exe D:/20191031_tensorf ...

  8. python 多进程multiprocessing进程池pool tensorflow-yolov3 报错TypeError: 'ApplyResult' object is not iterable

    首先,代码结构它长这样: 可每次调用线程池进行识别时,就会报如下错误: D:\20191031_tensorflow_yolov3\python\python.exe D:/20191031_tens ...

  9. 【python】详解multiprocessing多进程-Pool进程池模块(二)

    [python]详解multiprocessing多进程-process模块(一) [python]详解multiprocessing多进程-Pool进程池模块(二) [python]详解multip ...

最新文章

  1. 独家 | 使用机器学习加速对非结构化数据的查询-第1部分(使用BlazeIt加速聚合和限制查询)...
  2. mysql支撑union_mysql 不支撑union select 的盲注方式
  3. java缓冲流,BufferedReader,BufferedWriter 详解
  4. 肇庆计算机那个中专学校好,肇庆中专学校排名,肇庆有哪些中专学校
  5. 一行SQL代码能做什么?
  6. mysql系列:登陆和退出
  7. 学python后到底能干什么-学会Python后都能做什么?网友们的回答简直不要太厉害...
  8. [笔记] 最大权闭合子图最大流最小割相关笔记
  9. Python爬虫从入门到放弃(十五)之 Scrapy框架中Spiders用法
  10. 计算机诗人 原理,自动作诗器,藏头诗软件生成器原理是什么?
  11. 博弈论(Game Theory)入门——完全信息静态博弈
  12. RTKLIB中的各种AR mode 详解
  13. 【单片机毕业设计】【mcuclub-jj-015】基于单片机的风扇的设计
  14. 【论文】Oriented R-CNN for Object Detection
  15. 图像处理术语解释:灰度、色相、饱和度、亮度、明度、阿尔法通道、HSL、HSV、RGBA、ARGB和PRGBA以及Premultiplied Alpha(Alpha预乘)等基础概念详解
  16. 用matlab作地震波vsp图,用于深井VSP地震波检测的光纤配重装置的制作方法
  17. 美国能限制linux内核,因故意引入漏洞,美国一所大学被禁止为 Linux 内核做贡献...
  18. SpringBoot实现多数据源(二)【Mybatis插件】
  19. Opencv学习笔记 - OpenCV 4机器学习算法简介
  20. 使用CSS3+JQuery打造自定义视频播放器

热门文章

  1. ITECH 微电网测试解决方案
  2. sql 创建表,批量插入数据
  3. Druid连接池参考配置和说明
  4. 仿淘票票 —— 微信小程序
  5. 测试工程师需要掌握哪些软技能?
  6. web学习第七天:来自倔驴的怒吼——生理年龄不代表技术年龄
  7. Java 变态面试题
  8. Linux小白进阶之克隆服务器
  9. TensorFlow Estimator 官方文档之----内置Estimator
  10. Inventor API学习