[Tutor] after signal handler

Hameed U. Khan hameed.u.khan at gmail.com
Tue Mar 7 13:48:44 CET 2006


On 3/6/06, Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
>
> On Mon, 6 Mar 2006, Hameed U. Khan wrote:
>
> >  I want to go back after signal handler to the isntruction where I was
> > stuck. Below is the code I'm trying to play with.
>
> Hi Hameed,
>
> Here's a small test program that shows that the signal handling does go
> back to where we left off:

Hi Danny,

Thanks your program really solved one misconception I had for singlas.
That they dont pass control back. Now I've a little more understanding
of signal handlers in python.

> > What I'm trying to do is to implement a prompt with the remaining time
> > they have to enter some input.
>
> This should be doable.  What can we help with?

After your example I realised that I'm following a wrong approach.
Then I decided to use select module. I've almost solved the problem,
but there is still one issue. which I think can't be resolved.
Following is my program which implements prompt with indicator of time
left for user to input. I'll be happy to recieve suggestions and
improvements :)


#!/usr/bin/env python2.4
####### Start program

import sys
import time
import select

def get_input(prompt="Enter input in %d seconds: ",timecount=10):
    tcount = timecount
    while True:
        if tcount == 0:
            print
            break
        print "\r" + prompt % tcount,
        sys.stdout.flush()
        time.sleep(1)
        list = select.select([sys.stdin],[],[],0)
        if list[0] != []:
            input = sys.stdin.readline()
            print
            return (timecount-tcount,input)
        tcount = tcount -1
    print
    return None


print "Input check: "
myin= get_input()
if myin == None:
    print "No input"
else:
    print "You enetered in %d seconds: %s" % myin

######## End program

--
Hameed U. Khan
Registered Linux User #: 354374


More information about the Tutor mailing list