Python Basic Learning Log day9--thread event

Source: Internet
Author: User

One: Thread event function

Python provides the event object for inter-thread communication, which is a signal flag set by the thread, and if the signal flag bit is true, other threads wait until the signal ends.

The event object implements a simple threading mechanism that provides a set of signals, clear signals, waits, and so on to enable communication between threads.

 Official explanation

An event is a simple synchronization object, and an event represents an internal flag and thread. You can wait for the flag to set, or to set or clear the flag itself.

II: Event Use

1) event = Threading. Event () Declaration event Instance
2) event.wait (). Thread set Wait flag
3) Event.set () If the flag is set, wait for the method to take no action
4) Event.clear () If the flag is cleared, the wait will block until it is set again.

Any number of threads can wait for the same event.

Three: Sample code

Through the event to realize the interaction between two or more threads, here is an example of a traffic light, that is, start a thread to do traffic lights, generate a few threads to do the vehicle, the vehicle driving by red light stop, Green Line rules.

  

Python Basic Learning Log day9--thread event

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.