Python3.5多进程原理与用法实例分析


Posted in Python onApril 05, 2019

本文实例讲述了Python3.5多进程原理与用法。分享给大家供大家参考,具体如下:

Python3.5多进程原理与用法实例分析

进程类:Process

Python3.5多进程原理与用法实例分析

示例及代码:

Python3.5多进程原理与用法实例分析

(1)创建函数作为单进程

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#创建函数并将其作为单个进程
def worker(interval):
  n = 5    #进程数
  while n>0:
    print("The time is :{0}".format(time.ctime()))   #初始化时间
    time.sleep(interval)    #睡眠时间
    n-=1
if __name__ == "__main__":
  # 创建进程,target:调用对象,args:传参数到对象
  p = multiprocessing.Process(target=worker,args=(2,))
  p.start()    #开启进程
  print("进程号:",p.pid)
  print("进程别名:",p.name)
  print("进程存活状态:",p.is_alive())

运行结果:

进程号: 6784
进程别名: Process-1
进程存活状态: True
The time is :Wed Nov  1 10:59:03 2017
The time is :Wed Nov  1 10:59:05 2017
The time is :Wed Nov  1 10:59:07 2017
The time is :Wed Nov  1 10:59:09 2017
The time is :Wed Nov  1 10:59:11 2017

(2)创建函数作为多进程

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#创建函数作为多进程
def work1(interval):
  print("work1...")
  time.sleep(interval)
  print("end work1...")
def work2(interval):
  print("work2...")
  time.sleep(interval)
  print("end work2...")
def work3(interval):
  print("work3...")
  time.sleep(interval)
  print("end work3...")
if __name__ == "__main__":
  p1 = multiprocessing.Process(target=work1,args=(1,))
  p2 = multiprocessing.Process(target=work2,args=(2,))
  p3 = multiprocessing.Process(target=work3,args=(3,))
  p1.start()
  p2.start()
  p3.start()
  print("The number of CPU is %d:"%(multiprocessing.cpu_count()))   #打印CPU核数
  for p in multiprocessing.active_children():     #循环打印子进程的名称和pid
    print("子进程名称:%s,子进程pid:%d" %(p.name,p.pid))
  print("ending....")

运行结果:

The number of CPU is 4:
子进程名称:Process-2,子进程pid:7108
子进程名称:Process-1,子进程pid:1896
子进程名称:Process-3,子进程pid:7952
ending....
work3...
work1...
work2...
end work1...
end work2...
end work3...

注:先运行主进程的内容,再运行子进程

(3)将进程定义成一个类

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#将进程定义为一个类
class ClockProcess(multiprocessing.Process):
  def __init__(self,interval):
    multiprocessing.Process.__init__(self)   #重构了Process类里面的构造函数
    self.interval = interval
  def run(self):     #固定用run方法,启动进程自动调用run方法
    n = 5
    while n>0:
      print("The time is {0}".format(time.ctime()))
      time.sleep(self.interval)
      n-=1
if __name__ == "__main__":
  p = ClockProcess(2)
  p.start()

运行结果:

The time is Wed Nov  1 11:31:28 2017
The time is Wed Nov  1 11:31:30 2017
The time is Wed Nov  1 11:31:32 2017
The time is Wed Nov  1 11:31:34 2017
The time is Wed Nov  1 11:31:36 2017

(4)Queue(队列)实现多进程数据传输

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
#Queue是多进程安全的队列,可以使用实现多进程之间的数据传递
def writer_proc(q):
  try:
    q.put(1,block=False)    #put方法插入数据到队列中
  except:
    pass
def reader_proc(q):
  try:
    print(q.get(block=False))    #get方法从队列中读取并删除一个元素
  except:
    pass
if __name__ == "__main__":
  q = multiprocessing.Queue()
  writer = multiprocessing.Process(target=writer_proc,args=(q,))
  writer.start()
  reader = multiprocessing.Process(target=reader_proc,args=(q,))
  reader.start()
  reader.join()
  writer.join()

