Python进程间通信Queue实例解析

作者:王辉_Python 时间:2023-11-06 13:50:08 

本文研究的主要是Python进程间通信Queue的相关实例,具体如下。

1.Queue使用方法:

  • Queue.qsize():返回当前队列包含的消息数量;

  • Queue.empty():如果队列为空,返回True,反之False ;

  • Queue.full():如果队列满了,返回True,反之False;

  • Queue.get():获取队列中的一条消息,然后将其从列队中移除,可传参超时时长。

  • Queue.get_nowait():相当Queue.get(False),取不到值时触发异常:Empty;

  • Queue.put():将一个值添加进数列,可传参超时时长。

  • Queue.put_nowait():相当于Queue.get(False),当队列满了时报错:Full。

2.Queue使用实例:

来,上代码:


#!/usr/bin/env python3

import time
from multiprocessing import Process,Queue

q = Queue() #创建列队,不传数字表示列队不限数量
for i in range(11):
 q.put(i)

def A():
 while 1:
   try:
     num = q.get_nowait()
     print('我是进程A,取出数字:%d'%num)
     time.sleep(1)
   except :
     break

def B():
 while 1:
   try:
     num = q.get_nowait()
     print('我是进程B,取出数字:%d'%num)
     time.sleep(1)
   except :
     break

p1 = Process(target = A)
p2 = Process(target = B)
p1.start()
p2.start()

此程序是在队列中加入10个数字,然后用2个进程来取出。

运行结果:

我是进程A,取出数字:0
我是进程B,取出数字:1
我是进程A,取出数字:2
我是进程B,取出数字:3
我是进程A,取出数字:4
我是进程B,取出数字:5
我是进程B,取出数字:6
我是进程A,取出数字:7
我是进程B,取出数字:8
我是进程A,取出数字:9
我是进程B,取出数字:10

3.使用进程池Pool时,Queue会出错,需要使用Manager.Queue:

上代码


#!/usr/bin/env python3

import time
from multiprocessing import Pool,Manager,Queue

q = Manager().Queue()
for i in range(11):
 q.put(i)

def A(i):
 num = q.get_nowait()
 print('我是进程%d,取出数字:%d'%(i,num))
 time.sleep(1)

pool = Pool(3)

for i in range(10):
 pool.apply_async(A,(i,))

pool.close()
pool.join()

运行结果:

我是进程1,取出数字:0
我是进程0,取出数字:1
我是进程2,取出数字:2
我是进程4,取出数字:3
我是进程3,取出数字:4
我是进程5,取出数字:5
我是进程6,取出数字:6
我是进程7,取出数字:7
我是进程8,取出数字:8
我是进程9,取出数字:9

当把Manager().Queue()直接换成Queue(),可能会出现资源混乱,缺少进程。

4.主进程定义了一个Queue类型的变量,并作为Process的args参数传给子进程processA和processB,两个进程一个向队列中写数据,一个读数据。


import time
from multiprocessing import Process,Queue

MSG_QUEUE = Queue(5)

def startA(msgQueue):
 while True:
   if msgQueue.empty() > 0:
     print 'queue is empty %d' % (msgQueue.qsize())
   else:
     msg = msgQueue.get()
     print 'get msg %s' % (msg,)
   time.sleep(1)

def startB(msgQueue):
 while True:
   msgQueue.put('hello world')
   print 'put hello world queue size is %d' % (msgQueue.qsize(),)
   time.sleep(3)

if __name__ == '__main__':
 processA = Process(target=startA,args=(MSG_QUEUE,))
 processB = Process(target=startB,args=(MSG_QUEUE,))

processA.start()
 print 'processA start..'

processB.start()
 print 'processB start..'

其打印的结果如下:

C:\Python27\python.exe E:/outofmemory/test/queuetest/queuetest.py
processA start..
processB start..
queue is empty 0
put hello world queue size is 1
get msg hello world
queue is empty 0
queue is empty 0
put hello world queue size is 1
get msg hello world
queue is empty 0
queue is empty 0
put hello world queue size is 1

来源:https://www.cnblogs.com/PrettyTom/p/6583153.html

标签:python,进程,queue
0
投稿

猜你喜欢

  • pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)

    2023-05-15 02:03:20
  • python实现从ftp上下载文件的实例方法

    2021-04-24 16:37:02
  • Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法详解

    2022-10-26 19:49:05
  • form的submit方法和submit事件(onsubmit)

    2008-09-28 13:29:00
  • 详解MySQL主键唯一键重复插入解决方法

    2024-01-20 16:41:22
  • 静态页面利用JS读取cookies记住用户信息

    2011-04-14 11:17:00
  • perl哈希的一个实例分析

    2023-07-23 21:35:16
  • python-pymysql如何实现更新mysql表中任意字段数据

    2024-01-19 17:35:01
  • Python list列表删除元素的4种方法

    2021-09-11 06:39:09
  • 设定sql server定期自动备份数据库

    2024-01-16 18:14:30
  • Python简单基础小程序的实例代码

    2021-03-29 21:17:59
  • asp+jsp+JavaScript动态实现添加数据行

    2023-07-03 05:37:15
  • PyTorch+LSTM实现单变量时间序列预测

    2023-09-23 02:58:05
  • canvas时钟效果

    2024-05-05 09:14:22
  • CSS模块化设计

    2009-01-05 12:10:00
  • JavaScript 解析 Cookie 的函数

    2007-11-08 11:58:00
  • Python+Pygame实现神庙逃亡游戏

    2022-06-12 16:26:28
  • MySQL下载安装详情图文教程

    2024-01-24 15:48:48
  • django重新生成数据库中的某张表方法

    2024-01-23 01:14:45
  • python如何正确的操作字符串

    2023-12-28 02:46:30
  • asp之家 网络编程 m.aspxhome.com