do something in time interval

Discussion in 'Python' started by Petr Jakes, Oct 6, 2008.

  1. Petr Jakes

    Petr Jakes Guest

    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
    Petr Jakes, Oct 6, 2008
    #1
    1. Advertising

  2. Petr Jakes

    Terry Reedy Guest

    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,func_to_call) pairs
    in a priority queue (heapq module).

    Terry Jan Reedy
    Terry Reedy, Oct 6, 2008
    #2
    1. Advertising

  3. Petr Jakes

    Jerry Hill Guest

    On Mon, Oct 6, 2008 at 2:07 PM, 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.


    Take a look at the sched module.
    http://www.python.org/doc/2.5.2/lib/module-sched.html

    > 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.scheduler(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
    Jerry Hill, Oct 6, 2008
    #3
  4. On Mon, 6 Oct 2008 21:13:21 +0200
    "Petr Jake?" <> wrote:
    > > 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 <> | 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.
    D'Arcy J.M. Cain, Oct 6, 2008
    #4
  5. 2008/10/6 Petr JakeŇ° <>:
    >> 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(data0)

    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
    Mathieu Prevot, Oct 6, 2008
    #5
    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. Dica
    Replies:
    0
    Views:
    408
  2. Wes Harrison

    Measuring a short interval in time

    Wes Harrison, Sep 14, 2004, in forum: Java
    Replies:
    2
    Views:
    578
    Paul Lutus
    Sep 14, 2004
  3. Graeme Longman
    Replies:
    1
    Views:
    301
    Waldemar Osuch
    Aug 4, 2004
  4. Hendrik van Rooyen

    re : do something in time interval

    Hendrik van Rooyen, Oct 8, 2008, in forum: Python
    Replies:
    1
    Views:
    305
    Lawrence D'Oliveiro
    Oct 9, 2008
  5. Andrew Poulos

    'point in time' time interval

    Andrew Poulos, Jun 14, 2005, in forum: Javascript
    Replies:
    4
    Views:
    252
    Andrew Poulos
    Jun 15, 2005
Loading...

Share This Page