A pure python calendar-queue based on asyncio
Project description
calendar-queue
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:
- providing the primitives for creating your own scheduler by exporting a
CalendarQueue
class in which events can be awaited - providing a higher level abstraction
Calendar
class that simplifies the usage ofCalendarQueue
.
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
and on latest ubuntu
, macos
, windows
OSes.
For local development you'll need to have pdm installed, then you can:
- run
pdm install
to create the virtual environment and install the dependencies - run
pdm venv activate
to activate the virtual environment - run the tests using
pytest
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file calendar_queue-0.1.0.tar.gz
.
File metadata
- Download URL: calendar_queue-0.1.0.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.19.1 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d67adcc6501cd7197ec0aefe57da4687264107d909b1042b33ffaa75189102d2 |
|
MD5 | d9a4c719db220c00bfac5937835c1730 |
|
BLAKE2b-256 | 5d0edd43293400f3c45be16e978b33b9bb78949b3e7027ec028c5bc43f02c35a |
File details
Details for the file calendar_queue-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: calendar_queue-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.19.1 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f1140381b2dfb5313413d37b6c478919d89172fafc2bb9982c460900fe7b673 |
|
MD5 | b6effc71af57f83748a9bccfc6849480 |
|
BLAKE2b-256 | 0d192424bd0ab9f4351304eb26aa630dcab8e2711ffcb5e7e0fd5c9f81f9184a |