Using OS time

valhalla tireseas at onetel.com
Fri Jun 20 13:04:06 EDT 2003


Hmmm - I'm not at all familiar with signal.alarm() nor how to use it Skip.

After googling for info on this, I found this at 
http://www.linuxjournal.com/modules.php?op=modload&name=NS-lj-issues/issue73&file=3946l5

---begin code---------

Listing 5. Using Alarm Signal

#! /usr/local/bin/python
import signal
import sys
def alarm_handler(signal, frame):
    print 'BOOM!'
    sys.exit(0)
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(5) # generate SIGALRM after 5 secs
n = 0
while 1:
    print n
    n = n+1
-----end code------

This seems to be the closest to what you describe. I'll play around with this 
and see what it does.

I suppose a lengthier but more n00bie friendly way of doing this 'countdown' 
would be to countdown from a given number (e.g. 60) and import time.sleep to 
sleep for 1 second at each new iteration in the for loop. I think this would 
do the same thing.

Anyway, I'll fool around with both of these options and see which one I find 
easier to work with.

Thanks Skip.

Andy



On Friday 20 Jun 2003 17:07, Skip Montanaro wrote:

> How about signal.alarm()?
>
>     alarm(...)
>         alarm(seconds)
>
>         Arrange for SIGALRM to arrive after the given number of seconds.
>
> You will obviously have to have a signal handler set up to recover from the
> signal.  You'll have to experiment to see if the signal will terminate the
> call to raw_input() (or whatever you're using to gather user input).
>
> Skip

-- 
====================
||  Reg. Linux User: 313143 ||
====================
Sign the Petition:
http://www.PetitionOnline.com/endtcpa1/





More information about the Python-list mailing list