Skip to main content

Apscheduler integration for dishka

Project description

APScheduler integration for Dishka

GitHub Actions Workflow Status

This package provides integration of Dishka dependency injection framework and APScheduler, an advanced Python scheduler.

Installation

pip install apscheduler-dishka

Features

  • automatic REQUEST scope management for scheduled jobs
  • automatic injection of dependencies into job functions
  • support for both sync and async schedulers
  • support for auto_inject mode to automatically wrap all jobs

How to use

  1. Import
from apscheduler_dishka import (
    inject,
    setup_dishka,
)
from dishka import make_container, Provider, provide, Scope, FromDishka
  1. Create provider
class YourProvider(Provider):
    @provide(scope=Scope.REQUEST)
    def create_interactor(self) -> Interactor:
         ...
  1. Mark those of your job function parameters which are to be injected with FromDishka[] and decorate them using @inject
@inject
def job(
    data: str,
    interactor: FromDishka[Interactor],
):
    ...
  1. Create container
container = make_container(YourProvider())
  1. Setup dishka integration
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler_dishka import setup_dishka

scheduler = BlockingScheduler()
setup_dishka(container=container, scheduler=scheduler)
  1. Add jobs to scheduler
scheduler.add_job(
    job,
    trigger="interval",
    seconds=5,
    args=["args"],
)

Auto-inject mode

If you don't want to manually decorate each job with @inject, you can use auto_inject mode:

setup_dishka(
    container=container,
    scheduler=scheduler,
    auto_inject=True,
)

In this mode, all jobs will be automatically wrapped with dependency injection:

def job(
    data: str,
    interactor: FromDishka[Interactor],
):
    ...

scheduler.add_job(
    job,
    trigger="interval",
    seconds=5,
    args=["args"],
)

Async support

The library supports async schedulers:

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from dishka import make_async_container
from apscheduler_dishka import setup_dishka

scheduler = AsyncIOScheduler()
container = make_async_container(YourProvider())
setup_dishka(container=container, scheduler=scheduler)

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

apscheduler_dishka-1.0.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

apscheduler_dishka-1.0.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file apscheduler_dishka-1.0.0.tar.gz.

File metadata

  • Download URL: apscheduler_dishka-1.0.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for apscheduler_dishka-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ac2939867636b3264c65f2dbdc054fc69b71f6bc87760300c2952698824633f9
MD5 75a83ba91a4748283c44c687fb0cb939
BLAKE2b-256 751349deaab37b974667289596b88d2ebc688e71fae2dd988411e59c8af39fdb

See more details on using hashes here.

File details

Details for the file apscheduler_dishka-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for apscheduler_dishka-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2e03a4afec923e2c959f0645571088b8d14a39b673c28c510714d2b7289973b
MD5 4e94c8ecc8069f0034e76f980768dfc5
BLAKE2b-256 062bb6dc5b6745fce425c9ac59cfe746e9f1e69b9c8d7bb07f6813b9df402cca

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