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

 更新时间:2019年04月05日 09:20:35   作者:loveliuzz  
这篇文章主要介绍了Python3.5多进程原理与用法,结合实例形式分析了多进程的原理、单进程、多进程、进程类及进程队列等相关定义与使用技巧,需要的朋友可以参考下

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

进程类:Process

示例及代码:

(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数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》、《Python+MySQL数据库程序设计入门教程》及《Python常见数据库操作技巧汇总

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

相关文章

  • pytorch 使用加载训练好的模型做inference

    pytorch 使用加载训练好的模型做inference

    今天小编就为大家分享一篇pytorch 使用加载训练好的模型做inference,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Python RawString与open文件的newline换行符遇坑解决

    Python RawString与open文件的newline换行符遇坑解决

    这篇文章主要为大家介绍了Python RawString与open文件的newline换行符遇坑解决示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Pytorch 卷积中的 Input Shape用法

    Pytorch 卷积中的 Input Shape用法

    这篇文章主要介绍了Pytorch 卷积中的 Input Shape用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • 机器学习之KNN算法原理及Python实现方法详解

    机器学习之KNN算法原理及Python实现方法详解

    这篇文章主要介绍了机器学习之KNN算法原理及Python实现方法,结合实例形式详细分析了机器学习KNN算法原理以及Python相关实现步骤、操作技巧与注意事项,需要的朋友可以参考下
    2018-07-07
  • Python多进程使用及进程池详解

    Python多进程使用及进程池详解

    这篇文章主要为大家介绍了Python多进程使用及进程池详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • python redis 删除key脚本的实例

    python redis 删除key脚本的实例

    今天小编就为大家分享一篇python redis 删除key脚本的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • python数据解析BeautifulSoup爬取三国演义章节示例

    python数据解析BeautifulSoup爬取三国演义章节示例

    这篇文章主要介绍了python数据解析BeautifulSoup爬取三国演义章节示例,文中附含详细示例代码,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-09-09
  • django 装饰器 检测登录状态操作

    django 装饰器 检测登录状态操作

    这篇文章主要介绍了django 装饰器 检测登录状态操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • python持久性管理pickle模块详细介绍

    python持久性管理pickle模块详细介绍

    这篇文章主要介绍了python持久性管理pickle模块详细介绍,本文讲解了什么是持久性、一些经过 pickle 的 Python等内容,并讲给出了18个使用示例,需要的朋友可以参考下
    2015-02-02
  • Python:合并两个numpy矩阵的实现

    Python:合并两个numpy矩阵的实现

    今天小编就为大家分享一篇Python:合并两个numpy矩阵的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12

最新评论