码迷,mamicode.com
首页 > 系统相关 > 详细

多进程2

时间:2019-07-03 20:12:19      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:glob   dump   结果   结束   环境   ==   tar   append   lse   

# ************************************
# 守护进程 用处,将保护的进程独立出来与之前的环境数据隔离,但会随着主程序的终止而结束
# 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

标签:glob   dump   结果   结束   环境   ==   tar   append   lse   

原文地址:https://www.cnblogs.com/zrx19960128/p/11128353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!