# 实现的内容 模拟购票 20个人买,就有一张购票查,的时候大家都看到,但是购买只能一人购买成功
#利用互斥锁
# from multiprocessing import Process,Lock
# import os,random,json,time
# def search():
# with open('db.txt',encoding= 'utf-8')as f:
# dic = json.load(f) #反序列化 变成字典 因为文件里内容本身是字符串
# print('%s剩余的票数%s'%(os.getpid(),dic['count']))
# def get():
# with open('db.txt',encoding='utf-8')as read_f:
# dic = json.load(read_f)
# if dic['count']>0:
# dic['count']-=1
# time.sleep(random.randint(1,3))
# with open('db.txt','w',encoding='utf-8')as write_f:
# json.dump(dic,write_f) ##把字典写入文件 并且以字符串的形式
# print('%s抢票成功'% os.getpid())
#
# def task(mutex):
# search()
# mutex.acquire() # acquire 获得 互斥获得 加锁
# get()
# mutex.release() # 互斥释放 解锁
# if __name__=='__main__':
# mutex = Lock()
# for i in range(20):
# p = Process(target=task,args=(mutex,))
# p.start()
# #p.join() # 这个效果不能让其他人看见有票
##文件操作 文件的内容是默认存在的类型是字符串,而且里面用的json 必须是双引号。
# mutex 互斥
# mutex = Lock
# mutex.acquire() 加锁
# mutex.release 解锁

# db.txt
# {"count": 1}
# ----------线程的加锁的两种方法
# from threading import Thread,Lock
# import time
# n=100
#
# def task():
# # global n
# # mutex.acquire()
# # temp=n
# # time.sleep(0.1)
# # n=temp-1
# # mutex.release()
#
# global n
# with mutex:
# temp=n
# time.sleep(0.1)
# n=temp-1
# if __name__ == '__main__':
# mutex=Lock()
# t_l=[]
# for i in range(100):
# t=Thread(target=task)
# t_l.append(t)
# t.start()
# for t in t_l:
# t.join()
# print(n)
# ---------------
###生产者和消费者 初级供给 实现的并发
# from multiprocessing import Process,Queue
# import os,random,time
# def producer(q):
# for i in range(10):
# res = '包子%s'%i
# time.sleep(0.5)
# q.put(res)
# print('%s生产了%s'%(os.getpid(),res))
# ###time.sleep(random.randint(2,3)) #如果是启用此行 因为comsumer 的时间比较充裕比生产的
# ##供远远小于求 所以出现串行,生产出一个就立马吃一个
#
# def consumer(q):
# while True:
# res= q.get()
# if res is None:
# break
# print('%s吃了%s'%(os.getpid(),res))
# time.sleep(random.randint(2, 3))
# if __name__== '__main__':
# q = Queue()
# p = Process(target=producer,args=(q,))
# c = Process(target=consumer,args=(q,))
# p.start()
# c.start()
# p.join()
# q.put(None)
# print('主')


import queue #线程队列

# # 队列 线程的排序
# q=queue.Queue(4)
# q.put({'a':1})
# q.put('xxxxx')
# q.put(3)
# q.put(4)
# print(q.get())
# print(q.get())
# print(q.get())
# print(q.get())
# 1进程的排序
# from multiprocessing import Queue
# q = Queue(3)
# q.put(3)
# q.put('wang')
# q.put({'wang':777})
# print(q.get())
# print(q.get())
# print(q.get())
# 3
# wang
# {'wang': 777}
# 进程引用序列 从前到后依次取值
# 变量=Queue(数字存几个)
# 变量.put(任意数据类型)
# 变量.get()

2#优先级队列 从小到大的排列
# q=queue.PriorityQueue(4)
# q.put((10,{'a':1}))
# q.put((-1,'xxxxx'))
# q.put((0,3))
# q.put((4,))
#
# print(q.get())
# print(q.get())
# print(q.get())
# print(q.get())
#
# (-1, 'xxxxx')
# (0, 3)
# (4,)
# (10, {'a}
# 3 堆栈 排序从后到前排序
# q=queue.LifoQueue(4)
# q.put({'a':1})
# q.put('xxxxx')
# q.put(3)
# q.put(4)
#
# print(q.get())
# print(q.get())
# print(q.get())
# print(q.get())


进程 线程的顺序
守护进程:两个子进程,其中一个是守护子进程,一个主进程, 守护进程结束:是在主进程代码一结束就结束,
守护线程:两个子线程,其中一个是守护子线程,一个是主线程,守护线程结束:是在非守护子线程结束就结束。