A signal volume
Semaphore is also a lock, you can specify a signal volume of 5, compared to a mutex lock can only have a task at the same time to grab the lock to execute, the signal volume at the same time can have 5 tasks to get the lock to execute, if the mutex is a house to rob a toilet, then the signal volume is the equivalent of a group of passers-by to Public toilets are all multiple pit positions, which means that at the same time there can be more than one person on the public toilet, but the number of public toilets accommodated is certain, which is the size of the semaphore
From threading import Thread, Semaphoreimport threadingimport timedef func (): sm.acquire () print ('%s get sm '% th Reading.current_thread (). GetName ()) Time.sleep (3) sm.release () if __name__ = = ' __main__ ': sm = Semaphore (5) For I in range: t = Thread (target=func) T.start ()
Analytical
Semaphore manages a built-in counter whenever a built-in counter called Acquire () 1 calls release () the built-in counter +1 counter cannot be less than 0, and when the counter is 0 o'clock, acquire () will block the thread until other threads call rel ()
Second, the Event
A key feature of a thread is that each thread is run independently and the state is unpredictable. Thread synchronization problems can become tricky if other threads in the program need to determine the state of a thread to decide what to do next. To solve these problems, we need to use the event object in the threading library. The object contains a signal flag that can be set by the thread, which allows the thread to wait for certain events to occur. In the initial case, the signal flag in the event object is set to False. If the thread waits for an event object, and the flag of the event object is false, then the threads will be blocked until the flag is true. A thread if the signal flag of an event object is set to true, it will wake up all the threads waiting for the event object. If a thread waits for an event object that has already been set to true, it ignores the event and continues execution
From threading Import Eventevent.isset () # Returns the status value of the Event event.wait () # if Event.isset () ==false will block the thread; Event.set ( # To set the status value of event to true, all blocking pool threads are activated into a ready state, waiting for the operating system to dispatch event.clear () # Recovery event with a status value of False
For example: There are multiple worker threads trying to link MySQL, we want to make sure that the MySQL service is OK before linking to the MySQL server, and if the connection is unsuccessful, try to reconnect. So
From threading import Thread, Eventimport threadingimport time, Randomdef conn_mysql (): count = 1 and not event. Is_set (): If Count > 3: raise Timeouterror (' link timeout ') print (' <%s>%s attempts link '% (threading.current_ Thread (). GetName (), count) event.wait (0.5) count + = 1 print (' <%s> link succeeded '% Threading.current_ Thread (). GetName ()) def check_mysql (): print (' \033[45m[%s] checking mysql\033[0m '% threading.current_thread (). GetName ()) Time.sleep (Random.randint (2, 4) event.set () if __name__ = = ' __main__ ': event = event () conn1 = Thread (target=conn_mysql) conn2 = Thread (target=conn_mysql) check = Thread (target=check_ MySQL) conn1.start () Conn2.start () Check.start ()
Three timers
Timer, specifying n seconds after an action is performed
From threading Import Timerdef hello (): print (' Hello, world ') t = Timer (1, hello) t.start ()
Python concurrent Programming: Multi-threading-semaphore, Event, timer