call a function every n mseconds

David David
Sun Feb 22 10:56:19 EST 2004


On Sat, 21 Feb 2004 17:47:59 -0800, Josiah Carlson
<jcarlson at 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




More information about the Python-list mailing list