5

A python newbie question. Is there an equivalent to Java's Scheduled executor service in python?

3

3 Answers 3

5

Doesn't threading.timer do what you want?

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

EDIT Added repeatable example.

import threading
import time

def repeat_every(n, func, *args, **kwargs):
    def and_again():
        func(*args, **kwargs)
        t = threading.Timer(n, and_again)
        t.daemon = True
        t.start()
    t = threading.Timer(n, and_again)
    t.daemon = True
    t.start()


def scheduled_task(msg='hello, world', **kwargs):
    print time.time(), "scheduled_task:", msg, kwargs

repeat_every(.5, scheduled_task )
repeat_every(1, scheduled_task, "Slow", name="Hand luke")

for x in range(5):
    print time.time(), "Main: busy as a bee."
    time.sleep(3)

Generates:

 1360662042.34 Main: busy as a bee.
1360662042.84 scheduled_task: hello, world {}
1360662043.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662043.34 scheduled_task: hello, world {}
1360662043.84 scheduled_task: hello, world {}
1360662044.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662044.34 scheduled_task: hello, world {}
1360662044.84 scheduled_task: hello, world {}
1360662045.34 Main: busy as a bee.
1360662045.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662045.34 scheduled_task: hello, world {}
1360662045.85 scheduled_task: hello, world {}
1360662046.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662046.35 scheduled_task: hello, world {}
1360662046.85 scheduled_task: hello, world {}
1360662047.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662047.35 scheduled_task: hello, world {}
1360662047.85 scheduled_task: hello, world {}
1360662048.34 Main: busy as a bee.
1360662048.34 scheduled_task: Slow {'name': 'Hand luke'}
1360662048.35 scheduled_task: hello, world {}
1360662048.85 scheduled_task: hello, world {}
1360662049.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662049.35 scheduled_task: hello, world {}
1360662049.86 scheduled_task: hello, world {}
1360662050.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662050.36 scheduled_task: hello, world {}
1360662050.86 scheduled_task: hello, world {}
1360662051.34 Main: busy as a bee.
1360662051.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662051.36 scheduled_task: hello, world {}
1360662051.86 scheduled_task: hello, world {}
1360662052.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662052.36 scheduled_task: hello, world {}
1360662052.86 scheduled_task: hello, world {}
1360662053.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662053.36 scheduled_task: hello, world {}
1360662053.86 scheduled_task: hello, world {}
1360662054.34 Main: busy as a bee.
1360662054.35 scheduled_task: Slow {'name': 'Hand luke'}
1360662054.37 scheduled_task: hello, world {}
1360662054.87 scheduled_task: hello, world {}
1360662055.36 scheduled_task: Slow {'name': 'Hand luke'}
1360662055.37 scheduled_task: hello, world {}
1360662055.87 scheduled_task: hello, world {}
1360662056.36 scheduled_task: Slow {'name': 'Hand luke'}
1360662056.37 scheduled_task: hello, world {}
1360662056.87 scheduled_task: hello, world {}
Sign up to request clarification or add additional context in comments.

4 Comments

I needed for the task to keep running every N seconds, I ended up using twisted framework.
I expanded example to show how to repeat/reset Timer
This is very similar to the answer here stackoverflow.com/questions/2398661/…. Chaining tasks works so I upvoted you but is not very clean. Twisted has a much cleaner interface.
I was answering those that had said there was nothing in the standard python library to do it. Thanks for the link it was an interesting read.
1

My quick research states that Java's Scheduled Executor allows commands to be run after a delay or periodically (http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html).

Python doesn't have anything in the standard library to handle periodic execution but the sched module can execute tasks after a specified delay: http://docs.python.org/2/library/sched.html

A similar question was asked here about periodic events: Schedule a repeating event in Python 3 (python 3, though).

Comments

1

Funnily enough, I saw this github package a few minutes before reading your question:

Python job scheduling for humans.

Example taken from the README:

import schedule

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)

You may want to give it a shot :

$ pip install schedule

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.