多进程2

Posted zrx19960128

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多进程2相关的知识,希望对你有一定的参考价值。

# ************************************
# 守护进程 用处,将保护的进程独立出来与之前的环境数据隔离,但会随着主程序的终止而结束
# from multiprocessing import Process
# import time
#
# def task():
# print(‘zi run‘)
# # time.sleep(5)
# print(‘zi over‘)
#
# if __name__ == ‘__main__‘:
# p = Process(target=task)
#
# p.daemon = True # 将这个进程设置为守护进程
# p.start()
#
# print(‘zhu start‘)
# time.sleep(3)
# print(‘zhu over‘)
"""
zhu start
zi start
zi over
zhu over
"""
"""
如果没有time.sleep(3)
结果为
zhu start
zhu over
"""


# ************************************
# 进程安全问题

# from multiprocessing import Process
#
# def task():
# for i in range(1000):
# 1 + 1
#
# for i in range(1000):
# print("-------name is nick")
# print("-------gender is girl")
# print("-------age is 18")
#
# def task2():
# for i in range(1000):
# 1 + 2
#
# for i in range(1000):
# print("++++++++name is bgon")
# print("++++++++gender is oldman")
# print("++++++++age is 48")
#
# if __name__ == ‘__main__‘:
# p1 = Process(target=task)
# p2 = Process(target=task2)
#
# p1.start()
# p1.join()
# p2.start()
#
# ************************************

# 互斥锁:防止全局变量的互相争抢
# 确保锁里的在其他全局变量完结之后,在运行或者某种条件之下,锁内一起运行而不会与锁外交叉运行

# from multiprocessing import Process,Lock
# import time,random
#
# def task1(mutex):
# # 假设这不是访问公共资源,那么还可也并发执行
# for i in range(1000):
# print(1)
#
# mutex.acquire() # 这是加锁
# time.sleep(random.random())
# print(‘---name is nick‘)
# time.sleep(random.random())
# print(‘----garden is girl‘)
# time.sleep(random.random())
# print(‘-----age is 18‘)
# mutex.release() # 解锁
#
# def task2(mutex):
# for i in range(1000):
# print(2)
#
# mutex.acquire()
# time.sleep(random.random())
# print("++++++++name is bgon")
# time.sleep(random.random())
# print("++++++++gender is oldman")
# time.sleep(random.random())
# print("++++++++age is 48")
# mutex.release()
#
# if __name__ == ‘__main__‘:
# mutex = Lock() #创建一把互斥锁
# print(‘创建互斥锁‘)
#
# p1 = Process(target=task1,args=(mutex,))
# p2 = Process(target=task2,args=(mutex,))
#
# p1.start()
# p2.start()
#
# """
# 创建互斥锁
# 1
# ......
# 1
# 2
# ......
# 2
# ---name is nick
# ----garden is girl
# -----age is 18
# ++++++++name is bgon
# ++++++++gender is oldman
# ++++++++age is 48
# """
# # ************************************
# # 锁的原理
# lock = False
#
#
#
#
# def task1():
# global lock
# if lock == False:
# lock = True
# open("aaa.txt","wt")
# lock = False
#
#
# def task2():
# global lock
# if lock == False:
# lock = True
# open("aaa.txt","wt")
# lock = False

# ***********************************************************************
# 锁的应用,抢票

# from multiprocessing import Process,Lock
# import json
#
# def show():
# with open(‘db.json‘) as f:
# data = json.load(f)
# print("剩余票数",data["count"])
#
# def buy():
# with open(‘db.json‘) as f:
# data = json.load(f)
# if data["count"] >0:
# data["count"] -= 1
#
# with open("db,json","wt") as f2:
# json.dump(data,f2)
# print("抢票成功")
#
# def task(mutex):
# show()
# mutex.acquire()
# buy()
# mutex.release()
# # 5个人同时抢,假如只有一张票,可能会有多个人刷到1张票的存在,但是下单的话,只能锁定一个人买票
#
# if __name__ == ‘__main__‘:
# mutex = Lock()
# for i in range(5):
# p = Process(target=task,args=(mutex,))
# p.start()


# ***********************************************************************
# Manager的使用
#
# from multiprocessing import Process,Lock,Manager
# import time
#
# def task(data,lock):
# lock.acquire()
# num = data[0]
# time.sleep(0.2)
# data[0] = num -1
# lock.release()
#
# print(‘zi over‘)
#
# if __name__ == ‘__main__‘:
#
# d = [100]
# m = Manager() #创建一个管理器
# syncdict = m.list(d)# 让管理器创建一个与进程同步的字典
#
# # 创建一个锁
# lock = Lock()
#
# ps = []
# for i in range(10):
# p = Process(target=task,args=(syncdict,lock))
# p.start()
# ps.append(p)
#
# for p in ps:p.join()
#
# print(d)
# print(syncdict)

"""
zi over
zi over
zi over
zi over
zi over
zi over
zi over
zi over
zi over
zi over
[100]
[90]

"""

# ***********************************************************************
# 进程队列

# from multiprocessing import Queue
#
# q = Queue(2)# 创建队列 并且只能存储两个元素
#
# q.put(1)
# q.put(2)
# # q.put(3,block=True,timeout=3) # 默认是阻塞的 当容器中没有了位置就阻塞 知道有人从里面取走了元素为止
#
#
#
# print(q.get())
# print(q.get())
# print(q.get(block=True,timeout=3)) # 默认是阻塞的 当容器没有了位置就阻塞 直到有人存入了元素为止


# ***********************************************************************
# 进程调用栈

# def a():
# b()
#
#
# def b():
# c()
#
#
# def c():
# print(‘c‘)
# raise Exception
#
# 调用顺序 a--->b--->c
# 结束顺序 c--->b--->a
#
# 顺序汇总 a--->b--->c--->c--->b--->a

以上是关于多进程2的主要内容,如果未能解决你的问题,请参考以下文章

python并发编程:多线程,多进程

多进程和多线程

linux用户空间 - 多进程编程(三)

Python核心编程总结(五多任务编程之进程与线程)

Python核心编程总结(五多任务编程之进程与线程)

python:多进程,多进程队列,多进程管道,Manager,进程锁,进程池