本文实例讲述了Python3多进程 multiprocessing 模块。分享给大家供大家参考,具体如下:

多进程 Multiprocessing 模块

对《Python3多进程 multiprocessing 模块实例详解》总结来说,为我们帝国cms模板制作很实用。

multiprocessing 模块官方说明文档

Process 类

Process 类用来描述一个进程对象。创建子进程的时候,只需要传入一个执行函数和函数的参数即可完成 Process 示例的创建。

star()方法启动进程,

join()方法实现进程间的同步,等待所有进程退出。

close() 用来阻止多余的进程涌入进程池 Pool 造成进程阻塞。

multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

target是函数名字,需要调用的函数

args函数需要的参数,以 tuple 的形式传入

示例:

import multiprocessing

import os

def run_proc(name):

print('Child process {0} {1} Running '.format(name, os.getpid()))

if __name__ == '__main__':

print('Parent process {0} is Running'.format(os.getpid()))

for i in range(5):

p = multiprocessing.Process(target=run_proc, args=(str(i),))

print('process start')

p.start()

p.join()

print('Process close')

结果:

Parent process 809 is Running

process start

process start

process start

process start

process start

Child process 0 810 Running

Child process 1 811 Running

Child process 2 812 Running

Child process 3 813 Running

Child process 4 814 Running

Process close

Pool

Pool 可以提供指定数量的进程供用户使用,默认是 CPU 核数。当有新的请求提交到 Poll 的时候,如果池子没有满,会创建一个进程来执行,否则就会让该请求等待。

- Pool 对象调用 join 方法会等待所有的子进程执行完毕

- 调用 join 方法之前,必须调用 close

- 调用 close 之后就不能继续添加新的 Process 了

pool.apply_async

apply_async方法用来同步执行进程,允许多个进程同时进入池子。

import multiprocessing

import os

import time

def run_task(name):

print('Task {0} pid {1} is running, parent id is {2}'.format(name, os.getpid(), os.getppid()))

time.sleep(1)

print('Task {0} end.'.format(name))

if __name__ == '__main__':

print('current process {0}'.format(os.getpid()))

p = multiprocessing.Pool(processes=3)

for i in range(6):

p.apply_async(run_task, args=(i,))

print('Waiting for all subprocesses done...')

p.close()

p.join()

print('All processes done!')

结果:

current process 921

Waiting for all subprocesses done...

Task 0 pid 922 is running, parent id is 921

Task 1 pid 923 is running, parent id is 921

Task 2 pid 924 is running, parent id is 921

Task 0 end.

Task 3 pid 922 is running, parent id is 921

Task 1 end.

Task 4 pid 923 is running, parent id is 921

Task 2 end.

Task 5 pid 924 is running, parent id is 921

Task 3 end.

Task 4 end.

Task 5 end.

All processes done!

pool.apply

apply(func[, args[, kwds]])

该方法只能允许一个进程进入池子,在一个进程结束之后,另外一个进程才可以进入池子。

import multiprocessing

import os

import time

def run_task(name):

print('Task {0} pid {1} is running, parent id is {2}'.format(name, os.getpid(), os.getppid()))

time.sleep(1)

print('Task {0} end.'.format(name))

if __name__ == '__main__':

print('current process {0}'.format(os.getpid()))

p = multiprocessing.Pool(processes=3)

for i in range(6):

p.apply(run_task, args=(i,))

print('Waiting for all subprocesses done...')

p.close()

p.join()

print('All processes done!')

结果:

Task 0 pid 928 is running, parent id is 927

Task 0 end.

Task 1 pid 929 is running, parent id is 927

Task 1 end.

Task 2 pid 930 is running, parent id is 927

Task 2 end.

Task 3 pid 928 is running, parent id is 927

Task 3 end.

Task 4 pid 929 is running, parent id is 927

更多:python人脸考勤系统Python3多进程 multiprocessing 模块实例详解

https://www.002pc.comhttps://www.002pc.com/python/721.html

你可能感兴趣的multiprocessing,Python3,详解,实例,模块,进程

No alive nodes found in your cluster

0踩

0 赞

