What is the Python Semaphore (semaphore)? (example)

Source: Internet
Author: User
Tags semaphore
In the following article we will look at what is Semaphore (semaphore) in Python. Learn about Python semaphores and how Semaphore (semaphores) can be used in Python programming.

Semaphore (semaphore)

mutexes allow only one thread to change data at the same time, while Semaphore allows a certain number of threads to change data, such as a toilet with 3 pits, which allows up to 3 people to go to the toilet, while the latter can only wait for someone to come out.

import Threading,timedef Run (n): Semaphore.acquire () time.sleep (1) print ("Ru n the thread:%s\n "%n) semaphore.release () if __name__ = = ' __main__ ': num= 0 semaphore = Threading. Boundedsemaphore (5) #最多允许5个线程同时运行 for I in range: T = Threading. Thread (target=run,args= (i,)) T.start () while Threading.active_count ()! = 1:pass #print threading.active_count () E Lse:print ('----All threads do---') print (num) 

Events Inter-thread communication

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 touches.

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.

Use of events

event = Threading. Event ()

Event.wait ()

The event object Wait method only executes quickly and completes the return if the internal signal is true. When an event object's internal signal flag is false, the wait method waits until it is true to return.

Event.set ()

Use the Set () method of the event to set the signal token inside the event object to be true. The event object provides the Isset () method to determine the state of its internal signal flag. When you use the Event object's set () method, the IsSet () method returns the true

Event.clear ()

use the Clear () method of the event object to clear the signal flag inside the event object and set it to false, and when the clear method of the event is used, the IsSet () method returns false .

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.