views:

495

answers:

5

In python how to implement a thread which runs in the background (may be when the module loads) and calls the function every minute Monday to Friday 10 AM to 3 PM. For example the function should be called at:

10:01 AM 10:02 AM 10:03 AM . . 2:59 PM

Any pointers?

Environment: Django

Thanks

+4  A: 

Django is a server application, which only reacts to external events.

You should use a scheduler like cron to create events that call your django application, either calling a management subcommand or doing an HTTP request on some special page.

Tobu
GAE does this with Task Queues (experimental) and Cron hooks
Dustin Getz
A: 

Note sure how django affects threads (unless you're using App Engine where you can't do low level such), but once your thread is running you can continuously check timestamps:

from datetime import time
from datetime import date
time_delta = 60
while True:
    end_time = time.time() + time_delta
    while time.time() < end_time:
        time.sleep(1)
    if date.today().weekday() in range(1,5):
        #do something baby

Not tested, so take it for a spin first.

pokstad
A: 

Here is an article about scheduling tasks in cron from google code.

Scheduling Tasks with Cron

Diakonia7
Do you this it's OK to use GTK in web environment?
Denis Otkidach
+1  A: 

The threading.Timer class is convenient to do such tasks. But you have to compute interval yourself.

Denis Otkidach
+1  A: 

I think you can make a command http://docs.djangoproject.com/en/1.1/howto/custom-management-commands/#howto-custom-management-commands and then cron it.

diegueus9