[Tutor] Timers in Python

Rikard Bosnjakovic rikard.bosnjakovic at gmail.com
Thu Oct 4 13:18:20 CEST 2007


On 04/10/2007, Kamal <poolmasterji at yahoo.com> wrote:

> Basically, I want to call functionOne() every x minutes, and wondering
> whats the best way to do it.

If you need to run the functions concurrently, use threads. Else you
can setup a simple signal-handler for SIGALRM and set the time
accordingly:


import signal

def setup_signal():
  # Wait 5 seconds before alarming
  signal.alarm(5)
  signal.signal(signal.SIGALRM, signal_handler)

def signal_handler(signum, frame):
  print "I got an alarm!"

  # need to resetup the signal
  setup_signal()


setup_signal()
# A pointless loop
for x in xrange(500000000):
  if (x % 100000) == 0:
    print x



-- 
- Rikard - http://bos.hack.org/cv/


More information about the Tutor mailing list