TGScheduler
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
-
TypeError: my_task() missing 1 required positional argument: 'message'
cause Attempting to schedule a function that expects arguments without providing them correctly via the `args` parameter.fixEnsure `args` is a tuple containing all required positional arguments for `my_task`. Example: `s.add_task(my_task, args=('Hello',))`. -
ModuleNotFoundError: No module named 'tgscheduler.scheduler'
cause Incorrect import path for the Scheduler class.fixThe Scheduler class is directly available from the top-level `tgscheduler` package. Use `from tgscheduler import Scheduler`. -
Task does not run or runs inconsistently in a web framework/GUI application.
cause TGScheduler's `tick()` method needs to be called regularly. If the main thread is blocked (e.g., by a web server's run loop or a GUI event loop), `tick()` won't be executed.fixRun the scheduler's `tick()` method in a separate thread using `threading.Thread` or integrate it carefully into the application's event loop to ensure it's periodically called without blocking.
Warnings
- gotcha TGScheduler is a blocking scheduler if `tick()` is called in a tight loop. To integrate it into existing asynchronous applications (e.g., web servers), ensure `tick()` is called periodically without blocking the main event loop, or run the scheduler in a separate thread.
- deprecated The project appears to be in maintenance mode, with the last PyPI release in 2015. While functional, it might not receive updates for new Python versions or critical bug fixes. Consider more actively maintained alternatives for new projects requiring robust scheduling.
- gotcha TGScheduler's `add_task` method requires careful handling of task arguments. If a task function takes arguments, they must be passed as a tuple to the `args` parameter, even for a single argument.
Install
-
pip install tgscheduler
Imports
- Scheduler
from tgscheduler.scheduler import Scheduler
from tgscheduler import Scheduler
Quickstart
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()