4

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

Usman Ismail
  • 17,999
  • 14
  • 83
  • 165
  • These questions are similar and have suggested solutions: - http://stackoverflow.com/questions/373335/suggestions-for-a-cron-like-scheduler-in-python - http://stackoverflow.com/questions/2398661/schedule-a-repeating-event-in-python-3 – dbader May 28 '13 at 08:03
  • No, there isn't in the standard library. You might find something on PyPI though. – Ned Batchelder Feb 11 '13 at 17:25
  • https://pypi.org/project/scheduled-thread-pool-executor/ – Yogaraj Jan 05 '22 at 11:03

3 Answers3

4

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 {}
sotapme
  • 4,695
  • 2
  • 19
  • 20
  • I needed for the task to keep running every N seconds, I ended up using twisted framework. – Usman Ismail Feb 12 '13 at 02:28
  • I expanded example to show how to repeat/reset ``Timer`` – sotapme Feb 12 '13 at 09:42
  • This is very similar to the answer here http://stackoverflow.com/questions/2398661/schedule-a-repeating-event-in-python-3. Chaining tasks works so I upvoted you but is not very clean. Twisted has a much cleaner interface. – Usman Ismail Feb 12 '13 at 14:27
  • 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. – sotapme Feb 12 '13 at 17:31
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).

Community
  • 1
  • 1
Lllama
  • 374
  • 1
  • 9
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
jlengrand
  • 12,152
  • 14
  • 57
  • 87