473,396 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

call a function every n mseconds

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
5 5738
> 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
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
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
> [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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Michael | last post by:
Hello everybody, Newbie here. I've been working on this for the last two days and I can't figure out where this problem is. I think it's something so obvious, but I can't see it! OK, firstly...
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
19
by: Riko Wichmann | last post by:
hi everyone, I'm googeling since some time, but can't find an answer - maybe because the answer is 'No!'. Can I call a function in python inline, so that the python byte compiler does...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
9
by: yndygo | last post by:
Alright then - I know it's got to be out there somewhere, but I can't find it... I'm looking at a piece of code and there's some debate as to what the behavior will be. Given a switch/case...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
4
by: faraz | last post by:
I have a javascript function that i want to call masterpage load method called. I cant add onLoad event of body or table in master page. I have put the call to script function in <form>, it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.