Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to timeout when waiting for raw_input from user ?

Reply
Thread Tools

How to timeout when waiting for raw_input from user ?

 
 
northof40
Guest
Posts: n/a
 
      12-04-2009
Hi - I'm writing a *very* simple program for my kids. It asks the user
to give it the answer to a maths question and says "right" or "wrong"

They now want a timed version where they would only get so long to
respond to the question.

I'm thinking of some logic where a raw_input call is executed and then
if more than X seconds elapses before the prompt is replied to the
process writes a message "Sorry too slow" (or similar).

I can't see the wood for the trees here - what's the best way to do
this given the rather simple environment it's needed within.

Regards

richard.

 
Reply With Quote
 
 
 
 
northof40
Guest
Posts: n/a
 
      12-04-2009
On Dec 5, 12:52*pm, northof40 <shearich...@gmail.com> wrote:
> Hi - I'm writing a *very* simple program for my kids. It asks the user
> to give it the answer to a maths question and says "right" or "wrong"
>
> They now want a timed version where they would only get so long to
> respond to the question.
>
> I'm thinking of some logic where a raw_input call is executed and then
> if more than X seconds elapses before the prompt is replied to the
> process writes a message "Sorry too slow" (or similar).
>
> I can't see the wood for the trees here - what's the best way to do
> this given the rather simple environment it's needed within.
>
> Regards
>
> richard.


Sorry I should said that based upon other answers I've seen to similar
questions this needs to run on a windows machine (other answers
suggest this is more difficult than running on *nix)

 
Reply With Quote
 
 
 
 
Maxim Khitrov
Guest
Posts: n/a
 
      12-05-2009
On Fri, Dec 4, 2009 at 6:55 PM, northof40 <> wrote:
> On Dec 5, 12:52Â*pm, northof40 <shearich...@gmail.com> wrote:
>> Hi - I'm writing a *very* simple program for my kids. It asks the user
>> to give it the answer to a maths question and says "right" or "wrong"
>>
>> They now want a timed version where they would only get so long to
>> respond to the question.
>>
>> I'm thinking of some logic where a raw_input call is executed and then
>> if more than X seconds elapses before the prompt is replied to the
>> process writes a message "Sorry too slow" (or similar).
>>
>> I can't see the wood for the trees here - what's the best way to do
>> this given the rather simple environment it's needed within.
>>
>> Regards
>>
>> richard.

>
> Sorry I should said that based upon other answers I've seen to similar
> questions this needs to run on a windows machine (other answers
> suggest this is more difficult than running on *nix)
>


Simplest solution I could come up with. This is indeed much easier on
*nix (just use select.select on sys.stdin with a timeout).

---
from msvcrt import getch, kbhit, putch
from time import sleep, time

ans = ''
end = time() + 5

print('2 + 2 = ?')

while True:
while time() < end:
if kbhit():
break
else:
sleep(0.001)
else:
ans = None
break

char = getch()
if char == '\r':
print('')
break
ans += char
putch(char)

if ans is None:
print('\nSorry too slow')
else:
try:
print('right' if int(ans) == 4 else 'wrong')
except:
print('not a number')
---

- Max
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      12-05-2009
northof40 <> writes:
> I'm thinking of some logic where a raw_input call is executed and then
> if more than X seconds elapses before the prompt is replied to the
> process writes a message "Sorry too slow" (or similar).


The simplest way to do this is with the alarm function and a signal
handler. See the docs for the signal module.

 
Reply With Quote
 
northof40
Guest
Posts: n/a
 
      12-05-2009
On Dec 5, 2:44*pm, Maxim Khitrov <mkhit...@gmail.com> wrote:
> On Fri, Dec 4, 2009 at 6:55 PM, northof40 <shearich...@gmail.com> wrote:
> > On Dec 5, 12:52*pm, northof40 <shearich...@gmail.com> wrote:
> >> Hi - I'm writing a *very* simple program for my kids. It asks the user
> >> to give it the answer to a maths question and says "right" or "wrong"

>
> >> They now want a timed version where they would only get so long to
> >> respond to the question.

>
> >> I'm thinking of some logic where a raw_input call is executed and then
> >> if more than X seconds elapses before the prompt is replied to the
> >> process writes a message "Sorry too slow" (or similar).

>
> >> I can't see the wood for the trees here - what's the best way to do
> >> this given the rather simple environment it's needed within.

>
> >> Regards

