# 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": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "django, signals, celery",
"author": "Janusz Kamie\u0144ski",
"author_email": "200957+ivellios@users.noreply.github.com",
"download_url": "https://files.pythonhosted.org/packages/d7/ae/7de63a3a97164390672113d363140fd1d3f9fc09b3ca207b1e8f5f6a1705/celery_signal_receivers-0.2.1.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.2.1",
"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": "7d49e13d7207f4d029e5ef9839653efeb6ea33289d7f0f18ee361acd82a6b0ed",
"md5": "f27deec7fefbfebc9872cf12c60991a7",
"sha256": "b7e9a063b010f94d10f379acd30b0cb062ec343c74c5a2fa9edc7f8bf52e82cf"
},
"downloads": -1,
"filename": "celery_signal_receivers-0.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f27deec7fefbfebc9872cf12c60991a7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 4406,
"upload_time": "2024-10-10T17:38:24",
"upload_time_iso_8601": "2024-10-10T17:38:24.836333Z",
"url": "https://files.pythonhosted.org/packages/7d/49/e13d7207f4d029e5ef9839653efeb6ea33289d7f0f18ee361acd82a6b0ed/celery_signal_receivers-0.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d7ae7de63a3a97164390672113d363140fd1d3f9fc09b3ca207b1e8f5f6a1705",
"md5": "00e29534cd539791fcd2d774deabef8a",
"sha256": "d4b7e9eb44e3d97481bc023095387ff1509eb047b04ebac995e74f989d90a0e2"
},
"downloads": -1,
"filename": "celery_signal_receivers-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "00e29534cd539791fcd2d774deabef8a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 4098,
"upload_time": "2024-10-10T17:38:26",
"upload_time_iso_8601": "2024-10-10T17:38:26.244706Z",
"url": "https://files.pythonhosted.org/packages/d7/ae/7de63a3a97164390672113d363140fd1d3f9fc09b3ca207b1e8f5f6a1705/celery_signal_receivers-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-10 17:38:26",
"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"
}