call a function every n mseconds

Discussion in 'Python' started by Vedran Furac, Feb 22, 2004.

  1. Vedran Furac

    Vedran Furac Guest

    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()
    Vedran Furac, Feb 22, 2004
    #1
    1. Advertising

  2. > 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
    Josiah Carlson, Feb 22, 2004
    #2
    1. Advertising

  3. Vedran Furac <> wrote in message news:<>...
    > 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
    Michele Simionato, Feb 22, 2004
    #3
  4. On Sat, 21 Feb 2004 17:47:59 -0800, Josiah Carlson
    <> 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
    David Morgenthaler, Feb 22, 2004
    #4
  5. Vedran Furac

    Jonas Galvez Guest

    > [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
    Jonas Galvez, Feb 22, 2004
    #5

  6. > 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
    Josiah Carlson, Feb 22, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,441
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
  2. grbgooglefan
    Replies:
    2
    Views:
    411
    Pascal Bourguignon
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    4
    Views:
    430
    Kenny McCormack
    Jan 30, 2008
  4. grbgooglefan
    Replies:
    0
    Views:
    384
    grbgooglefan
    Jan 30, 2008
  5. Alok
    Replies:
    3
    Views:
    241
Loading...

Share This Page