repost: http web page fetch question

G

grocery_stocker

Given the following...

[cdalten@localhost oakland]$ more basic.py
#!/usr/bin/python

import sched
import time

scheduler = sched.scheduler(time.time, time.sleep)

def print_event(name):
print 'EVENT:', time.time(), name

print 'START:', time.time()
scheduler.enter(2, 1, print_event, ('first',))

scheduler.run()
[cdalten@localhost oakland]$ ./basic.py
START: 1240584506.06
EVENT: 1240584508.06 first
[cdalten@localhost oakland]$


How do I modify it so that it runs every hour on the hour.
 
E

Emile van Sebille

grocery_stocker said:
Given the following...

[cdalten@localhost oakland]$ more basic.py
How do I modify it so that it runs every hour on the hour.

I'd probably use cron, but here's one way.

Emile
-----

import sched
import time

scheduler = sched.scheduler(time.time, time.sleep)

def print_event(count,rescheduler):
print 'EVENT:', time.time(), count
rescheduler.enter(2,1,print_event,(count+1,rescheduler))

print 'START:', time.time()
scheduler.enter(2, 1, print_event, (1,scheduler))

scheduler.run()
 
E

Emile van Sebille

grocery_stocker said:
Given the following...

[cdalten@localhost oakland]$ more basic.py
How do I modify it so that it runs every hour on the hour.

I'd probably use cron, but here's one way.

Emile
-----

import sched
import time

scheduler = sched.scheduler(time.time, time.sleep)

def print_event(count,rescheduler):
print 'EVENT:', time.time(), count
rescheduler.enter(2,1,print_event,(count+1,rescheduler))

print 'START:', time.time()
scheduler.enter(2, 1, print_event, (1,scheduler))

scheduler.run()
 
G

Gabriel Genellina

scheduler = sched.scheduler(time.time, time.sleep)

How do I modify it so that it runs every hour on the hour.

(The sched module is almost useless, IMHO)

I'd use cron (linux) or schtasks (windows).
If it has to be a Python script, I'd just use time.sleep:

def compute_delay_until_next_hour():
now = time.localtime()
secs_past_hour = now.tm_min * 60 + now.tm_sec
delay = 60*60 - secs_past_hour
return delay

# do_something() # optional
while True:
dt = compute_delay_until_next_hour()
time.sleep(dt)
do_something()
 
G

Gabriel Genellina

scheduler = sched.scheduler(time.time, time.sleep)

How do I modify it so that it runs every hour on the hour.

(The sched module is almost useless, IMHO)

I'd use cron (linux) or schtasks (windows).
If it has to be a Python script, I'd just use time.sleep:

def compute_delay_until_next_hour():
now = time.localtime()
secs_past_hour = now.tm_min * 60 + now.tm_sec
delay = 60*60 - secs_past_hour
return delay

# do_something() # optional
while True:
dt = compute_delay_until_next_hour()
time.sleep(dt)
do_something()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top