Yesterday's content review
Process multiprocessprocess--process creates a module of a process in Python the start daemon daemon join waits for the child process to execute the end lock Lockacquire Release Lock is a tool for synchronous control if there are multiple processes executing a piece of code at the same time, then the data in memory will not conflict, but if the file is involved, There is a resource conflict problem with the database. We need to lock this code up with a lock. After any process executes the acquire, all other processes will block here, waiting for a release semaphore Semaphore lock + Counters can only have a specified number of processes executing the same piece of code event at the same time Eventset clear Is_set control the state of the object wait depending on the state to perform the effect also different state is true---> Pass The status is false--blocking general wait is and set clear is placed in a different process set/clear is responsible for controlling the state wait is responsible for perceiving state I can control the operation of another or more processes in one process IPC communication queues queue Pipeline pipe
I. interprocess communication (Queues and pipelines)
Determine if the queue is empty
From multiprocessing Import Process,queueq = Queue () print (Q.empty ())
Execution output: True
Determine if the queue is full
From multiprocessing Import Process,queueq = Queue () print (Q.full ())
Execution output: False
If the queue is full, then the operation to increment the value will be blocked until the queue has a spare
From multiprocessing import Process,queueq = Queue # Create a queue that can only put 10 value for I in range: q.put (i) # Add a Valueprint (Q.qsize ()) # Returns the correct number of current items in the queue print (Q.full ()) # If q is full, return to Trueq.put (111) # and add another value print ( Q.empty ())
Execution output:
You can see that the program is not over, and the code after Q.put (111) is blocked.
Summarize:
Queues can specify a capacity at the time of creation
If the queue already has enough data in the process of running the program, then put will block
If the queue is empty, then get is blocked
Why do you want to specify the length of the queue? is to prevent memory explosion.
A queue that cannot be stored indefinitely. After all, there is a limit to memory.
The put, get, qsize, full and empty mentioned above are not allowed.
Items may be added or deleted in the queue because of the results returned and the results are used later in the program. On some systems, this method may throw an Notimplementederror exception.
If other processes or threads are adding items to the queue, the results are unreliable. In other words, the results are returned and used
Python full stack development, DAY40 (interprocess communication (queue and pipeline), inter-process data sharing manager, Process Pool)