The Python Library computer language is widely used in practical applications, and few people will involve the specific application of the Event specific operation solution in the Python Library, the following is a detailed description of the relevant steps in actual operations.
This corresponds to. NET ManualResetEvent and is used for collaborative operations among multiple threads. Event. wait () waits for the Event signal to continue execution. set () sets the signal so that the waiting thread can execute and clear () clears the signal.
- event1 = Event()
- event2 = Event()
- def test1():
- for i in range(5):
- event1.wait()
WAITING SIGNAL
- print currentThread().name, i
- event1.clear()
After the command is executed, clear the flag so that you need to wait for the notification again at the next wait.
- event2.set()
Set another wait event to send signals to another thread.
- def test2():
- for i in range(5):
- event2.wait()
- print currentThread().name, i
- event2.clear()
- event1.set()
- Thread(target = test1).start()
- Thread(target = test2).start()
- event1.set()
Remember to activate one first, otherwise it will all be "waiting for death. Output:
- $ ./main.py
- Thread-1 0
- Thread-2 0
- Thread-1 1
- Thread-2 1
- Thread-1 2
- Thread-2 2
- Thread-1 3
- Thread-2 3
- Thread-1 4
- Thread-2 4
The above article introduces the actual operation scheme of Event in Python Library.