Skip to main content

task scheduler package

Project description

Tscheduler Tasks Scheduler (Tscheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or periodically. You can add new jobs or remove old ones on the fly as you please.

Among other things, Tscheduler can be used as a cross-platform, application specific replacement to platform specific schedulers, such as the cron daemon or the Windows task scheduler. Please note, however, that Tscheduler is not a daemon or service itself, nor does it come with any command line tools. It is primarily meant to be run inside existing applications. That said, Tscheduler does provide some building blocks for you to build a scheduler service or to run a dedicated scheduler process.

Tscheduler has three built-in scheduling systems you can use:

Cron-style scheduling

Interval-based execution (runs jobs on even intervals)

Tscheduler also integrates with several common Python frameworks, like:

asyncio (PEP 3156)

gevent

Tornado

Twisted

Qt (using either PyQt , PySide6 , PySide2 or PySide)

There are third party solutions for integrating Tscheduler with other frameworks:

Django

Flask

Fastapi

How to use

First we install the package from pypi.org

pip install tscheduler

Second we call BackgroundScheduler from the package tscheduler

from tscheduler.tscheduler import BackgroundScheduler

Python Example 1:

from tscheduler.tscheduler import BackgroundScheduler

def hello():
    print("hello")

scheduler = BackgroundScheduler()
scheduler.add_job(target=hello,trigger="interval",seconds=5) # line 1
scheduler.add_job(target=hello,seconds=5) # line 2  line 1 same result with line 2
scheduler.start()

This example shows the use of the interval

Python Example 2:

from tscheduler.tscheduler import BackgroundScheduler

def hello():
    print("hello")

scheduler = BackgroundScheduler()
scheduler.add_job(target=hello,trigger="cron",hours=10,minutes=30,seconds=0) # line 1
scheduler.add_job(target=hello,trigger="cron",day_of_week="mon-tue-wed-thu-fri-sat-sun",hours=10,minutes=30,seconds=0) # line 2   line 1 same result with line 2
                                                                                                                       # day_of_week: With this property you can specify the days on which the job works.
scheduler.start()

This example shows the use of the cron

Python Example 3: this example using fastapi

from contextlib import asynccontextmanager
from time import sleep
from fastapi import FastAPI
from tscheduler.tscheduler import BackgroundScheduler


@asynccontextmanager
async def lifespan(_: FastAPI):
    print("app started....")
    sch.start()
    yield
    print("app stopped...")
    sch.stop()


app = FastAPI(lifespan=lifespan)
sch = BackgroundScheduler()


def hello():
    print("hello")


sch.add_job(target=hello, seconds=5)

Note: The function must not contain args But there is a solution for it. The function must be wrapped with a function not contain args Example:

 from tscheduler.tscheduler import BackgroundScheduler

def hello(name):
    print(f"hello {name}")

name="taibaoui mohamed"

def print_name():
    hello(name)
scheduler = BackgroundScheduler()
scheduler.add_job(target=print_name,trigger="interval",seconds=5)
scheduler.start()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tscheduler-0.0.6.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tscheduler-0.0.6-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file tscheduler-0.0.6.tar.gz.

File metadata

  • Download URL: tscheduler-0.0.6.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for tscheduler-0.0.6.tar.gz
Algorithm Hash digest
SHA256 de7b2b53e5118ca79cef43fd20f02c73e47e31bfa4a629f64c2c1f5a8bd63ff5
MD5 0edceb9bd2767e6993cc1d518f01c1d1
BLAKE2b-256 5ae0eb2eafa8b6bcc5940f367ae46f6bbbe0ab519f06304a1c331d0ed56d1ead

See more details on using hashes here.

File details

Details for the file tscheduler-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: tscheduler-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for tscheduler-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 bfc65884211950bc7611dd404491c7144efcb71312e1269c9e4a62927e03f21d
MD5 217e245bb363fb4ca5e33515a5c3982c
BLAKE2b-256 3b872f48addc7afb5e409f2032dfb6e6334119b311a84200dcb6c05ab741a826

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page