Canceling/interrupting raw_input

Daniel Cer Daniel.Cer at gmail.com
Mon Apr 18 03:02:15 EDT 2005


Just a little bit of a follow up on this...

If you use win32api.TerminateProcess() instead of sys.exit(), everything 
works as it should on Windows. That is, there is no longer a need to hit 
'enter' one last time in order to get "inputLoop" to terminate.

So, modifying the sample code I posted earlier, the new Win32 specific 
code would be the following:

import thread
import time
import sys
import win32api

def inputLoop():
      while 1:
              input_string = raw_input("Type something: ")
              print "You entered: ", input_string

thread.start_new_thread(inputLoop, () )
time.sleep(3)
print "\nTime's up exiting...."
win32api.TerminateProcess(-1, 0)

-Dan

Daniel Cer wrote:
> For what it's worth, this looks like a Windows specific problem.
> 
> The code below seems to work as expected on a Linux box. That is, 
> everything terminates, including the "inputLoop", after sys.exit() is 
> called, without the user needing to press 'enter' one last time.
> 
> However, if I try to run the code on Windows XP, it exhibits the exact 
> same behavior you described.
> 
> #!/usr/bin/python
> 
> import thread
> import time
> import sys
> 
> def inputLoop():
>      while 1:
>              input_string = raw_input("Type something: ")
>              print "You entered: ", input_string
> 
> thread.start_new_thread(inputLoop, () )
> 
> time.sleep(15)
> 
> sys.exit()
> 
> -Dan
> 
> 
> J. W. McCall wrote:
> 
>> I'm working on a MUD server and I have a thread that gets keyboard 
>> input so that you can enter commands from the command line while it's 
>> in its main server loop.  Everything works fine except that if a 
>> player enters the 'shutdown' command, everything shuts down, but the 
>> input thread is still sitting waiting for enter to be pressed for 
>> raw_input.  After enter is pressed, it exits back to the command 
>> prompt as it should.
>>
>> I'm wondering if there's a way that I can make the thread stop waiting 
>> for input.  Even sys.exit() still leaves it waiting.  It's not a big 
>> deal, but it bugs me.
>>
>> Any ideas?  Should I be using something other than raw_input?  I'm on 
>> Windows2000 and running this from the DOS prompt.  I'm using Python 2.4.



More information about the Python-list mailing list