Skip to main content

Open source, single-thread interval scheduler

Project description

An elegant and simple way to schedule periodic tasks in Python. Both the scheduluer and the task run in the main thread, which avoids the issue of synchronizing the data access between tasks. However, if a task takes a lot of time to complete, the other tasks must wait until it finishes.

ischedule accounts for the time it takes for the task function to execute. For example, if a task is scheduled every second and takes 0.6 seconds to complete, there will be a delay of only 0.4 seconds between consecutive executions. Delays are not propagated. If the previously-mentioned task is scheduled for execution at t=1 second, but is delayed by 0.3 seconds, the next execution of the same task will never the less be scheduled at t=2 seconds.

What happens during heavy loading

  • If more than one task become pending at the same time, they are executed in the order in which they were scheduled by schedule.
  • Regardless of the load, all pending tasks will be executed if they become pending (unless another task hangs).
  • If the execution of a task is delayed that the next execution of the same task become pending, this execution will be skipped.

Exceptions

Exceptions during the execution are propagated out of run_pending, and can be dealth with by the caller.

Example

import time

from ischedule import schedule, run_loop

start_time = time.time()

def job_1():
    dt = time.time() - start_time
    print(f"Started a _fast_ job at t={dt:.2f}")

def job_2():
    dt = time.time() - start_time
    if dt > 2:
        return
    print(f"Started a *slow* job at t={dt:.2f}")
    time.sleep(1)


schedule(job_1, interval=0.1)
schedule(job_2, interval=0.5)

run_loop()

This example produces the following output:

Started a _fast_ job at t=0.10
Started a _fast_ job at t=0.20
Started a _fast_ job at t=0.31
Started a _fast_ job at t=0.41
Started a _fast_ job at t=0.50
Started a *slow* job at t=0.50
Started a _fast_ job at t=1.51
Started a *slow* job at t=1.51
Started a _fast_ job at t=2.52
Started a _fast_ job at t=2.61
Started a _fast_ job at t=2.71
Started a _fast_ job at t=2.81
Started a _fast_ job at t=2.90
Started a _fast_ job at t=3.00

The fast job runs every 0.1 seconds, and completes quickly. When the slow job starts running at t=0.5, it doesn't return control until one second later, at t=1.50s. By that time, both the fast and the slow job become pending, and are executed in the order they were added to the scheduler. The slow job does not run after t=2.0, so the fast job returns to running normally every 0.1 seconds. The task had to be stopped with a keyboard interrupt.

Known issues

None at this time. Issues and suggestions can be submitted to https://github.com/aleksve/ischedule/issues.

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

ischedule-0.92.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

ischedule-0.92-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file ischedule-0.92.tar.gz.

File metadata

  • Download URL: ischedule-0.92.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for ischedule-0.92.tar.gz
Algorithm Hash digest
SHA256 fea6449ea03ee6e99920a394c2e933385f761720b109710448a9de0bcd2d9be5
MD5 e733470d75ed82a2b5523d42bb795672
BLAKE2b-256 b92fc05576a7dba1180f0d11a358b588fe9a8102755e03c2f29fb1181b2a27b1

See more details on using hashes here.

File details

Details for the file ischedule-0.92-py3-none-any.whl.

File metadata

  • Download URL: ischedule-0.92-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for ischedule-0.92-py3-none-any.whl
Algorithm Hash digest
SHA256 416a4fb46d5a43204a951b48b98550707a4d1cde1956ed38feec4d8bc21d8a61
MD5 aba2f88dda3199c204899e0dc936425f
BLAKE2b-256 95337a7e2fa7048db57d7a1b305538119bacde5831e29c029e71d995a5e6e768

See more details on using hashes here.

Supported by

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