do "some action" once a minute

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

    #1

    do "some action" once a minute

    I would like to do "some action" once a minute. My code (below) works,
    I just wonder if there is some more pythonic approach or some "trick"
    how to do it differently.

    minutes=time.lo caltime()[4]
    while 1:
    min, sec = time.localtime( )[4:6]
    if sec==0 and minutes!=min: # first occur of sec==0 only!! polling
    10x a second
    minutes=min
    print "Eureca"
    time.sleep(0.1)

    Regards

    Petr Jakes

  • Sybren Stuvel

    #2
    Re: do "some action" once a minute

    Petr Jakes enlightened us with:[color=blue]
    > I would like to do "some action" once a minute. My code (below)
    > works, I just wonder if there is some more pythonic approach or some
    > "trick" how to do it differently.[/color]

    I'd use the Threading module, and the Timer object from that module to
    be more precise. There you can simply say "call this function in 60
    seconds" or something similar. Then your program just sleeps until
    it's time to call the function.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa

    Comment

    • Petr Jakes

      #3
      Re: do "some action" once a minute

      Thanks for your comment. It is mainly English issue (I am not native
      English speaker).

      OK, to be more specific, I would like to run the code, when the value
      of seconds in the timestamp become say "00".
      The whole code will run in the infinitive loop and other actions will
      be executed as well, so it can not "sleep" for 60 seconds :).

      Regards

      Petr

      Comment

      • Diez B. Roggisch

        #4
        Re: do "some action" once a minute

        Petr Jakes wrote:
        [color=blue]
        > Thanks for your comment. It is mainly English issue (I am not native
        > English speaker).
        >
        > OK, to be more specific, I would like to run the code, when the value
        > of seconds in the timestamp become say "00".
        > The whole code will run in the infinitive loop and other actions will
        > be executed as well, so it can not "sleep" for 60 seconds :).[/color]

        The you have to go for a thread, as otherwise you'd have to create all sorts
        of checks in your program to check for the current date. That therad then
        could wait like this:


        import time
        def seconds():
        return time.localtime( )[5]


        def wait_for_second s(secs=0):
        while seconds() != secs:
        time.sleep(.5)

        def foo():
        # initial wait
        wait_for_second s()
        do_something()
        while True:
        time.sleep(50)
        wait_for_second s
        do_something()


        Diez


        Comment

        • Irmen de Jong

          #5
          Re: do "some action" once a minute

          Petr Jakes wrote:[color=blue]
          > OK, to be more specific, I would like to run the code, when the value
          > of seconds in the timestamp become say "00".
          > The whole code will run in the infinitive loop and other actions will
          > be executed as well, so it can not "sleep" for 60 seconds :).[/color]

          Have a look at my 'Kronos' task scheduler (based on the sched module).
          Available via http://www.razorvine.net/downloads.html

          It may provide the functionality you want.

          --Irmen

          Comment

          • jordan.taylor2@gmail.com

            #6
            Re: do "some action" once a minute

            This is why your best bet is probably threads.

            Class Eureka(Threadin g.Thread):
            def __init__(self):
            Threading.Threa d.__init__(self )
            self.start()
            def run(self,sleep_ time):
            while 1:
            time.sleep(slee p_time)
            print "eureka"

            Comment

            • Ben C

              #7
              Re: do "some action" once a minute

              On 2006-05-09, Petr Jakes <petr@tpc.cz> wrote:[color=blue]
              > I would like to do "some action" once a minute.[/color]

              You can try the sched module (import sched).

              You give it a time at which to call a callback. Then in the callback you
              can reset the "alarm" for a minute later, using enterabs.

              If the task might take longer than a minute, it just means you'll be
              setting an alarm for a time in the past, but it should still get called.
              But there is another problem, which is that the queue will presumably
              get very big over time, so you'll need to clear it out now and again
              with empty()... which means keeping track of the event objects yourself,
              which is annoying.

              Comment

              • Larry Bates

                #8
                Re: do &quot;some action&quot; once a minute

                Petr Jakes wrote:[color=blue]
                > I would like to do "some action" once a minute. My code (below) works,
                > I just wonder if there is some more pythonic approach or some "trick"
                > how to do it differently.
                >
                > minutes=time.lo caltime()[4]
                > while 1:
                > min, sec = time.localtime( )[4:6]
                > if sec==0 and minutes!=min: # first occur of sec==0 only!! polling
                > 10x a second
                > minutes=min
                > print "Eureca"
                > time.sleep(0.1)
                >
                > Regards
                >
                > Petr Jakes
                >[/color]
                What platform? If it is Windows, they write your application
                as a Windows Service and have it sleep for however long you
                want. It won't impact your system looping and sleeping.
                It will also be asynchronous, as it will sleep for time you
                specify, run your code to completion and then sleep again
                which isn't the same as running every one minute.

                If it is Linux, others have answered separately.

                -Larry Bates

                Comment

                Working...