do something in time interval

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Petr Jakes

    do something in time interval

    I have infinitive loop running script and I would like to check
    something periodically after 5 seconds (minutes, hours...) time period
    (I do not mean time.sleep(5) ). Till now, I have following script, but
    I think there must be something more elegant.

    eventFlag = False
    while 1:
    time.sleep(0.01 )
    seconds = time.time()
    if not int(seconds % (5)):
    if eventFlag:
    print "5 seconds, hurray"
    eventFlag = False
    else:
    eventFlag = True

    Best regards

    Petr Jakes
  • Terry Reedy

    #2
    Re: do something in time interval

    Petr Jakes wrote:
    I have infinitive loop running script and I would like to check
    something periodically after 5 seconds (minutes, hours...) time period
    (I do not mean time.sleep(5) ). Till now, I have following script, but
    I think there must be something more elegant.
    >
    eventFlag = False
    while 1:
    time.sleep(0.01 )
    seconds = time.time()
    if not int(seconds % (5)):
    if eventFlag:
    print "5 seconds, hurray"
    eventFlag = False
    else:
    eventFlag = True
    The eventFlag is confusing. Better:

    dt = 5 # for instance
    event_time = time.time()
    while 1:
    #do every loop stuff
    now = time.time()
    if now >= event_time:
    event_time = now + dt
    event()

    If you have multiple delayed events, quite possibly each with a
    different or even variable delta, store (future_time,fu nc_to_call) pairs
    in a priority queue (heapq module).

    Terry Jan Reedy

    Comment

    • Jerry Hill

      #3
      Re: do something in time interval

      On Mon, Oct 6, 2008 at 2:07 PM, Petr Jakes <petr.jakes.tpc @gmail.comwrote :
      I have infinitive loop running script and I would like to check
      something periodically after 5 seconds (minutes, hours...) time period
      (I do not mean time.sleep(5) ). Till now, I have following script, but
      I think there must be something more elegant.
      Take a look at the sched module.

      eventFlag = False
      while 1:
      time.sleep(0.01 )
      seconds = time.time()
      if not int(seconds % (5)):
      if eventFlag:
      print "5 seconds, hurray"
      eventFlag = False
      else:
      eventFlag = True
      Using sched, I think you would re-write this as:

      import sched, time
      s=sched.schedul er(time.time, time.sleep)

      def do_event():
      print "5 seconds, hurray!"
      s.enter(5, 1, do_event, ())

      s.enter(5, 1, do_event, ())
      s.run()

      That will run do_event() after five seconds, and then do_event() puts
      itself back in the queue to be executed in another 5 seconds.

      --
      Jerry

      Comment

      • D'Arcy J.M. Cain

        #4
        Re: do something in time interval

        On Mon, 6 Oct 2008 21:13:21 +0200
        "Petr Jake?" <petr.jakes@tpc .czwrote:
        I am not an expert, but why not to use time.sleep(5)?
        During the 5s period my script has to do some stuff instead of sleeping.
        Thats why it runs in the loop and once in 5s period it has to trigger some
        other stuff(function, method, action) to do.
        Then check out http://www.python.org/doc/current/library/select.html

        --
        D'Arcy J.M. Cain <darcy@druid.ne t | Democracy is three wolves
        http://www.druid.net/darcy/ | and a sheep voting on
        +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

        Comment

        • Mathieu Prevot

          #5
          Re: do something in time interval

          2008/10/6 Petr Jakeš <petr.jakes@tpc .cz>:
          >I am not an expert, but why not to use time.sleep(5)?
          >If you are using wxPython, you may also try wx.Timer, in which you could
          >set its interval.
          >>
          >
          Thanks for your reply.
          During the 5s period my script has to do some stuff instead of sleeping.
          Thats why it runs in the loop and once in 5s period it has to trigger some
          other stuff(function, method, action) to do.
          Petr Jakes
          You might want to use threads so you have a very simple code like this:

          data data0

          thread1:
          while True:
          time.sleep(5)
          doSomething(dat a0)

          thread2:
          while True:
          time.sleep(0.01 )
          doAnotherthing( data0)

          thread1 and thread2 can run in parallel; if thread2 take more time,
          thread1 won't be impaired.

          Mathieu

          Comment

          Working...