How to run a program periodically by using Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • excite
    New Member
    • Oct 2006
    • 5

    How to run a program periodically by using Python

    I want to run a function periodically by using Python, and I found that
    time.sleep() may be of help, however, I think it may not be safe and flexible. Can somebody give me some suggestions on it? Thanks.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by excite
    I want to run a function periodically by using Python, and I found that
    time.sleep() may be of help, however, I think it may not be safe and flexible. Can somebody give me some suggestions on it? Thanks.
    The Python 2.4.4 docs say: "The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal's catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system".
    Please tell us more about your application - there may be better ways of doing this. So tell us: Does you app have a GUI? How often is "periodical ly"? What operating system are you running? Any other details about the function you want to run. Keep posting,
    Barton

    Comment

    • excite
      New Member
      • Oct 2006
      • 5

      #3
      Originally posted by bartonc
      The Python 2.4.4 docs say: "The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal's catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system".
      Please tell us more about your application - there may be better ways of doing this. So tell us: Does you app have a GUI? How often is "periodical ly"? What operating system are you running? Any other details about the function you want to run. Keep posting,
      Barton
      Thanks. The program does not have a GUI, running on a windows system, and I would like to run some functions every hour. The current programs are written in both C and python language, and I packed all the files by Python. Actually, the input files of my programs will be updated every hour externally, so I can run the functions after I detect the input files have been modified. However, if something wrong and the input files have not been updated in some hour, the program will lose synchronization . So I still need to know the time, and run the functions in each hour even the input files have not been updated.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        I've got a few ideas that I'll work up for you.

        Anybody else?

        Comment

        • Subsciber123
          New Member
          • Nov 2006
          • 87

          #5
          How about something like this:
          Code:
          import time
          next_time=time.time()
          while True:
              hourly_function()
              next_time=(next_time+3600) #sets the next occurrence of the
                                             #function to 1 hour later
              while True:
                  sleep(60)             #sleeps for a minute
                  if time.time()>next_time:
                      break
          This code will not work, however, for more than a day or so, and is only accurate to within about a minute.

          Comment

          • excite
            New Member
            • Oct 2006
            • 5

            #6
            Originally posted by Subsciber123
            How about something like this:
            Code:
            import time
            next_time=time.time()
            while True:
                hourly_function()
                next_time=(next_time+3600) #sets the next occurrence of the
                                               #function to 1 hour later
                while True:
                    sleep(60)             #sleeps for a minute
                    if time.time()>next_time:
                        break
            This code will not work, however, for more than a day or so, and is only accurate to within about a minute.
            Thanks. But why this code will not work for more than a day? Does the timer will be initialized every day? (Sorry that I am not very good at Python, I will check it later). If it is true, I think I can put the "next_time=time .time()" into the first while block, like this:
            Code:
            import time
            while True:
                [B]next_time=time.time()[/B]
                hourly_function()
                next_time=(next_time+3600) #sets the next occurrence of the
                                               #function to 1 hour later
                while True:
                    sleep(60)             #sleeps for a minute
                    if time.time()>next_time:
                        break
            What do you think of it?

            Comment

            • fuffens
              New Member
              • Oct 2006
              • 38

              #7
              On a linux/unix system I would always use Cron for periodic tasks like this instead of running your own Python script to control execution of other Python scripts and c-programs. There are several implementations of Cron on Windows also. Just Google for cron and windows (http://www.cronforwindows.com/ for example).

              BR
              /Fredrik

              Comment

              Working...