python - Thread is not terminating -
Can someone tell why the thread is not finished ..
import System, threading, select class MyMaster (threading.Thread): def __init __ (self, event_stop): super (mimster, self) .__ init __ (name = 'MyMaster') self.event_stop = event_stop def run (self): # User is waiting for the keyboard (self.event_stop.isSet (): # Always waiting on selection if sys.stdin is select.select ([sys.stdin], [], [], 0 ): Ch = sys.stdin.read (1) # Print "You have entered:", repr (ch) if ch == 'a': #do Content Ig elif ch == 'h': #do content elif ch == 'p': #de stuff elif ch == 'R': #do Content other: Print "Breaking ........... ........................... .... "Break Print" done "Def Stop (self): Self. Event_stop.set ( ) Try: event_stop = threading.Event () # master master = MyMaster (event_stop) master.start () is creating event_stop.wait () Master.stop () print threading. Excluding NMNET (): traceback.print_exc ()
You can see the event_stop.wait () in the main thread Are calling, which will be blocked until another thread comes along and Event_stop.set () . However, there is no such other thread - the main thread should eventually do this by calling master.stop () , but not to the next line, which can never be reached. You can just fix it by removing event_stop.wait () call: event_stop = threading.Event () # master master = MyMaster (event_stop) Creating Master Start () time.sleep (2) # Use it to delay a while, if you were going for a guru then Stop () print threading. Enumerate ()
Comments
Post a Comment