9

I have a Python 2.7 program running an infinite while loop and I want to incorporate a timer interrupt. What I aim to do is to set off a timer at some point in the loop, and when 5 seconds have elapsed I want the code to branch to a specific part of the while loop.

What I have been doing so far is the following: in each iteration of the while loop I am checking how much time has elapsed when I reach that point in the code using

time.clock()

and if the difference exceeds 5 I run the chunk of code I mean to run

However that way 7 seconds might pass before I evaluate the time, it will be >5sec but I want to go there exactly when 5 seconds pass

Also, I need this to work for more than 1 counter (possibly up to a 100) but I do not want the interrupts to interrupt each other. Using Timer did not work either.

I know this can be done using timer interrupts in assembly but how can I do that in python?

Stack Player
  • 1,470
  • 2
  • 18
  • 32
  • 2
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See [Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – rene Apr 19 '14 at 13:00

2 Answers2

7

If a single event is to be handled, then the easiest way is to use the signal framework which is a standard module of Python.

However, if we need a fully-fledged scheduler, then we have to resort to another module: sched. Here is a pointer to the official documentation. Please be aware, though, that in multi-threaded environments sched has limitations with respect to thread-safety.

Another option is the Advanced Python Scheduler, which is not part of the standard distribution.

Roberto Reale
  • 4,247
  • 1
  • 17
  • 21
  • From my very limited experience with python what I understood is that signal or timer can be used for 1 event, how could I make a dynamic array of such events? this is turning out to be troublesome – Stack Player Apr 19 '14 at 13:04
  • So I am sending frames, and I want a timer for each frame, if a frame does not get acknowledged before *ITS OWN* timer sets off then when the timer does set off I want to resend that frame, and if timer2 sets off while I am sending a frame it has to wait, no interrupting except in specified places in the code – Stack Player Apr 19 '14 at 13:12
0

You can't get real-time without special hardware/software support. You don't need it in most cases (do you need to control giant robots?).

How to delay several function calls by known numbers of seconds depends on your needs e.g., if a time it takes to run a function is negligible compared to the delay between the calls then you could run all functions in a single thread:

#!/usr/bin/env python
from __future__ import print_function
from Tkinter import Tk

root = Tk()
root.withdraw() # don't show the GUI window
root.after(1000, print, 'foo') # print foo in a second
root.after(0, print, 'bar') # print bar in a jiffy
root.after(2000, root.destroy) # exit mainloop in 2 seconds
root.mainloop()
print("done")

It implements yours "I do not want the interrupts to interrupt each other either" because the next callback is not called until the previous one is complete.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670