运行结果:

1

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python聚类算法之DBSACN实例分析
Nov 20 Python
Python 爬虫的工具列表大全
Jan 31 Python
Python使用smtp和pop简单收发邮件完整实例
Jan 09 Python
利用Django-environ如何区分不同环境
Aug 26 Python
python爬取淘宝商品销量信息
Nov 16 Python
Python Cookie 读取和保存方法
Dec 28 Python
通过python爬虫赚钱的方法
Jan 29 Python
python利用Opencv实现人脸识别功能
Apr 25 Python
python实现五子棋游戏
Jun 18 Python
Windows下Anaconda和PyCharm的安装与使用详解
Apr 23 Python
什么是python的id函数
Jun 11 Python
使用opencv-python如何打开USB或者笔记本前置摄像头
Jun 21 Python
Python选择网卡发包及接收数据包
Apr 04 #Python
详解Python的数据库操作(pymysql)
Apr 04 #Python
python dlib人脸识别代码实例
Apr 04 #Python
python图像处理入门(一)
Apr 04 #Python
python爬虫简单的添加代理进行访问的实现代码
Apr 04 #Python
Django项目中添加ldap登陆认证功能的实现
Apr 04 #Python
使用 Python 玩转 GitHub 的贡献板(推荐)
Apr 04 #Python
You might like
破解图片防盗链的代码(asp/php)测试通过
2010/07/02 PHP
跟着JQuery API学Jquery 之二 属性
2010/04/09 Javascript
理解Javascript_11_constructor实现原理
2010/10/18 Javascript
为超链接加上disabled后的故事
2010/12/10 Javascript
jquery的each方法使用示例分享
2014/03/25 Javascript
JavaScript更改字符串的大小写
2015/05/07 Javascript
javascript中mouseover、mouseout使用详解
2015/07/19 Javascript
js确认框confirm()用法实例详解
2016/01/07 Javascript
JS对象创建的几种方式整理
2017/02/28 Javascript
node.js连接MongoDB数据库的2种方法教程
2017/05/17 Javascript
webpack引入eslint配置详解
2018/01/22 Javascript
vue 使用html2canvas将DOM转化为图片的方法
2018/09/11 Javascript
vue中组件的3种使用方式详解
2019/03/23 Javascript
微信小程序实现转盘抽奖
2020/09/21 Javascript
[01:17]辉夜杯战队访谈宣传片—EHOME
2015/12/25 DOTA
Python读取图片为16进制表示简单代码
2018/01/19 Python
Python实现的本地文件搜索功能示例【测试可用】
2018/05/30 Python
TensorFlow Session使用的两种方法小结
2018/07/30 Python
Python requests库用法实例详解
2018/08/14 Python
解决Python3 抓取微信账单信息问题
2019/07/19 Python
使用Python实现Wake On Lan远程开机功能
2020/01/22 Python
python tkinter之顶层菜单、弹出菜单实例
2020/03/04 Python
python中字符串的编码与解码详析
2020/12/03 Python
python实现定时发送邮件到指定邮箱
2020/12/23 Python
python3定位并识别图片验证码实现自动登录功能
2021/01/29 Python
ebookers英国:隶属全球最大的在线旅游公司Expedia
2017/12/28 全球购物
Raleigh兰令自行车美国官网:英国凤头牌自行车
2018/01/08 全球购物
Steiff台湾官网:德国金耳釦泰迪熊
2019/12/26 全球购物
中学生团员自我评价分享
2013/12/07 职场文书
端午节粽子促销活动方案
2014/02/02 职场文书
网上卖盒饭创业计划书范文
2014/02/07 职场文书
给校长的一封建议书
2014/03/12 职场文书
出纳员岗位职责
2014/03/13 职场文书
2015年法院工作总结范文
2015/04/28 职场文书
公司团队口号霸气押韵
2015/12/24 职场文书
Win11绿屏怎么办?Win11绿屏死机的解决方法
2021/11/21 数码科技