MainThread blocks all others

Bryan Olson fakeaddress at nowhere.org
Wed Aug 10 09:12:34 EDT 2005


Nodir Gulyamov wrote:
 > Tried to rewrite using Event() just as experiment, but unfortunately
 > MainThread will blocking all others.
 > Code below:
 >
 > import threading
 >
 > class class1:
 >     def __init__(self):
 >         self.myEvent = threading.Event()
 >         result = doSomeJob()
 >
 >     def increaseCounter(self):
 >         self.myEvent.set()
 >
 >     doSomeJob(self):
 >         ##### BLOCKING HERE ###
 >         if not self.myEvent.isSet():
 >             self.myEvent.wait()

The initial 'if' is superflous.


 >         self.myEvent.clear()
 >         # ... continue...
 >
 > # this class subscribed to some observer which implements thread
 > class monitor:
 >     def __init__(self, klass):
 >         #do some init
 >         self.c = klass
 >     def update(self):
 >         self.c.increaseCounter()
 >
 > if __name__ == "__main__":
 >     cl1 = class1()
 >     m = monitor(cl1)
 >     mo = MonitorObserver(m)

Obviously that won't work. You only have one thread, and it blocks
in doSomeJob, so no one can ever trigger the even.


-- 
--Bryan



More information about the Python-list mailing list