message queue for interprocess communication

message queue for interprocess communication

1. Access messages according to the principle of first-in, first-out, and the messages put in first are taken out first

2. If there is no message in the queue, the queue is empty, which means that the operation of fetching messages cannot be performed. When the number of messages in the queue reaches the upper limit, the queue is full and the message cannot be stored at this time.

3. Between different processes, the purpose of communication is achieved by storing and getting messages to the queue

from  multiprocessing  import  Queue

q = Queue(maxsize)
Function: Create a message queue object
maxsize : Set the size of the message queue, indicating how many messages can be stored at most

q.put(obj,block = True,timeout = None)
Function: store a message into the queue
obj : the object to store
block: Whether it is blocking mode, the default is True, if set to False, it is non-blocking

timeout: block time when block = True

q.get()
Function: remove a message from the queue
block: The default is True for blocking, and false for non-blocking, if the queue is empty, an empty exception is returned immediately
timeout: When block=True, it means blocking waiting time, and if it times out, it returns an exception



q.full()
Determine if the queue is full, if it is full then return True otherwise return false

q.empty()
Check if the queue is empty, if it is empty then return True otherwise return false

q.qsize()
View the number of messages in the current queue

practise:
Parent process: Get input from the terminal and put it into the message queue

Subprocess: get the content from the message queue and print it out

Shared memory:

Features: 1. Efficient inter-process communication
       2. There is a risk in security, because the storage of the content will overwrite the original content, so it is likely to have been tampered with during use
			 3. Based on 2 reasons, it is often necessary to consider the locking problem when using

from multiprocessing import Value,Array

1. The use of the two methods is basically the same. The value stores a value in the shared memory, and the array can store multiple values, but the types must be the same

2. After any process modifies the data in the shared memory, other processes will also obtain the modified data

3. The first parameter of the two methods is the same, both are ctypes, see the table for details. The second parameter value is a value of the corresponding type. Array can be a value (representing how much data to open a memory space to fill with 0), or it can be an iterative object (which will open up space from the iterative content and fill the content)

           Pipeline message queue shared memory

open up space memory memory memory

Read and write mode can be bidirectional one-way FIFO operation memory
           Message flow count numeric array
					 send/recv put/get direct modification

Efficiency is generally fast

Whether it is needed or not
mutually exclusive


Signal:

It is an asynchronous inter-process communication method

Signals have names, meanings, and default behaviors

send a signal
import them

os.kill(pid,sig)
Function: Send a signal to a process
pid : to which process to send, the PID number of the process
sig : what signal to send using signal.signum

signal.alarm(sec)
Function: send a signal to itself
sec: after sec seconds the signal will be sent

* Only one clock signal can be suspended in a process

process signal

signal.pause()
Function: suspend waiting for a signal

signal.signal(signum,handler)
Function: handle a signal
Parameters: signum : the signal to handle
       handler: the processing method for the signal
			 Processing method: ignore the signal SIG_IGN
                 Execute SIG_DFL with default method
								 Execute function using specified method

Asynchronous processing: Using signal to catch a signal at a certain time will tell the kernel to monitor the process instead of blocking and waiting. During the life cycle of the process, as long as the signal is sent in, it will be processed.

Zombie process handling
The parent process joins before the execution of the child process ends:
signal.signal(signal.SIGCHLD,signal.SIG_IGN)


synchronization and mutual exclusion

Critical resource: Visible to multiple processes or threads, resources that are prone to contention (such as shared memory) are called critical resources

Critical section: A code segment that operates on critical resources, called a critical section

Synchronization: Synchronization is a constraint relationship. Two or more processes are established to complete a certain task, and the processes wait in an orderly manner to transmit information and complete the work. This constraint stems from the cooperation between processes

Mutual exclusion: Mutual exclusion is an indirect restriction. When a process enters the critical area for locking, other processes cannot operate the critical resources at this time. Only after the process finishes using the critical resources and unlocks the critical resources, other processes can use. This technique is often done by blocking

Synchronized Mutex Method

Event

e = Event() creates an event object

e.is_set() determines whether the event is set

e.set() Set the event object

e.wait(2) The blocking wait time is set (the parameter indicates the timeout time)

Lock
from multiprocessing import Lock
lock = Lock()

lock.acquire() locks
lock.release() unlocks

with lock: lock

Operation:
The story of the driver and conductor

1. Create parent and child processes to represent drivers and conductors respectively
2. When the conductor catches the SIGINT signal, it sends SIGUSR1 to the driver, and the driver prints ('departure')
   
	 When the conductor catches the SIGQUIT signal, send SIGUSR2 to the driver, the driver prints ('stop')
   
	 The driver catches SIGTSTP, sends SIGUSR1 to the conductor,
	 conductor print ('get off when you arrive')
3. After arriving at the station, the driver waits for the conductor to get off the bus first, and then exits by himself

Reminder: When a signal is sent through the keyboard, it will be sent to all processes in the terminal

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325740696&siteId=291194637