Get the App
SLTechnology News&Howtos  ›  Development  › 

The concrete Operation Scheme of Event in Python Library

Shulou Source: shulou.com Published: 2022-06-03 14:53:43 09月30日 Update

This article mainly explains the "specific operation plan of Event in Python Library". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the specific operation plan of Event in Python Library".

This corresponds to the .NET ManualResetEvent and is used to collaborate 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 ()

Wait for the signal

Print currentThread () .name, I event1.clear ()

When the execution is complete, clear the flag (flag) so that the next time you wait (), you need to wait for notification again.

Event2.set ()

Set another wait event to signal 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, or they'll all be there waiting to die. 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 Thank you for reading, the above is the content of "the specific operation plan of Event in Python Library". After the study of this article, I believe you have a deeper understanding of the specific operation plan of Event in Python Library, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Signals schemes threads learning events content again multiple that is ideas situations articles more tags activation knowledge knowledge points articles follow questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno vpn Xiaomi NVidia