Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Simple Asynchronous Scheduler

AsyncScheduler is a wrapper for sched.scheduler that provides asynchronous operation out of the box. Thus, starting the scheduler does not block the execution of the next statements. Further, adding and removing events can be done without manually stopping/starting the scheduler.

The event itself is executed synchronously. Consequently, it the execution of the calling method takes longer than the delay to the next event, execution of the next method is postponed until the previous method returns.

Four different methods are available to add new events: * enter - adds a single event to take place in n seconds * enterabs - adds a single event to take place at time t * repeat - adds a repeating event that is triggered every n seconds * enterabs - adds a repeating event that is triggered at time t for the first time and then every n seconds

Example

Some events

Code

from asyncscheduler import AsyncScheduler
from time import sleep

a = AsyncScheduler()
a.start()
event = a.enter(1, 1, print, args=("event 1",))
a.enter(2, 1, print, args=("event 2",))
a.enter(3, 1, print, args=("event 3",))
a.enter(4, 1, print, args=("event 4",))
a.cancel(event)
sleep(3.1)
a.clear_scheduler()
a.stop()

Output

event 2
event 3

Digital clock

Code

from asyncscheduler import AsyncScheduler
import time, datetime


def display_time():
    print("\r{}".format(datetime.datetime.now().strftime("%H:%M:%S")), end='\r')


a = AsyncScheduler()
a.start()
a.repeatabs(math.floor(time.time()) + 1, 1, 1, display_time)

try:
    while True:
        time.sleep(0.25)
except KeyboardInterrupt:
    pass

a.stop()

Output

12:34:56

API

enter

AsyncScheduler.enter(self, delay, priority, action, args=(), kwargs={})

Add an event to the scheduler. It will be executed after the provided delay with ‘action(*argument, **kwargs)’. In case of two events scheduled for the same time the priority is used for execution order. A lower number means a higher priority.

Parameter: * delay - delay call of func for this amount of seconds. e.g. 12.34 * priority - events scheduled for the same time are processed according to their priority. * action - function that is called upon expires * args - tuple of arguments for this function * kwargs - dict of arguments for this function

Returns the instance of the added event.

enterabs

AsyncScheduler.enterabs(self, time, priority, action, args=(), kwargs={})

Add an event to the scheduler. It will be executed at the provided time with ‘action(*argument, **kwargs)’. In case of two events scheduled for the same time the priority is used for execution order. A lower number means a higher priority.

Parameter: * time - call the action at this time stamp. * priority - events scheduled for the same time are processed according to their priority. * action - function that is called upon expires * args - tuple of arguments for this function * kwargs - dict of arguments for this function

Returns the instance of the added event.

clear_scheduler

AsyncScheduler.clear_scheduler(self)

Cancels all scheduled events.

cancel

AsyncScheduler.cancel(self, event)

Remove the provided event from the scheduler. In case of an unknown event, a ValueError will be raised.

Parameter: * event - event instance as returned from add_event.

repeat

repeat(self, every, priority, action, args=(), kwargs={})

Add a repeating event to the scheduler. It will be executed each time the provided delay (every-n-seconds) has expired with ‘func(*argument, **kwargs)’. In case of two events scheduled for the same time the priority is used for execution order. A lower number means a higher priority.

See repeatabs for more information.

Parameter: * time - call the action at this time stamp. * every - every-n-seconds call action. e.g. 12.34 * priority - events scheduled for the same time are processed according to their priority. * action - function that is called upon expirey * args - tuple of arguments for this function * kwargs - dict of arguments for this function

Returns the instance of the added event.

repeatabs

repeatabs(self, time, every, priority, action, args=(), kwargs={})

Add a repeating event to the scheduler. It will be executed each time the provided delay (every-n-seconds) has expired with ‘func(*argument, **kwargs)’. The first event is triggered at the provided time. In case of two events scheduled for the same time the priority is used for execution order. A lower number means a higher priority.

A repeating event will trigger one last time in case of a regular stop with wait=False (=default).

Note: the returned event instance is the instance of the first iteration only. Thus, after the first iteration it will not be part of scheduler.queue no more. Instead a new event for this repeating event has been created. AsyncScheduler keeps track of the current instance and uses the first instance for identification of which event to cancel. This is done with the method _repeat_event_hash and the map _repeat_event_mapping.

Parameter: * time - call the action at this time stamp. * every - every-n-seconds call action. e.g. 12.34 * priority - events scheduled for the same time are processed according to their priority. * action - function that is called upon expirey * args - tuple of arguments for this function * kwargs - dict of arguments for this function

Returns the instance of the added event.

start

start(self)

Starts the scheduler.

stop

stop(self)

Stops the scheduler. After stop, the scheduler is emptied. Thus, calling start after stop results in a new, blank schedule that must be filled.

Todos

  • readthedocs

  • CI/CD

Misc

The code is written for python3 (and tested with python 3.5).

Merge requests / bug reports are always welcome.

Metadata

Release files for AsyncScheduler 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for AsyncScheduler 0.2.0
File Size Uploaded Provenance
AsyncScheduler-0.2.0.tar.gz 7.1 kB Without provenance Details

Release files / AsyncScheduler-0.2.0.tar.gz

Download URL AsyncScheduler-0.2.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d077131667bbcb499d13f906b2cd3670e2c359344a63a949eb1ba69263a25d00
BLAKE2b-256 checksum
How to use checksums
c8342c72a02768183a17c40376c96dffee1cc82cf0ad2118f8e5853830cf7494
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Security for AsyncScheduler 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page