After learning the event and condition two thread synchronization tools, there is a tool I think is more chicken semaphores
1. The effect of using semaphores and the condition notify method are basically the same. Only one blocking thread can be notified to continue running at a time
2. Semaphore synchronization is based on internal counters, each call acquire (), the counter minus 1, each call to release (), the Counter plus 1. When the counter is 0 o'clock, the acquire () call is blocked
1 ImportThreading2 Import Time3 4 defCountdown (N, SEMA):5 whilen >0:6N-= 17 Sema.acquire ()8 Print('Current countdown:', N)9 Ten One defcountup (n, SEMA): A whileN < 100: -n + = 1 - Sema.acquire () the Print('Current Countup:', N) - -SEMA =Threading. Semaphore () -Threading. Thread (target=countdown,args= (100, SEMA)). Start () +Threading. Thread (target=countup,args=(0, SEMA)). Start () - forIinchRange (100): + sema.release () ATime.sleep (3)
Python thread Threading (v)