SoFunction
Updated on 2024-11-17

Python inter-process communication Queue message queue usage analysis

This article example describes Python inter-process communication Queue message queue usage. Shared for your reference, as follows:

Inter-process communication-Queue

Processes sometimes need to communicate with each other, and the operating system provides a number of mechanisms to enable inter-process communication.

1. Use of Queues

Multiprocessing module can be used to achieve the multiprocessing module Queue data transfer between , Queue itself is a message queuing program , first of all, with a small example to demonstrate the working principle of Queue:
The code is as follows:

#coding=utf-8
from multiprocessing import Queue
# Initialize a Queue object that can receive up to three put messages
q = Queue(3)
('Message 1')
('Message 2')
print(())#False
('Message 3')
print(())#True
#Because the message queue is full the following tries will throw exceptions, the first try will wait 2 seconds before throwing an exception, the second Try will throw an exception immediately.
try:
  ('Message 4', True, 2)
except:
  print('Message queue is full, number of existing messages: %s'%())
try:
  q.put_nowait('Message 4')# Equivalent to ('Message 4', False)
except:
  print('Message queue is full, number of existing messages: %s'%())
# The recommended way to determine if the message queue is full before writing to the
if not ():
  q.put_nowait('Message 4')
# When reading a message, first determine whether the message queue is empty, and then read the
if not ():
  for i in range(()):
    print(q.get_nowait())

Run results:

False
True
Message queue is full, number of existing messages: 3
Message queue is full, number of existing messages: 3
Message 1
Message 2
Message 3

clarification

When initializing a Queue() object (e.g., q=Queue()), if the maximum number of messages that can be received is not specified in the parentheses, or if the number is negative, then this means that there is no upper limit to the number of messages that can be received (up to the end of memory);

(): 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, vice versa False;
([block[, timeout]]): Gets a message in the queue and then removes it from the queue. block defaults to True;

1) If block uses the default value and does not set timeout (in seconds), the message queue if empty, at this time the program will be blocked (stopped in the read state) until the message is read from the message queue, if timeout is set, it will wait for timeout seconds, if it has not read any message, then it will throw the " " exception;

2) If the block value is False, the message queue will immediately throw a " " exception if it is empty;
Queue.get_nowait(): Equivalent (False);
(item,[block[, timeout]]): Write the item message to the queue. block defaults to True;

1) If block uses the default value and does not set a timeout (in seconds), the program will block (stop in Write In state) if there is no more space in the message queue until space is freed from the message queue. If timeout is set, the program waits for the timeout seconds, and throws a "" exception if there is no space left;

2) If the block value is False, the message queue will immediately throw a "" exception if there is no space to write;

Queue.put_nowait(item): equivalent(item, False);

2. Example of a Queue

Let's take Queue as an example and create two child processes, one to write data to Queue and one to read data from Queue:

#coding=utf-8
from multiprocessing import Queue, Process
import time, random, os
# Code executed by the write data process
def write(q):
  l1 = ['A','B','C']
  for value in l1:
    print('put %s to queue...'%value)
    (value)
    (())
# Code for reading data execution
def read(q):
  while True:
    if not ():
      value = (True)
      print('get %s from queue...' % value)
      (())
    else:
      break
if __name__ == "__main__":
  #The parent process creates the Queue and passes it to each child process.
  q = Queue()
  qw = Process(target=write, args=(q,))
  qr = Process(target=read, args=(q,))
  #Start sub-process qw write
  ()
  ()
  # Start sub-process qr write
  ()
  ()
  The # qr process is a dead loop and cannot wait for it to end, it can only be forcibly terminated :)
  print('All data has been written and read')

Run results:

put A to queue...
put B to queue...
put C to queue...
get A from queue...
get B from queue...
get C from queue...
All data has been written and read.

3. Queue in the process pool

To create a process using a Pool, you need to use the()hit the nail on the headQueue()Instead of()Otherwise, you will get an error message as follows:

RuntimeError: Queue objects should only be shared between processes
through inheritance.

The following example demonstrates how processes in a process pool communicate:

The code is as follows:

#coding=utf-8
from multiprocessing import Manager, Pool
import time, random, os
def writer(q):
  print('writer started %s, parent process is %s'%((),()))
  l1 = ['a','b','c','d','e']
  for value in l1:
    (value)
def reader(q):
  print('reader started %s, parent process is %s'%((),()))
  for i in range(()):
    print('reader got message from Queue: %s'%(True))
if __name__ == "__main__":
  print('Parent process %s started...'%())
  q = Manager().Queue() # Use the Queue in Manager to initialize the
  po = Pool()
  # Create the process in blocking mode so that you don't need to use a dead loop in the reader, you can let the writer finish executing completely and then use the reader to read it.
  (writer, (q,))
  (reader, (q,))
  ()
  ()
  print('%s end'%())

Run results:

Parent process 7415 starts...
Writer starts 7421, parent process 7415.
Reader starts 7422, parent 7415.
reader gets the message from Queue: a
reader gets the message from Queue: b
reader gets the message from Queue: c
reader gets the message from Queue: d
reader gets the message from Queue: e
End of 7415

Readers interested in more Python related content can check out this site's topic: theSummary of Python process and thread manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorials》、《Python + MySQL Database Programming Tutorial for Beginnersand theSummary of common database manipulation techniques in Python

I hope that what I have said in this article will help you in Python programming.