473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Canceling/interrupting raw_input

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.
Jul 19 '05 #1
2 7213
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.

Jul 19 '05 #2
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.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
1
by: JerryKreps | last post by:
Hi, folks -- I'm a Python pup. As you can see from the session copied at the end of this post, I have the latest version of Python, and I've been using the Editor-Shell of the latest version of...
0
by: rcarmich | last post by:
I am having an issue canceling a beginReceive call on a timeout. For example, the following function: public int ReadBytes(Socket theSock, byte arr, int startByte, int length, int timeout) {...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.