Skip to main content

A pure python calendar-queue based on asyncio

Project description

calendar-queue

PyPI pyversions tests linting codecov

A pure-python calendar queue and library for scheduling events with asyncio.

Many other libraries allow for event/job scheduling but either they are not asynchronous or they are just fire-and-forget. calendar-queue has two purposes:

  1. providing the primitives for creating your own scheduler by exporting a CalendarQueue class in which events can be awaited
  2. providing a higher level abstraction Calendar class that simplifies the usage of CalendarQueue.

Depending on your needs, you can use one of the two to develop your own event manager/scheduler.

The idea is: we take care of emitting events at the right time, you write the logic for acting accordingly.

Install

pip install calendar-queue

Usage

An example usage of CalendarQueue

import asyncio
from datetime import datetime
from random import randrange
from secrets import token_hex

from calendar_queue import CalendarQueue

# (optional) define the item type
CustomItem = tuple[str]

# use the low level calendar queue
cq: CalendarQueue[CustomItem] = CalendarQueue()

async def put_random():

    print("Putting new items in the calendar queue. Hit CTRL + C to stop.")

    while True:

        # sleep for a while, just to release the task
        await asyncio.sleep(1)

        scheduled_ts = datetime.now().timestamp() + randrange(1, 5)

        s = token_hex(8)

        current_item: CustomItem = (s)

        print(f"{datetime.now().isoformat()}: putting {current_item} scheduled for {datetime.fromtimestamp(scheduled_ts).isoformat()}")

        cq.put_nowait((scheduled_ts, current_item))


async def get_from_queue():

    while True:
        ts, el = await cq.get()

        print(f"{datetime.now().isoformat()}: getting {el} scheduled for {datetime.fromtimestamp(ts).isoformat()}")

async def main():

    await asyncio.gather(
        asyncio.create_task(put_random()),
        asyncio.create_task(get_from_queue()),
    )


if __name__ == "__main__":
    asyncio.run(main())

Development

This library is developed using Python 3.11 and pdm as dependency manager.

Testing is done via github actions and it's done on python versions 3.10, 3.11, 3.12, 3.13, 3.14, 3.14t and on latest ubuntu, macos, windows OSes.

For local development you'll need to have pdm installed, then you can:

  1. run pdm install to create the virtual environment and install the dependencies
  2. run pdm venv activate to activate the virtual environment
  3. run the tests using pytest
  4. run the linter pylint src

Contributing

Contributions are welcome! Especially for new tests and for improving documentation and examples.

License

The code in this project is released under the MIT License.

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

calendar_queue-0.2.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

calendar_queue-0.2.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file calendar_queue-0.2.0.tar.gz.

File metadata

  • Download URL: calendar_queue-0.2.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.26.3 CPython/3.14.2 Linux/6.11.0-1018-azure

File hashes

Hashes for calendar_queue-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f67ef2eadeae10c09283b8ddac4031c70029d783ab2a9c1f162374c5a5f5dd7b
MD5 b6e0d6cb7258da94309a60549bc4806d
BLAKE2b-256 be8036326bf203e0e89061c23017ee8a254436fe60c6ef85f5aee52ac5ffb410

See more details on using hashes here.

File details

Details for the file calendar_queue-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: calendar_queue-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.26.3 CPython/3.14.2 Linux/6.11.0-1018-azure

File hashes

Hashes for calendar_queue-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b45726579edce0c72c378bbba124310d77619f8e52dbc476c1d0e7cbf8761b4d
MD5 b8e503217538ef99c218bffee42a703f
BLAKE2b-256 7edd3efd9c0bc3299c8c7bc8336668c421ce545318bf17a677255727fbb7eac6

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