call a function every n mseconds

V

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. Currently I use this:
def function():
[...]
t = threading.Timer(n, function)
t.start()
function()
 
J

Josiah Carlson

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
 
M

Michele Simionato

Vedran Furac said:
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
 
D

David Morgenthaler

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
 
J

Jonas Galvez

[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
 
J

Josiah Carlson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top