Stopping a thread from the calling process
Gordon McMillan
gmcm at hypernet.com
Mon Jan 17 08:42:42 EST 2000
More information about the Python-list mailing list
Mon Jan 17 08:42:42 EST 2000
- Previous message (by thread): Proposal: i-String literals in Future Version of Python?
- Next message (by thread): Stopping a thread from the calling process
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
P.J.W.S. Vrijlandt wrote: > Gordon wrote: > > > The normal idiom is to ask politely - have the thread > > periodically check some variable to see if it should stop. > > How would you do this if the thread-to-be-stopped is waiting for > (keyboard-) input? > > e.g. in telnetlib we have: > > def mt_interact(self): > """Multithreaded version of interact().""" > import thread > thread.start_new_thread(self.listener, ()) > while 1: > line = sys.stdin.readline() > if not line: > break > self.write(line) > > def listener(self): > """Helper for mt_interact() -- this executes in the other > thread.""" > while 1: > try: > data = self.read_eager() > except EOFError: > print '*** Connection closed by remote host ***' > return > if data: > sys.stdout.write(data) > else: > sys.stdout.flush() > > mt_interact should break out of its loop when EOFError occurs within > listener. > > (also note that select on input won't work on win32, and looping on > keypressed seems rather processor-unfriendly) Well, select (if it worked on files on WIn32) would be optimal. Using busy-wait is somewhat offensive, but if not msvcrt.kbhit(): time.sleep(0.1) is not going to consume much CPU. If EOF on the socket is the end of the program, I would reverse the purpose of the threads, (have the worker thread read stdin). > BTW: Would this be better rewritten (for this reason or another) > using threading? Glancing at it (I've never used it), it looks like there should probably be a condition protecting use of the socket; so probably yes. - Gordon
- Previous message (by thread): Proposal: i-String literals in Future Version of Python?
- Next message (by thread): Stopping a thread from the calling process
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list