signal.alarm() for less than one second?

Andrei Kulakov ak at silmarill.org
Sat Jul 6 15:28:39 EDT 2002


Hello python folks,

It looks like signal.alarm takes 1+ seconds as argument. I need
something lower, like 0.5 second or so.

I'm making a text user interface where you have to pick an item from a
list by typing its number. Normally you'd have a raw_input() call but
in this case it's a UI for picking tracks from a list in an mp3 player
- something you may want to do many times in a row and I want to save a
one keypress for each command.

You see a list of 15 songs and you type in '13'. It goes to song #13
immediately. Then you type in '1' - it waits for ~.5 sec waiting for
further input, then goes to 1st song if none given.

SIGALRM seemed perfect for this, here's the way I put it together:


from signal import *
import avk_util

t = avk_util.Term()
s = ""

Complete = "Got the num!"

def handler(signum, frame):
    if s:
        print "there is s!"
        raise Complete

try:
    while 1:
        signal(SIGALRM, handler)
        alarm(1)
        a = ""
        try:
            a = t.getch()
        except OSError:
            pass
        alarm(0)
        s += a
        print "s is", s
except Complete:
    print "GOT RESULT: '%s'"% s


getch() here gets one number at a time (using my own util module
avk_util).

Here's the rub: if I put alarm(0.5) in here, it won't work at all.

What else can I do here? Use threads?

Thanks,

 - Andrei

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org



More information about the Python-list mailing list