TGScheduler

1.7.0 · maintenance · verified Thu Apr 16

TGScheduler is a pure Python scheduler library, currently at version 1.7.0. It enables applications to run one-time or recurring tasks, supporting execution in-process (using threads), as forked processes, or synchronously within the main code. The library is based on the scheduler originally built into TurboGears 1, offering a straightforward approach to task automation. Its release cadence is infrequent, with the last update in 2015.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the TGScheduler, add both one-time and recurring tasks, and run the scheduler in a simple blocking loop. Tasks are defined as callable functions, and arguments can be passed via the `args` parameter. The `delay` argument schedules a task for a single future execution, while `interval` schedules it for recurring executions. Logging is included to show task execution.

from tgscheduler import Scheduler
import time
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def my_task(message):
    logging.info(f"Executing task with message: {message}")

s = Scheduler()

# Schedule a task to run once, 5 seconds from now
s.add_task(my_task, args=('One-time task!',), delay=5)

# Schedule a recurring task every 10 seconds
s.add_task(my_task, args=('Recurring task!',), interval=10, initialdelay=2, name='RecurringExample')

logging.info("Scheduler started. Tasks will run.")
try:
    # Run the scheduler in a blocking loop (or integrate into an existing event loop)
    while True:
        s.tick()
        time.sleep(1) # Sleep to prevent busy-waiting
except KeyboardInterrupt:
    logging.info("Scheduler stopped by user.")
    s.shutdown()

view raw JSON →