Python thread Threading (i) http://www.cnblogs.com/someoneHan/p/6204640.html
Python thread Threading (ii) http://www.cnblogs.com/someoneHan/p/6209240.html
Use Threading. Thread.is_alive () This method can determine whether a thread is alive. But on an existing basis it is not possible until when the thread starts, when it ends, and when it is interrupted.
If one or more threads need to be on another thread to reach a certain point, do the following. You can use threading at this time. The event object.
The event object is similar to a conditional token, allowing the thread to wait for an event to occur, and if the event is not set and the thread is posted, the thread is blocked until the event has been set. When the thread sets this event, it wakes up all the waiting threads.
1 fromThreadingImportThread, Event2 Import Time3 4 5 defCountdown (N, start_evt):6 start_evt.wait ()7 Print('Countdown Start')8 whilen > 1:9 Print('T-minus', N)TenN-= 1 OneTime.sleep (3) A - defcountup (n, start_evt): - start_evt.wait () the Print('Countup Start') - whileN < 100: - Print('T-minus', N) -n + = 1 +Time.sleep (3) - + A #Create The event object that would be a used to signal starting atSTART_EVT =Event () - - Print('Launching Countdown') -t = Thread (Target=countdown, args= (10, start_evt)) - T.start () -T2 = Thread (Target=countup, args= (1, start_evt)) in T2.start () - Print('threads to started') toStart_evt.set ()
View Code
The result of the code operation is:
Threads to Started
Countdown start
T-minus 10
Countup start
Threads to started runs before Countdown start and Countup start. Because Countdown and Countup two threads set the wait state for event events, they will not start running when the event is not triggered.
Attention:
The event is best used only for one-time events. Because once the event object is set, it will be discarded and may cause problems if you continue to use it. If you want to repeatedly notify an event, you can use condition
The key feature of the event object is to wake up the waiting thread if you only want to wake up a waiting thread that can use semaphore or condition
Python thread Threading (iii)