tnishada
tnishada

Reputation: 1414

run python scheduler continuously

 #!/usr/bin/python
import sched, time
s = sched.scheduler(time.time, time.sleep)
def print_time():       
    print "From print_time", time.time()

def print_some_times():
    print time.time()
    s.enter(5, 1, print_time, ())
    s.enter(10, 1, print_time, ())
    s.run()

print_some_times()

The above code can be used to execute the print_time() function exactly in 5 seconds and 10 seconds.

I want to know that how I could execute print_time() function exactly with a 5 second time period continuously(periodically) without stoping.

Upvotes: 0

Views: 5877

Answers (3)

tnishada
tnishada

Reputation: 1414

even though time.sleep() function works it is not much accurate. consider the below code

while True:
    time.sleep(5)
    print_time()

in this case the function always delay execution 5 seconds. assume print_time() function spend 2 seconds for executions. then according to this method print_time() function invokes with a period of 7 seconds( 5 + 2 )

but the answer can be obtained more accurately by doing it via following code

 #!/usr/bin/python
import sched, time
s = sched.scheduler(time.time, time.sleep)
def print_time(): 
    s.enter(5, 1, print_time, ())  
    print "From print_time", time.time()

def print_some_times():
    print time.time()
    s.enter(5, 1, print_time, ())        
    s.run()

print_some_times()

what here happens is when print_time() function is executed. at the very beginning of the print_time() function, another entry to the scheduler queue is added in this case the accuracy is very high

Upvotes: 2

user4344563
user4344563

Reputation:

you can use a thread that sleeps 5 seconds:

import threading, time

def worker():
    i = 0
    while (True):
        print 'do something ... ' + str(time.time())
        time.sleep(5)
        i += 1
        if i > 5: break

t = threading.Thread(target=worker)
print time.time()
t.start()
print time.time()

If you want to run this "forever" - just remove the break condition ...

Upvotes: 1

d-coder
d-coder

Reputation: 14003

I think you can go with time.sleep() The parameter passed into this function is the exact number of seconds you can pause the program.

import time
time.sleep(5)  ## pauses for 5 seconds.

I don't know if I'm helping you right but you can try like the following if this is what you are looking for

import sched, time
s = sched.scheduler(time.time, time.sleep)
def print_time():
    var =10
    var = "fdfd"
    print "From print_time", time.time()
def print_some_times():
    print time.time()
    s.enter(5, 1, print_time, ())
    s.enter(10, 1, print_time, ())
    s.run()

while True:
    time.sleep(5)
    print_some_times()

OUTPUT :

1418811191.3
From print_time 1418811196.31
From print_time 1418811201.3
1418811206.31
From print_time 1418811211.34
From print_time 1418811216.32

If you just want to call the function print_time() with delay of 5 seconds and forever then try the following

import sched, time
s = sched.scheduler(time.time, time.sleep)
def print_time():
    var =10
    var = "fdfd"
    print "From print_time", time.time()
def print_some_times():
    print time.time()
    s.enter(5, 1, print_time, ())
    s.enter(10, 1, print_time, ())
    s.run()

while True:
    time.sleep(5)
    print_time()

OUTPUT :

From print_time 1418811465.18
From print_time 1418811470.2
From print_time 1418811475.27
From print_time 1418811480.27
From print_time 1418811485.27

Upvotes: 0

Related Questions