内容简介:序. multiprocessingPython中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在Python中大部分情况需要使用多进程。Python提供了非常好用的多进程包multiprocessing,只需要定义一个函数,Python会完成其他所有事情。借助这个包,可以轻松完成从单进程到并发执行的转换。multiprocessing支持子进程、通信和共享数据、执行不同形式的同步,提供了Process、Queue、Pipe、Lock等组件。1. Process
序. multiprocessing
Python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在 Python 中大部分情况需要使用多进程。Python提供了非常好用的多进程包multiprocessing,只需要定义一个函数,Python会完成其他所有事情。借助这个包,可以轻松完成从单进程到并发执行的转换。multiprocessing支持子进程、通信和共享数据、执行不同形式的同步,提供了Process、Queue、Pipe、Lock等组件。
1. Process
创建进程的类:Process([group [, target [, name [, args [, kwargs]]]]]),target表示调用对象,args表示调用对象的位置参数元组。kwargs表示调用对象的字典。name为别名。group实质上不使用。
方法:is_alive()、join([timeout])、run()、start()、terminate()。其中,Process以start()启动某个进程。
属性:authkey、daemon(要通过start()设置)、exitcode(进程在运行时为None、如果为–N,表示被信号N结束)、name、pid。其中daemon是父进程终止后自动终止,且自己不能产生新进程,必须在start()之前设置。
例1.1:创建函数并将其作为单个进程
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__":
p = multiprocessing.Process(target = worker, args = (3,))
p.start()
print ("p.pid:", p.pid)
print ("p.name:", p.name)
print ("p.is_alive:", p.is_alive())
运行结果:
p.pid: 2322
p.name: Process-1
p.is_alive: True
The time is Sun May 5 07:36:18 2019
The time is Sun May 5 07:36:21 2019
The time is Sun May 5 07:36:24 2019
The time is Sun May 5 07:36:27 2019
The time is Sun May 5 07:36:30 2019
[Finished in 15.2s]
例1.2:创建函数并将其作为多个进程
import multiprocessing
import time
def worker_1(interval):
print ("worker_1")
time.sleep(interval)
print ("end worker_1")
def worker_2(interval):
print ("worker_2")
time.sleep(interval)
print ("end worker_2")
def worker_3(interval):
print ("worker_3")
time.sleep(interval)
print ("end worker_3")
if __name__ == "__main__":
p1 = multiprocessing.Process(target = worker_1, args = (2,))
p2 = multiprocessing.Process(target = worker_2, args = (3,))
p3 = multiprocessing.Process(target = worker_3, args = (4,))
p1.start()
p2.start()
p3.start()
print("The number of CPU is:" + str(multiprocessing.cpu_count()))
for p in multiprocessing.active_children():
print("child p.name:" + p.name + "\tp.id" + str(p.pid))
print ("END!!!!!!!!!!!!!!!!!")
运行结果如下:
worker_2
worker_1
worker_3
The number of CPU is:1
child p.name:Process-3 p.id2783
child p.name:Process-1 p.id2781
child p.name:Process-2 p.id2782
END!!!!!!!!!!!!!!!!!
end worker_1
end worker_2
end worker_3
[Finished in 4.1s]
例1.3:将进程定义为类
import multiprocessing
import time
class ClockProcess(multiprocessing.Process):
def __init__(self, interval):
multiprocessing.Process.__init__(self)
self.interval = interval
def run(self):
n = 5
while n > 0:
print("the time is {0}".format(time.ctime()))
time.sleep(self.interval)
n -= 1
if __name__ == '__main__':
p = ClockProcess(3)
p.start()
注:进程p调用start()时,自动调用run()
结果如下:
the time is Sun May 5 07:45:05 2019
the time is Sun May 5 07:45:08 2019
the time is Sun May 5 07:45:11 2019
the time is Sun May 5 07:45:14 2019
the time is Sun May 5 07:45:17 2019
[Finished in 15.1s]
例1.4:daemon程序对比结果
#1.4-1 不加daemon属性
import multiprocessing
import time
def worker(interval):
print("work start:{0}".format(time.ctime()));
time.sleep(interval)
print("work end:{0}".format(time.ctime()));
if __name__ == "__main__":
p = multiprocessing.Process(target = worker, args = (3,))
p.start()
print ("end!")
结果如下:
end!
work start:Sun May 5 07:46:54 2019
work end:Sun May 5 07:46:57 2019
[Finished in 3.1s]
#1.4-2 加上daemon属性
import multiprocessing
import time
def worker(interval):
print("work start:{0}".format(time.ctime()));
time.sleep(interval)
print("work end:{0}".format(time.ctime()));
if __name__ == "__main__":
p = multiprocessing.Process(target = worker, args = (3,))
p.daemon = True
p.start()
print ("end!")
结果如下:
end!
[Finished in 0.1s]
注:因子进程设置了daemon属性,主进程结束,它们就随着结束了。
#1.4-3 设置daemon执行完结束的方法
import multiprocessing
import time
def worker(interval):
print("work start:{0}".format(time.ctime()));
time.sleep(interval)
print("work end:{0}".format(time.ctime()));
if __name__ == "__main__":
p = multiprocessing.Process(target = worker, args = (3,))
p.daemon = True
p.start()
p.join()
print ("end!")
结果如下:
work start:Sun May 5 07:49:59 2019
work end:Sun May 5 07:50:02 2019
end!
[Finished in 3.1s]
待续,继续更新中......
Linux公社的RSS地址 : https://www.linuxidc.com/rssFeed.aspx
本文永久更新链接地址: https://www.linuxidc.com/Linux/2019-05/158488.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- php多进程编程详解
- 详解Oracle实例内存结构和进程结构
- Android(IPC)进程间通讯1:详解Binder由来?
- Android 进程通讯机制之 Binder(信使)架构以及内存映射详解
- Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解
- 一篇运维老司机的大数据平台监控宝典(1)-联通大数据集群平台监控体系进程详解
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深入理解TensorFlow:架构设计与实现原理
彭靖田、林健、白小龙 / 人民邮电出版社 / 2018-5-1 / 79.00元
本书以TensorFlow 1.2为基础,从基本概念、内部实现和实践等方面深入剖析了TensorFlow。书中首先介绍了TensorFlow设计目标、基本架构、环境准备和基础概念,接着重点介绍了以数据流图为核心的机器学习编程框架的设计原则与核心实现,紧接着还将TensorFlow与深度学习相结合,从理论基础和程序实现这两个方面系统介绍了CNN、GAN和RNN等经典模型,然后深入剖析了TensorF......一起来看看 《深入理解TensorFlow:架构设计与实现原理》 这本书的介绍吧!