线程
官方:进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。在早期面向进程设计的计算机结构中,进程是程序的基本执行实体;在当代面向线程设计的计算机结构中,进程是线程的容器。程序是指令、数据及其组织形式的描述,进程是程序的实体。
格式:
import multiprocessing #使用进程对象之前要导入进程模块
p=multiprocessing.Process(target=[函数],arges=(tuple))
target 函数的名字
arges 传入函数的参数,为tuple类型数据
常用对象
is_alive() #判断是否进程存活,返回bool类型数据
run() #启动进程
start() #启动进程,自动调用run(),此方法常用
join(timeout) #等待进程结束或进程超时
name #进程名字
pid #进程名字
terminate #结束进程
举例:
import multiprocessing
import time
def worker(arge,interval):
print("进程{0}开始".format(arge))
time.sleep(interval)
print("进程{0}结束".format(arge))
def main():
print("开始主进程")
p1 = multiprocessing.Process(target=worker,args=(1,1))
p2 = multiprocessing.Process(target=worker,args=(2,1))
p3 = multiprocessing.Process(target=worker,args=(3,1))
p1.start()
p2.start()
p3.start()
print("主进程结束")
print("the number of cpu is: {0}".format(multiprocessing.cpu_count())) #查看cpu数目
for i in multiprocessing.active_children():
print("the active of child is :{0},进程id号:{1},process is alive:{2}".format(i.name,i.pid,i.is_alive())) #打印子进程名称、id号、存活状态
if __name__ == '__main__':
main()
结果:
开始主进程
主进程结束
the number of cpu is: 4
the active of child is :Process-1,进程id号:33600,process is alive:True
the active of child is :Process-3,进程id号:34240,process is alive:True
the active of child is :Process-2,进程id号:31216,process is alive:True
进程1开始
进程2开始
进程3开始
进程1结束
进程2结束
进程3结束
Process finished with exit code 0
lock组件
lock组件
import multiprocessing
import time
def add1(lock, value, number):
with lock:
print("start add1 number = {0}".format(number))
for i in range(1, 5):
number += value
time.sleep(0.3)
print("number = {0}".format(number))
def add3(lock, value, number):
lock.acquire()
print("start add3 number = {0}".format(number))
try:
for i in range(1, 5):
number += value
time.sleep(0.3)
print("number = {0}".format(number))
finally:
lock.release()
if __name__ == "__main__":
lock = multiprocessing.Lock()
number = 0
pw = multiprocessing.Process(target=add1, args=(lock, 1, number))
pa = multiprocessing.Process(target=add3, args=(lock, 3, number))
pw.start()
pa.start()
print("main process end.")
结果:
main process end.
start add1 number = 0
number = 1
number = 2
number = 3
number = 4
start add3 number = 0
number = 3
number = 6
number = 9
number = 12
Process finished with exit code 0
共享内存
不同进程彼此之间是互不通信,multiprocessing模块中存在Value和array对象,提供进程之间共享内存进行通信
举例:
import multiprocessing
from multiprocessing import Value
import time
def add3(value,number):
try:
print("------开始+3进程------")
for i in range(1,5):
number.value+=value #3+3
print("number = {0}.".format(number.value))
except Exception as e:
raise e
def add1(value,number):
try:
print("------开始+1进程------")
for i in range(1,5):
number.value+=value #1+1
print("number = {0}.".format(number.value))
except Exception as e:
raise e
def main():
number = Value('d',0)
p1=multiprocessing.Process(target=add1,args=(1,number))
p2=multiprocessing.Process(target=add3,args=(3,number))
p1.start()
p2.start()
if __name__ == '__main__':
main()
结果:
------开始+3进程------
number = 3.0.
number = 6.0.
number = 9.0.
number = 12.0.
------开始+1进程------
number = 13.0.
number = 14.0.
number = 15.0.
number = 16.0.
Process finished with exit code 0
今天的文章python学习笔记-线程与进程「建议收藏」分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/87052.html