>
> >> richard.

>
> > Sorry I should said that based upon other answers I've seen to similar
> > questions this needs to run on a windows machine (other answers
> > suggest this is more difficult than running on *nix)

>
> Simplest solution I could come up with. This is indeed much easier on
> *nix (just use select.select on sys.stdin with a timeout).
>
> ---
> from msvcrt import getch, kbhit, putch
> from time import sleep, time
>
> ans = ''
> end = time() + 5
>
> print('2 + 2 = ?')
>
> while True:
> * * * * while time() < end:
> * * * * * * * * if kbhit():
> * * * * * * * * * * * * break
> * * * * * * * * else:
> * * * * * * * * * * * * sleep(0.001)
> * * * * else:
> * * * * * * * * ans = None
> * * * * * * * * break
>
> * * * * char = getch()
> * * * * if char == '\r':
> * * * * * * * * print('')
> * * * * * * * * break
> * * * * ans += char
> * * * * putch(char)
>
> if ans is None:
> * * * * print('\nSorry too slow')
> else:
> * * * * try:
> * * * * * * * * print('right' if int(ans) == 4 else 'wrong')
> * * * * except:
> * * * * * * * * print('not a number')
> ---
>
> - Max


That's really great - thanks. I've never looked at the whole msvcrt
module before - it looks like it could be useful ... at least for
windows programmes.

Thanks again.

R.

 
Reply With Quote
 
northof40
Guest
Posts: n/a
 
      12-05-2009
On Dec 5, 6:23*pm, Paul Rubin <no.em...@nospam.invalid> wrote:
> northof40 <shearich...@gmail.com> writes:
> > I'm thinking of some logic where a raw_input call is executed and then
> > if more than X seconds elapses before the prompt is replied to the
> > process writes a message "Sorry too slow" (or similar).

>
> The simplest way to do this is with the alarm function and a signal
> handler. *See the docs for the signal module.


Hi Paul - Thanks for your reply. Unfortunately it seems like the bit
of the signal module I would need for this is not implemented for
windows (AttributeError: 'module' object has no attribute 'SIGALRM').
Still no matter when I asked the question I couldn't even figure out
what module might provide this functionality for any platform so it
was useful knowledge for the future. Thanks again.

regards

Richard.
 
Reply With Quote
 
Rune Strand
Guest
Posts: n/a
 
      12-05-2009
The easiest wasy is to use the Timer object in the threading module.


from threading import Timer

 
Reply With Quote
 
Maxim Khitrov
Guest
Posts: n/a
 
      12-05-2009
On Sat, Dec 5, 2009 at 9:01 AM, Rune Strand <> wrote:
> The easiest wasy is to use the Timer object in the threading module.
>
>
> from threading import Timer


Doesn't work on Windows.

- Max
 
Reply With Quote
 
Rune Strand
Guest
Posts: n/a
 
      12-05-2009
On Dec 5, 3:07*pm, Maxim Khitrov <mkhit...@gmail.com> wrote:
>
> Doesn't work on Windows.
>
> - Max


Yes, it does. I've used it a lot, also in Py2Exe apps. Try the
documentation example yourself

def hello():
print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed


 
Reply With Quote
 
Maxim Khitrov
Guest
Posts: n/a
 
      12-05-2009
On Sat, Dec 5, 2009 at 9:11 AM, Rune Strand <> wrote:
> On Dec 5, 3:07Â*pm, Maxim Khitrov <mkhit...@gmail.com> wrote:
>>
>> Doesn't work on Windows.
>>
>> - Max

>
> Yes, it does. I've used it a lot, also in Py2Exe apps. Â*Try the
> documentation example yourself
>
> def hello():
> Â* Â*print "hello, world"
>
> t = Timer(30.0, hello)
> t.start() # after 30 seconds, "hello, world" will be printed


I'm not talking about the Timer, I'm talking about the original
question. There's nothing (that I know of) you can do with a Timer on
Windows to interrupt a raw_input call.

- Max
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
using input(), raw_input() to allow user to run different functions rhvonlehe@gmail.com Python 3 06-30-2009 04:01 PM
Timeout waiting for an answer in TCPClient QDL ASP .Net 0 12-13-2006 10:20 AM
"Timeout while waiting for connection" caocheng C++ 0 04-09-2006 02:35 AM
Timeout::timeout and Socket timeout Mark Probert Ruby 1 10-06-2004 09:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57