SoFunction
Updated on 2024-11-17

Python inter-process communication Queue example analysis

This article examines the main examples related to Python inter-process communication Queue as follows.

Usage:

  • (): Returns the number of messages the current queue contains;
  • (): Returns True if the queue is empty, and False otherwise;
  • (): If the queue is full, return True, and vice versa False;
  • ():Get a message in the queue and then remove it from the queue, with a passable timeout duration.
  • Queue.get_nowait(): quite (False), triggers an exception when value is not fetched: Empty;
  • (): Add a value to the array, pass a reference to the timeout duration.
  • Queue.put_nowait(): equivalent to (False), error when queue is full: Full.

Usage Example:

Come on, get on the code:

#!/usr/bin/env python3

import time
from multiprocessing import Process,Queue

q = Queue() #Create queue, do not pass a number to indicate an unlimited number of queues
for i in range(11):
  (i)

def A():
  while 1:
    try:
      num = q.get_nowait()
      print('I am process A, take out number: %d'%num)
      (1)
    except :
      break

def B():
  while 1:
    try:
      num = q.get_nowait()
      print('This is process B, take out number: %d'%num)
      (1)
    except :
      break

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

This program is to add 10 numbers to the queue and then use 2 processes to take them out.

Run results:

I'm process A. Take out the number: 0.
I'm process B. Take out the number: 1.
I'm process A. Take out the number: 2.
I'm process B. Take out the number: 3.
I'm process A. Take out the number: 4.
I'm process B. Take out the number: 5.
I'm process B. Take out the number: 6.
I'm process A. Take out the number: 7.
I'm process B. Take out the number: 8.
I'm process A. Take out the number: 9.
I'm process B. Take out the number: 10.

3. When using the process pool Pool, Queue will error out and need to be used:

coding

#!/usr/bin/env python3

import time
from multiprocessing import Pool,Manager,Queue

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

def A(i):
  num = q.get_nowait()
  print('I am process %d, take out number: %d'%(i,num))
  (1)
      

pool = Pool(3)

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

()
()

Run results:

I'm process 1, take out the number: 0.
I'm process 0, take out the number: 1.
I'm process 2. Take out the number: 2.
I'm process 4, take out the number: 3.
I'm process 3, take out number 4.
I'm process 5, take out the number: 5.
I'm process 6. Take out the number: 6.
I'm process 7, take out the number: 7.
I'm process 8, take out the number: 8.
I'm process 9, take out the number: 9.

When replacing Manager().Queue() directly with Queue(), there may be resource confusion and missing processes.

4. The main process defines a variable of type Queue and passes it as the args parameter of Process to the sub-processes processA and processB, and one of the two processes writes data to the queue, and the other reads data.

import time
from multiprocessing import Process,Queue

MSG_QUEUE = Queue(5)

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

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

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

  ()
  print 'processA start..'

  ()
  print 'processB start..'

It prints as follows:

C:\Python27\ E:/outofmemory/test/queuetest/
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

summarize

This is the entire content of this article on Python inter-process communication Queue example analysis, I hope you can help. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for your support!