celery-signal-receivers


Namecelery-signal-receivers JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/ivellios/celery-signal-receivers
SummaryExtension for the Django signal receivers to process them asynchronously as the Celery tasks.
upload_time2023-09-22 09:56:00
maintainer
docs_urlNone
authorJanusz Kamieński
requires_python>=3.9,<4.0
licenseMIT
keywords django signals celery
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://pypi.org/project/celery-signal-receivers/):

```bash
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:

```python
EVENT_SIGNALS_CELERY_APP = 'myproject.celery.app'
```

# Usage

The package is using [Django Unified Signals](https://pypi.org/project/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.

```python
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:

```python
from unified_signals import UnifiedSignal

profile_updated_signal = UnifiedSignal(ProfileMessage)
```

See the [documentation of Django Unified Signals](https://pypi.org/project/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.

```python
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.

```python
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:

```python
@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](https://docs.djangoproject.com/en/4.2/topics/signals/#django.dispatch.Signal.connect). For example you can set the `dispatch_uid` to avoid registering the same receiver multiple times.

```python
@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. 

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ivellios/celery-signal-receivers",
    "name": "celery-signal-receivers",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9,<4.0",
    "maintainer_email": "",
    "keywords": "django,signals,celery",
    "author": "Janusz Kamie\u0144ski",
    "author_email": "200957+ivellios@users.noreply.github.com",
    "download_url": "https://files.pythonhosted.org/packages/98/09/8619fa43945d40bf3fbc3fd35cb4d9c420f8350c880862bda6d1b53dbdd9/celery_signal_receivers-0.1.0.tar.gz",
    "platform": null,
    "description": "# Celery Signal Receivers\n\nMake Django signals asynchronous with Celery tasks. This package allows you to convert and write signal receivers to run in the background as Celery tasks.\n\n# Installation\n\nThe package is [available on PyPI](https://pypi.org/project/celery-signal-receivers/):\n\n```bash\npip install celery-signal-receivers\n```\n\n# Configuration\n\nIn order to use this package you need to set the path to the celery app object in Django settings:\n\n```python\nEVENT_SIGNALS_CELERY_APP = 'myproject.celery.app'\n```\n\n# Usage\n\nThe package is using [Django Unified Signals](https://pypi.org/project/django-unified-signals/)\nfor 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.\n\nLet's start by defining the message structure. It can be any class you want.\n\n```python\nimport dataclasses\n\n@dataclasses.dataclass\nclass ProfileMessage:\n    id: int\n    name: str\n```\n\nNow that we have the message structure defined, we can create the signal. We will use `UnifiedSignal` class for that:\n\n```python\nfrom unified_signals import UnifiedSignal\n\nprofile_updated_signal = UnifiedSignal(ProfileMessage)\n```\n\nSee the [documentation of Django Unified Signals](https://pypi.org/project/django-unified-signals/) to learn more about sending the signal\nwith standardized message.\n\nLet's now write receiver for the signal. We will use the `celery_signal_receivers.receiver` decorator to convert the receiver to Celery task.\n\n```python\nfrom celery_signals import receiver_task\n\n@receiver_task(profile_updated_signal)\ndef handle_profile_updated(sender, message: ProfileMessage, **kwargs):\n    print(message.id)\n    print(message.name)\n    ...\n```\n\nThe 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.\n\n```python\napp.conf.task_routes = {\n    'handle_profile_updated': {'queue': 'profile-updated-queue'},\n}\n```\n\n# Options\n\nYou can also pass the celery options to the task using the param in the decorator:\n\n```python\n@receiver_task(profile_updated_signal, celery_task_options={'queue': 'profile-updated-queue'})\ndef foo(sender, message: ProfileMessage, **kwargs):\n    ...\n```\n\nThe decorator also accepts all other keyword arguments as regular `django.dispatch.receiver` decorator (ie. same as [Signal.connect](https://docs.djangoproject.com/en/4.2/topics/signals/#django.dispatch.Signal.connect). For example you can set the `dispatch_uid` to avoid registering the same receiver multiple times.\n\n```python\n@receiver_task(profile_updated_signal, dispatch_uid='profile_updated')\ndef foo(sender, message: ProfileMessage, **kwargs):\n    ...\n```\n\n\n# Limitations\n\nFor now this package does not support multiple signals passed to the `@receiver_task` decorator. \nYou should create separate receivers for each signal.\nThis may be added in the future. \n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Extension for the Django signal receivers to process them asynchronously as the Celery tasks.",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/ivellios/celery-signal-receivers",
        "Repository": "https://github.com/ivellios/celery-signal-receivers"
    },
    "split_keywords": [
        "django",
        "signals",
        "celery"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e8e8c1be2ebd871b4ee7ce3b75f0d921233b49e961a2dffb5e24a179e9989b80",
                "md5": "029aba552ae6877a5b8183f540077a8e",
                "sha256": "09ce87c5b5f367414212fe6c5c8ffdb379f12f9da2c96d8e845bdb9d79428e96"
            },
            "downloads": -1,
            "filename": "celery_signal_receivers-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "029aba552ae6877a5b8183f540077a8e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<4.0",
            "size": 4402,
            "upload_time": "2023-09-22T09:55:58",
            "upload_time_iso_8601": "2023-09-22T09:55:58.291065Z",
            "url": "https://files.pythonhosted.org/packages/e8/e8/c1be2ebd871b4ee7ce3b75f0d921233b49e961a2dffb5e24a179e9989b80/celery_signal_receivers-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "98098619fa43945d40bf3fbc3fd35cb4d9c420f8350c880862bda6d1b53dbdd9",
                "md5": "09a466b956c77434c52a471b1a4f6c5b",
                "sha256": "e6fa6ff49f6fff2d5ca9a791118bc455273ed04b128cee0f0165c27637eda7c0"
            },
            "downloads": -1,
            "filename": "celery_signal_receivers-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "09a466b956c77434c52a471b1a4f6c5b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<4.0",
            "size": 3899,
            "upload_time": "2023-09-22T09:56:00",
            "upload_time_iso_8601": "2023-09-22T09:56:00.059242Z",
            "url": "https://files.pythonhosted.org/packages/98/09/8619fa43945d40bf3fbc3fd35cb4d9c420f8350c880862bda6d1b53dbdd9/celery_signal_receivers-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-22 09:56:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ivellios",
    "github_project": "celery-signal-receivers",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "celery-signal-receivers"
}
        
Elapsed time: 0.11550s