First, the Event
1. Why is there an event?
One of the key features of a thread is that each thread runs independently and the state is unpredictable. If a thread in a program needs to determine whether a program in its own thread needs to be executed by the state of another thread, the event is created.
2. What is the role of event?
The event object in the threading library determines whether the thread that waits for the event object is awakened by judging the event object in its own thread, and the event object contains a signal flag that can be set by the thread, by default the signal is False, If there is another thread waiting for the event object, the waiting thread will remain blocked until the event object is true when it is false. If this signal is true, then other threads will ignore it and continue executing the program.
3. How to use the event
Event=threading. Event () #实例化产生一个Event的对象
Event.isset (): #返回event的状态值;
Event.wait (): #如果 event.isset () = =false to block threads;
Event.set (): #设置event的状态值为True, the thread of all blocking pools is activated into a ready state, waiting for the operating system to dispatch;
Event.clear (): #恢复event的状态值为False, by default, is flase.
4.
Thread 1 Thread 2
Note: In multiple threads, the state of Event.isset () in thread 1 will be judged when event.wait () is present, and if true, thread 1 continues to execute the program, and if flase, thread 1 waits for thread 2 to Event.set () The state is set to true before proceeding with the execution of the program.
Whether thread 1 can finish executing the program depends on whether the event object for thread 2 is true
5. Example:
6. The Event.wait () method also supports adding a time-out parameter, the default thread waits for Event.isser () to be true when the following code is executed, but when the timeout parameter is set, the following code is automatically executed when the thread waits longer than this time.
Second, the queue
1. What is a queue?
The data is processed in an orderly manner, which is equivalent to processing the data sequentially.
2. Get () and put () methods
1.get () is the sending of data, put () is to receive data, you can instantiate the object to set the length of the message queue, that is, queue. Queue (100).
2. Example:
Note: This method is based on the first-in-one-out mechanism to process the data, that is, the advanced data is now being processed.
2. Join () and Task_done ()
1.
2. Other uses
Q.qsize () returns the size of the queue Q.empty () returns True if the queue is empty, or false instead
Q.full () returns True if the queue is full, otherwise false
Q.full corresponds to maxsize size
Q.get ([block[, timeout]]) Get queue, timeout wait time
Q.get_nowait () quite q.get (False) non-blocking
Q.put (item) write queue, timeout wait time
Q.put_nowait (item) quite Q.put (item, False)
Q.task_done () After completing a work,
The Q.task_done () function sends a signal to the queue that the task has completed
Q.join () actually means waiting until the queue is empty before performing another operation
3. Other modes
1, the Python queue module FIFO queuing first-out. Class queue. Queue (maxsize)
2, LIFO similar to the heap, that is, advanced after out. Class queue. Lifoqueue (maxsize)
3, there is a priority queue level lower the more first out. Class queue. Priorityqueue (maxsize)
37th Day of walking into the computer (advanced usage of Python)