1

Possible Duplicate:
What is the best way to repeatedly execute a function every x seconds in Python?

Hi so here is the code I have:

client = myclient(info1,info2)
                sellor()
                Contractor()

It works perfectly but what I would like to do is to make python launch that code every 60 seconds indefinitely... I don't actually understand how I have to put the code together with the time loop Any help is appreciated Thank's

Community
  • 1
  • 1
user1886471
  • 21
  • 1
  • 1
  • 2

6 Answers6

4

If the 60 seconds ignores the time it takes to execute your code):

from time import sleep

while True:
    sleep(60)
    # your code here

but if the 60 seconds takes into account the time it takes to execute your code:

from time import sleep
from os import fork       
while True:
    sleep(60)
    fork() # create child process
    # your code here
Rich Tier
  • 9,021
  • 10
  • 48
  • 71
2

Use the sleep method. Just create a loop (while, for, whatever) and sleep for 60 secs every iteration.

sje397
  • 41,293
  • 8
  • 87
  • 103
1
import time

while True:

  client = myclient(info1,info2)
            sellor()
            Contractor()
            time.sleep(10)

hope it works,all the best mate

ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
0
import time

repeat_time = 3.0

while True:
    start_time = time.time()

    # Your code goes here

    time.sleep(max(repeat_time - (time.time() - start_time), 0.0))

And your code will be executed exactly every "repeat_time"

Crappy
  • 61
  • 1
  • 3
0

You could use sleep as already mentioned. But because there may be a variable amount of time needed for your own functions to run, this wouldn't necessarily mean your functions are run every 60 seconds.

If it was important that the period between each start of your functions is closer to 60 seconds, you could use time. I haven't tried this but something like

import time

while True:
    # Get the current time 
    startTime = time.time()

    # Your functions
    client = myclient(info1,info2)
    sellor()
    Contractor()

    delay = True
    while delay:
        if time.time() - startTime > 60:
            delay = False # Break the delay
Dan
  • 1,209
  • 3
  • 13
  • 29
0

You might also think of just scheduling the task through windows scheduler. The benefit here would end the script once run and then execute the script again after scheduled interval. In the second approach it seems that the script instance process would continually run and only use the sleep function to do nothing for the specified time. I take it this way if the scripts fails at any instance you might have to keep a check to restart the script. While as a scheduled activity the script will be executed in any case at that specified intervals.

You might also not want the process thread to be kept running for the python script executed. I will research on this and you might get to hear form our other folks in the mean while.

Regards, Harshal

Harry
  • 17
  • 1
  • 3