python人脸考勤系统_python人脸考勤系统Python3多进程 multiprocessing 模块实例详解相关推荐

  1. python 录制web视频_Python django框架 web端视频加密的实例详解

    视频加密流程图: 后端获取保利威的视频播放授权token,提供接口api给前端 参考文档:http://dev.polyv.net/2019/videoproduct/v-api/v-api-play ...

  2. python如何定义类_Python中类的定义、继承及使用对象实例详解

    本文实例讲述了Python中类的定义.继承及使用对象的方法.分享给大家供大家参考.具体分析如下: Python编程中类的概念可以比作是某种类型集合的描述,如"人类"可以被看作一个类 ...

  3. python脚本例子_python dict 字典 以及 赋值 引用的一些实例(详解)

    最近在做一个很大的数据库方面的东东,要用到根据数值来查找,于是想到了python中的字典,平时没用过dict这个东东 用的最多的还是 list 和 tuple (网上查 用法一大堆) 看了一下创建字典 ...

  4. python从date目录导入数据集_PyTorch加载自己的数据集实例详解

    数据预处理在解决深度学习问题的过程中,往往需要花费大量的时间和精力. 数据处理的质量对训练神经网络来说十分重要,良好的数据处理不仅会加速模型训练, 更会提高模型性能.为解决这一问题,PyTorch提供 ...

  5. python 经典脚本文件_Python3.5文件读与写操作经典实例详解

    本文实例讲述了Python3.5文件读与写操作.分享给大家供大家参考,具体如下: 1.文件操作的基本流程: (1)打开文件,得到文件句柄并赋值给一个变量 (2)通过句柄对文件进行操作 (3)关闭文件 ...

  6. python抽奖滚动界面_Python使用Tkinter实现转盘抽奖器的步骤详解

    我使用 Python 中的 Tkinter 模块实现了一个简单的滚动抽奖器,接下来继续写一个简单的转盘抽奖器. 滚动抽奖器与点名的场景相似,是从一群人中抽出中奖的人,奖品是提前确定了的,抽奖只是确定中 ...

  7. python类的命名空间_Python之关于类变量的两种赋值区别详解

    我就废话不多说了,还是直接看代码吧! # -*- coding:utf-8 -*- #面试题,写一个方法,将一行字符串中所有的单词数量统计出来 class Person(object): TAG = ...

  8. python模拟手写笔迹_Python实现基于KNN算法的笔迹识别功能详解

    本文实例讲述了Python实现基于KNN算法的笔迹识别功能.分享给大家供大家参考,具体如下: 需要用到: Numpy库 Pandas库 手写识别数据 点击此处本站下载. 数据说明: 数据共有785列, ...

  9. python中英文字频率_python实现统计文本中单词出现的频率详解

    本文实例为大家分享了python统计文本中单词出现频率的具体代码,供大家参考,具体内容如下 #coding=utf-8 import os from collections import Counte ...

  10. Python 多进程 multiprocessing.Pool类详解

    multiprocessing模块 multiprocessing包是Python中的多进程管理包.它与 threading.Thread类似,可以利用multiprocessing.Process对 ...

最新文章

  1. ucos-iii串口用信号量及环形队列中断发送,用内建消息队列中断接收
  2. [转]Ext Grid控件的配置与方法
  3. PYTHON2.day03
  4. 12C OCP 1Z0-063 题库(8月以前)
  5. apache 的工作模式
  6. VS2005 VSTO 项目创建
  7. Java后端开发需要具备哪些知识结构
  8. 简述计算机阶码表达什么,阶码
  9. flask url_for()
  10. php 中c函数重载,php函数重载的替代方法--伪重载详解
  11. 进销存系统收费标准是怎样的?
  12. PostgreSQL如何实现MVCC (基于xmin、xmax、cmin、cmax)
  13. Excel表格转换为MarkDown表格工具
  14. 用TinySpider进行网页抓取实例
  15. 用YOLO3进行人民币编码的定位与切割
  16. coda2怎么连接mysql_Coda 2.7.7 - Web管理通用工具包
  17. CUMT矿大----电路与数字系统实验四 计数、译码、显示的HDL设计
  18. 为什么常常会出现人+机人的现象?
  19. 英文自我介绍范例, 外企找工作必备(必背)神器
  20. 组合数学 C(n,k)

热门文章

  1. 直播丨上海传智播客-黑马程序员/黑马设计师-封箱之作-“大神季”
  2. 拼多多API接口调用方法(内附上多个可用API)
  3. 微信小程序实现每日签到功能
  4. RS-485上下拉电阻选择
  5. AVX512与AVX2比较
  6. pycharm连接github
  7. 2021年智慧校园APP全新上线
  8. json rpgmv 加密_【RPG Maker MV插件编程】【实例教程6】存档的加密解密与保护
  9. kingbase 修改数据库密码
  10. 计算机网络哈勃,NASA已基本确认哈勃故障原因 出在电源控制单元上