Skip to main content

Extension for the Django signal receivers to process them asynchronously as the Celery tasks.

Project description

Celery Signal Receivers

Make Django signals asynchronous with Celery tasks. This package allows you to convert and write signal receivers to run in the background as Celery tasks.

Installation

The package is available on PyPI:

pip install celery-signal-receivers

Configuration

In order to use this package you need to set the path to the celery app object in Django settings:

EVENT_SIGNALS_CELERY_APP = 'myproject.celery.app'

Usage

The package is using Django Unified Signals for passing the message object from sender to receiver. The message object is always expected to be passed when sending the signal. That way receiver knows what type of message will be received. This package automates the process of checking if the send message is following the contract.

Let's start by defining the message structure. It can be any class you want.

import dataclasses

@dataclasses.dataclass
class ProfileMessage:
    id: int
    name: str

Now that we have the message structure defined, we can create the signal. We will use UnifiedSignal class for that:

from unified_signals import UnifiedSignal

profile_updated_signal = UnifiedSignal(ProfileMessage)

See the documentation of Django Unified Signals to learn more about sending the signal with standardized message.

Let's now write receiver for the signal. We will use the celery_signal_receivers.receiver decorator to convert the receiver to Celery task.

from celery_signals import receiver_task

@receiver_task(profile_updated_signal)
def handle_profile_updated(sender, message: ProfileMessage, **kwargs):
    print(message.id)
    print(message.name)
    ...

The above task will be executed by celery worker for the handle_profile_updated task. This function works as any other celery task, so you can route it to specific queue, set the priority, etc.

app.conf.task_routes = {
    'handle_profile_updated': {'queue': 'profile-updated-queue'},
}

Options

You can also pass the celery options to the task using the param in the decorator:

@receiver_task(profile_updated_signal, celery_task_options={'queue': 'profile-updated-queue'})
def foo(sender, message: ProfileMessage, **kwargs):
    ...

The decorator also accepts all other keyword arguments as regular django.dispatch.receiver decorator (ie. same as Signal.connect. For example you can set the dispatch_uid to avoid registering the same receiver multiple times.

@receiver_task(profile_updated_signal, dispatch_uid='profile_updated')
def foo(sender, message: ProfileMessage, **kwargs):
    ...

Limitations

For now this package does not support multiple signals passed to the @receiver_task decorator. You should create separate receivers for each signal. This may be added in the future.

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

celery_signal_receivers-0.2.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

celery_signal_receivers-0.2.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file celery_signal_receivers-0.2.1.tar.gz.

File metadata

  • Download URL: celery_signal_receivers-0.2.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.2 Windows/11

File hashes

Hashes for celery_signal_receivers-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d4b7e9eb44e3d97481bc023095387ff1509eb047b04ebac995e74f989d90a0e2
MD5 00e29534cd539791fcd2d774deabef8a
BLAKE2b-256 d7ae7de63a3a97164390672113d363140fd1d3f9fc09b3ca207b1e8f5f6a1705

See more details on using hashes here.

File details

Details for the file celery_signal_receivers-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for celery_signal_receivers-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7e9a063b010f94d10f379acd30b0cb062ec343c74c5a2fa9edc7f8bf52e82cf
MD5 f27deec7fefbfebc9872cf12c60991a7
BLAKE2b-256 7d49e13d7207f4d029e5ef9839653efeb6ea33289d7f0f18ee361acd82a6b0ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page