In many cases, there will need to communicate with each other between the threads. Common scenario is a secondary thread to perform a specific task as the main threads in the implementation process need to continue to report on the progress of implementation. Foregoing condition variable synchronization has been related to a communication (threading.Condition the notify method) between threads. More common way is to use threading.Event object.
threading.Event can make a thread other threads waiting to be notified. Which is a built-flag initial value False. By thread wait () method into a wait state until another thread calls the set () method of the built-flag is set to True, Event notification status of all threads waiting to resume operation. May also be () method to query the current value of the built-in state by the object Envent isSet.
import threading
import random
import time
class MyThread(threading.Thread):
def __init__(self,threadName,event):
threading.Thread.__init__(self,name=threadName)
self.threadEvent = event
def run(self):
print "%s is ready" % self.name
self.threadEvent.wait()
print "%s run!" % self.name
sinal = threading.Event()
for i in range(10):
t = MyThread(str(i),sinal)
t.start()
sinal.set()
Reproduced in: https: //www.cnblogs.com/linbinqiang/p/5329127.html