kaiju-scheduler


Namekaiju-scheduler JSON
Version 0.1.4 PyPI version JSON
download
home_pagehttps://github.com/violet-black/kaiju-scheduler
SummaryAsynchronous tasks scheduler and executor
upload_time2024-05-08 10:20:35
maintainerNone
docs_urlNone
authorvioletblackdev@gmail.com
requires_python>=3.8
licenseMIT
keywords scheduler asyncio tasks
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![pypi](https://img.shields.io/pypi/v/kaiju-scheduler.svg)](https://pypi.python.org/pypi/kaiju-scheduler/)
[![docs](https://readthedocs.org/projects/kaiju-scheduler/badge/?version=latest&style=flat)](https://kaiju-scheduler.readthedocs.io)
[![codecov](https://codecov.io/gh/violet-black/kaiju-scheduler/graph/badge.svg?token=FEUUMQELFX)](https://codecov.io/gh/violet-black/kaiju-scheduler)
[![tests](https://github.com/violet-black/kaiju-scheduler/actions/workflows/tests.yaml/badge.svg)](https://github.com/violet-black/kaiju-scheduler/actions/workflows/tests.yaml)
[![mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
[![code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

[![python](https://img.shields.io/pypi/pyversions/kaiju-scheduler.svg)](https://pypi.python.org/pypi/kaiju-scheduler/)

**kaiju-scheduler** is a simple asynchronous tasks scheduler / executor for asyncio functions. It adds a bit of extra
such as retries, timeouts, execution policies etc.

# Installation

With pip and python 3.8+:

```bash
pip3 install kaiju-scheduler
```

# How to use

See the [user guide](https://kaiju-scheduler.readthedocs.io/guide.html) for more info.

Initialize a scheduler and schedule your procedure for periodic execution. Then start the scheduler.

```python
from kaiju_scheduler import Scheduler

async def call_async_procedure(*args, **kws):
    ...

async def main():
    scheduler = Scheduler()
    scheduler.schedule_task(call_async_procedure, interval_s=10, args=(1, 2), kws={'value': True})
    await scheduler.start()
    ...
    await scheduler.stop()

```

Alternatively you can use the scheduler contextually.

```python
async def main():
    async with Scheduler() as scheduler:
        scheduler.schedule_task(call_async_procedure, interval_s=10, args=(1, 2), kws={'value': True})
```

`Scheduler.schedule_task` returns a task object which you can enable / disable or supress the task execution in
your code temporarily using `task.suspend` context.  You can also access the previous call results from `task.result` attribute.

```python

class Cache:

    def __init__(self, scheduler: Scheduler):
        self._scheduler = scheduler
        self._cache_task = self._scheduler.schedule_task(
            self.cache_all, interval_s=600, policy=scheduler.ExecPolicy.WAIT)

    async def cache_all(self):
        ...

    async def reconfigure_cache(self):
        async with self._cache_task.suspend():
            "Do something while the caching is suspended"

```

You can specify retries for common types of errors such as `IOError` or `ConnectionError` using `retries` parameter.
The scheduler will try to retry the call on such type of error.

```python
scheduler.schedule_task(call_async_procedure, interval_s=300, retries=3, retry_interval_s=1)
```

There are various policies considering task execution.
See the [reference](https://kaiju-scheduler.readthedocs.io/reference.html) for more info on that.

# Server

There's also a simple 'server' for handling asyncio tasks inside Python. It extends the standard loop functionality
with retries, timeouts and impose some rate limit and prevent the loop from growing infinitely.

The server returns an `asyncio.Task` object which can be awaited independently. The idea is that any error is not
raised but instead returned inside of the result. This allows for more convenient handling of errors while using this
in streams, queues and server applications.

See the [reference](https://kaiju-scheduler.readthedocs.io/reference.html) for more info on server functions.

```python
from kaiju_scheduler import Server


async def call_something(arg1: int, arg2: int):
    return arg1 + arg2


async def main():
    async with Server() as server:
        task = await server.call(call_something, [1, 2])
        await task

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/violet-black/kaiju-scheduler",
    "name": "kaiju-scheduler",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "scheduler, asyncio, tasks",
    "author": "violetblackdev@gmail.com",
    "author_email": null,
    "download_url": null,
    "platform": null,
    "description": "[![pypi](https://img.shields.io/pypi/v/kaiju-scheduler.svg)](https://pypi.python.org/pypi/kaiju-scheduler/)\n[![docs](https://readthedocs.org/projects/kaiju-scheduler/badge/?version=latest&style=flat)](https://kaiju-scheduler.readthedocs.io)\n[![codecov](https://codecov.io/gh/violet-black/kaiju-scheduler/graph/badge.svg?token=FEUUMQELFX)](https://codecov.io/gh/violet-black/kaiju-scheduler)\n[![tests](https://github.com/violet-black/kaiju-scheduler/actions/workflows/tests.yaml/badge.svg)](https://github.com/violet-black/kaiju-scheduler/actions/workflows/tests.yaml)\n[![mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)\n[![code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n[![python](https://img.shields.io/pypi/pyversions/kaiju-scheduler.svg)](https://pypi.python.org/pypi/kaiju-scheduler/)\n\n**kaiju-scheduler** is a simple asynchronous tasks scheduler / executor for asyncio functions. It adds a bit of extra\nsuch as retries, timeouts, execution policies etc.\n\n# Installation\n\nWith pip and python 3.8+:\n\n```bash\npip3 install kaiju-scheduler\n```\n\n# How to use\n\nSee the [user guide](https://kaiju-scheduler.readthedocs.io/guide.html) for more info.\n\nInitialize a scheduler and schedule your procedure for periodic execution. Then start the scheduler.\n\n```python\nfrom kaiju_scheduler import Scheduler\n\nasync def call_async_procedure(*args, **kws):\n    ...\n\nasync def main():\n    scheduler = Scheduler()\n    scheduler.schedule_task(call_async_procedure, interval_s=10, args=(1, 2), kws={'value': True})\n    await scheduler.start()\n    ...\n    await scheduler.stop()\n\n```\n\nAlternatively you can use the scheduler contextually.\n\n```python\nasync def main():\n    async with Scheduler() as scheduler:\n        scheduler.schedule_task(call_async_procedure, interval_s=10, args=(1, 2), kws={'value': True})\n```\n\n`Scheduler.schedule_task` returns a task object which you can enable / disable or supress the task execution in\nyour code temporarily using `task.suspend` context.  You can also access the previous call results from `task.result` attribute.\n\n```python\n\nclass Cache:\n\n    def __init__(self, scheduler: Scheduler):\n        self._scheduler = scheduler\n        self._cache_task = self._scheduler.schedule_task(\n            self.cache_all, interval_s=600, policy=scheduler.ExecPolicy.WAIT)\n\n    async def cache_all(self):\n        ...\n\n    async def reconfigure_cache(self):\n        async with self._cache_task.suspend():\n            \"Do something while the caching is suspended\"\n\n```\n\nYou can specify retries for common types of errors such as `IOError` or `ConnectionError` using `retries` parameter.\nThe scheduler will try to retry the call on such type of error.\n\n```python\nscheduler.schedule_task(call_async_procedure, interval_s=300, retries=3, retry_interval_s=1)\n```\n\nThere are various policies considering task execution.\nSee the [reference](https://kaiju-scheduler.readthedocs.io/reference.html) for more info on that.\n\n# Server\n\nThere's also a simple 'server' for handling asyncio tasks inside Python. It extends the standard loop functionality\nwith retries, timeouts and impose some rate limit and prevent the loop from growing infinitely.\n\nThe server returns an `asyncio.Task` object which can be awaited independently. The idea is that any error is not\nraised but instead returned inside of the result. This allows for more convenient handling of errors while using this\nin streams, queues and server applications.\n\nSee the [reference](https://kaiju-scheduler.readthedocs.io/reference.html) for more info on server functions.\n\n```python\nfrom kaiju_scheduler import Server\n\n\nasync def call_something(arg1: int, arg2: int):\n    return arg1 + arg2\n\n\nasync def main():\n    async with Server() as server:\n        task = await server.call(call_something, [1, 2])\n        await task\n\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Asynchronous tasks scheduler and executor",
    "version": "0.1.4",
    "project_urls": {
        "Homepage": "https://github.com/violet-black/kaiju-scheduler"
    },
    "split_keywords": [
        "scheduler",
        " asyncio",
        " tasks"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2cc60b72532a35bf34ff394c0e6fe7674d78b8eca8ae6a01a33a7241b139b4a4",
                "md5": "27c9bef3903105d0c28fafcef68649c5",
                "sha256": "a2047818f025be5e46976b99cf16c0e7eb61c3ddf23d2a938da493b2c24dc095"
            },
            "downloads": -1,
            "filename": "kaiju_scheduler-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "27c9bef3903105d0c28fafcef68649c5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 12231,
            "upload_time": "2024-05-08T10:20:35",
            "upload_time_iso_8601": "2024-05-08T10:20:35.126016Z",
            "url": "https://files.pythonhosted.org/packages/2c/c6/0b72532a35bf34ff394c0e6fe7674d78b8eca8ae6a01a33a7241b139b4a4/kaiju_scheduler-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-08 10:20:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "violet-black",
    "github_project": "kaiju-scheduler",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "kaiju-scheduler"
}
        
Elapsed time: 1.62699s