473,224 Members | 1,738 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,224 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 7203
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....
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.