call a function every n mseconds

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vedran Furac

    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()
  • Josiah Carlson

    #2
    Re: call a function every n mseconds

    > How can I call a function every time a specified number of milliseconds[color=blue]
    > 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()[/color]

    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(msec s/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

    Comment

    • Michele Simionato

      #3
      Re: call a function every n mseconds

      Vedran Furac <vedranfREMOVE@ riteh.hr> wrote in message news:<slrnc3fue c.8pk.vedranf@r ijeka.riteh.hr> ...[color=blue]
      > 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()[/color]

      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

      Comment

      • David Morgenthaler

        #4
        Re: call a function every n mseconds

        On Sat, 21 Feb 2004 17:47:59 -0800, Josiah Carlson
        <jcarlson@nospa m.uci.edu> wrote:
        [color=blue]
        >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(msec s/1000.0)
        >###end of code
        >[/color]

        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

        Comment

        • Jonas Galvez

          #5
          Re: call a function every n mseconds

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

          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.star t()
          fWrapper.t = Timer(i, fWrapper)
          fWrapper.t.star t()
          return fWrapper

          def clearInterval(t imerRef):
          timerRef.t.canc el()


          Hope it works out for you.


          Jonas





          Comment

          • Josiah Carlson

            #6
            Re: call a function every n mseconds

            [color=blue]
            > 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.[/color]

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

            - Josiah

            Comment

            Working...