学习日记27

今日内容

进程间数据隔离

 在一整个进程内运行时数据是不隔离的

def task():
   global n
   n = 10
   print(f'这是子进程中的{n}')

if __name__ == '__main__':
   n = 100
   task()
   print(f'这是主进程中的{n}')
   
# 这是子进程中的10
# 这是主进程中的10


在不同的进程内运行的数据是隔离的

from multiprocessing import Process
def task():
   global n
   n = 10
   print(f'这是子进程中的{n}')

if __name__ == '__main__':
   n = 100
   p = Process(target=task,)
   p.start()
   print(f'这是主进程中的{n}')
   
# 这是主进程中的100
# 这是子进程中的10

Queue队列

ipc:进程间通信
   
Queue:队列或者管道,创建共享的进程队列
参数:
maxsize,是队列中能存放的最大项数,如没有则无限制,放的时候超过项数则阻塞
blcok为flase直接报错
timeout=1:如果一秒内参数存不进去就报错
put_nowait:如参数存不进去直接报错

方法:
q.put() # 存入一个值
q.get() # 取一个值,没有值的话就阻塞(等待)
q.get_nowait() # 取一个值,取不出来直接报错
q.qsize() # 队列中有几个值
q.empty() # 队列中是否还有空位
q.full() # 队列中是否满了

解决数据隔离问题

def task(queue):
   print('开始写入数据')
   time.sleep(2)
   queue.put(1)
   print('写入数据结束')

if __name__ == '__main__':
   q = Queue(3)
   p = Process(target=task,args=(q,))
   p.start()
   
   print('主进程')

   res = q.get()
print(res)
   
# 成功取出值 1

多进程写入数据

def get_task(queue):
   print('%s:%s' % (queue.get(),queue.get())
   
def put_task(queue):
   queue.put('%s获取到数据' % queue.getpid())
   

if __name__ == '__main__':
   q = Queue(3)
   p = Process(target=put_task,args=(q,))
   p.start()
   
   p1 = Process(target=put_task,args=(q,))
   p1.start()
   
   p2 = Process(target=get_task,args=(q,))
   p2.start()
   
   p3= Process(target=get_task,args=(q,))
   p3.start()
                   

生产者消费者模型代码演示

# 一个生产者一个消费者
def producer(queue):
   for i in range(1,10):
       print('生产了第%s个包子' % i)
       
       time.sleep(random.randint(1,3))
       queue.put('第%s个包子' % i)

       
def consumer(queue):
   while True:
       
       i = queue.get()
       if not i:break
       print('消费了第%s个包子' % i)
       
if __name__ == '__main__':
   q = Queue(3)
   p = Process(target=producer,args=(q,))
   p.start()
   p = Process(target=consumer,args=(q,))
   p.start()
   
   p.join()
   q.put(None)


# 多生产者,少消费者
def producer(queue):
   for i in range(1, 10):
       print('生产了第%s个包子' % i)

       time.sleep(1)
       queue.put('第%s个包子' % i)


def producer(queue, food):
   for i in range(1, 10):
       print('生产了第%s个%s' % (i, food))

       time.sleep(random.randint(1, 3))
       queue.put('第%s个%s' % (i, food))


def consumer(queue):
   while True:

       i = queue.get()
       if not i: break
       print('消费了%s' % i)


if __name__ == '__main__':
   q = Queue(3)
   p1 = Process(target=producer, args=(q, '包子'))
   p2 = Process(target=producer, args=(q, '油条'))
   p3 = Process(target=producer, args=(q, '茶叶蛋'))
   p1.start()
   p2.start()
   p3.start()
   p1 = Process(target=consumer, args=(q,))
   p2 = Process(target=consumer, args=(q,))
   p1.start()
   p2.start()
   p1.join()
   p2.join()
   p3.join()
   q.put(None)
   q.put(None)
   q.put(None)
 
# 多消费者,少生产者
def producer(queue):
   for i in range(1, 10):
       print('生产了第%s个包子' % i)

       time.sleep(1)
       queue.put('第%s个包子' % i)


def producer(queue, food):
   for i in range(1, 10):
       print('生产了第%s个%s' % (i, food))

       time.sleep(random.randint(1, 3))
       queue.put('第%s个%s' % (i, food))


def consumer(queue,name):
   while True:
       try:
           i = queue.get(timeout=3)
           print('%s吃了%s' % (name,i))
       except Exception as e:
           print(e)
           break

if __name__ == '__main__':
   q = Queue(3)
   p1 = Process(target=producer, args=(q, '包子'))
   p2 = Process(target=producer, args=(q, '油条'))
   p3 = Process(target=producer, args=(q, '茶叶蛋'))
   p1.start()
   p2.start()
   p3.start()
   p4 = Process(target=consumer, args=(q,'a'))
   p5 = Process(target=consumer, args=(q,'b'))
   p6 = Process(target=consumer, args=(q,'c'))
   p7 = Process(target=consumer, args=(q,'d'))
   p4.start()
   p5.start()
   p6.start()
   p7.start()
   p1.join()
   p2.join()
   p3.join()

 

posted @ 2021-07-21 19:06  小白白柏柏  阅读(118)  评论(0)    收藏  举报