By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

call a function every n mseconds

P: n/a
How can I call a function every time a specified number of milliseconds
elapses? Javascript has setInterval() function and I need something like
that. Currently I use this:
def function():
[...]
t = threading.Timer(n, function)
t.start()
function()
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> How can I call a function every time a specified number of milliseconds
elapses? Javascript has setInterval() function and I need something like
that. Currently I use this:
def function():
[...]
t = threading.Timer(n, function)
t.start()
function()


I guess it all depends on what you are doing. If you didn't need to do
anything else, and didn't want to deal with threads:

###beginning of code
import time

def runevery(msecs, function, args=(,), kwargs={}):
while 1:
function(*args, **kwargs)
time.sleep(msecs/1000.0)
###end of code

If you have other things to do:

###beginning of code
import time

timer = [time.time()]
def runevery(msecs, function, args=(,), kwargs={}):
global timer
if time.time() >= timer[0]:
timer[0] += msecs/1000.0
function(*args, **kwargs)

#call runfunct in some sort of mainloop like so:

while 1:
asyncore.poll(1) #or something like that
runevery(delay, function, args, kwargs)
###end of code

I hope this helps,
- Josiah
Jul 18 '05 #2

P: n/a
Vedran Furac <ve***********@riteh.hr> wrote in message news:<sl********************@rijeka.riteh.hr>...
How can I call a function every time a specified number of milliseconds
elapses? Javascript has setInterval() function and I need something like
that. Currently I use this:
def function():
[...]
t = threading.Timer(n, function)
t.start()
function()


You can fork the process and use time.sleep. Or you can use Tkinter
(hint: Tkinter also works for non-graphical applications): Tkinter
widgets have an "after" method that does what you want.

Michele Simionnato
Jul 18 '05 #3

P: n/a
On Sat, 21 Feb 2004 17:47:59 -0800, Josiah Carlson
<jc******@nospam.uci.edu> wrote:
I guess it all depends on what you are doing. If you didn't need to do
anything else, and didn't want to deal with threads:

###beginning of code
import time

def runevery(msecs, function, args=(,), kwargs={}):
while 1:
function(*args, **kwargs)
time.sleep(msecs/1000.0)
###end of code


If you use wxPython, you might want to use the wxTimer, which does
exactly what you're looking for.

If you use time.sleep, as in the example above, and the function takes
some amount of time, say deltaT, then your loop actually executes
every msecs+deltaT. This may or may not be an issue for you.

dave

Jul 18 '05 #4

P: n/a
> [Vedran Furac]
How can I call a function every time a specified number of
milliseconds elapses? Javascript has setInterval() function and I
need something like that.


Funny, yesterday I was looking for exactly the same thing. I ended up
creating a small module with a somewhat hackish implementation of
setInterval and clearInterval, but it's been OK for my needs:
from threading import Timer

def setInterval(f, i, *params):
def fWrapper():
apply(f, params)
fWrapper.t = Timer(i, fWrapper)
fWrapper.t.start()
fWrapper.t = Timer(i, fWrapper)
fWrapper.t.start()
return fWrapper

def clearInterval(timerRef):
timerRef.t.cancel()
Hope it works out for you.
Jonas

Jul 18 '05 #5

P: n/a
If you use time.sleep, as in the example above, and the function takes
some amount of time, say deltaT, then your loop actually executes
every msecs+deltaT. This may or may not be an issue for you.


Vedran wasn't worried about the delta, so I didn't concern myself with
it either.

- Josiah
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.