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

作者:loveliuzz 时间:2021-11-28 14:05:03 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/loveliuzz/article/details/78411458

标签:Python3.5,多进程
0
投稿

猜你喜欢

  • 使用 Python 快速实现 HTTP 和 FTP 服务器的方法

    2023-01-14 16:36:04
  • 只需7行Python代码玩转微信自动聊天

    2021-03-09 09:03:22
  • yahoo 页面的标签效果

    2024-04-10 10:38:14
  • mysql myisam 优化设置设置

    2024-01-15 11:04:52
  • 关于页面刷新,事件重复提交的方法分享

    2023-07-06 06:50:03
  • SSB(SQLservice Service Broker) 入门实例介绍

    2024-01-27 14:19:00
  • python:批量统计xml中各类目标的数量案例

    2021-11-17 05:22:44
  • python安装dlib库报错问题及解决方法

    2023-01-27 16:24:41
  • python3使用QQ邮箱发送邮件

    2023-09-05 05:16:15
  • Python cookbook(数据结构与算法)实现查找两个字典相同点的方法

    2022-07-20 22:09:46
  • js阻止浏览器默认行为的简单实例

    2024-04-27 15:22:55
  • SQLSERVER 本地查询更新操作远程数据库的代码

    2023-07-23 21:58:55
  • python遗传算法之单/多目标规划问题

    2021-09-09 20:27:24
  • python分块读取大数据,避免内存不足的方法

    2022-09-30 13:05:17
  • Python图像处理之透视变换的实战应用

    2022-08-16 17:22:10
  • 未知高度的图片垂直居中

    2010-12-17 12:36:00
  • 使用TensorFlow实现简单线性回归模型

    2022-11-30 19:51:48
  • vue 解决异步数据更新问题

    2024-04-30 10:45:28
  • js比较日期大小的方法

    2024-04-10 10:49:45
  • php设计模式之适配器模式实例分析【星际争霸游戏案例】

    2024-05-11 09:55:05
  • asp之家 网络编程 m.aspxhome.com