-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Django: integrate Celery task queue to handle asynchronous tasks
Integrate Celery task queue. Redis serves as the message broker for the Celery task queue. Redis can run in a seperate container with of-the-shelf available images. Gevent is used an efficient alternative compared to multiple python processes, where each process has to load it's own runtime resulting in high memory requirement. Celery is used for functions that wait for ReST API calls to Leshan, an IO-bound task. Currently, Celery starts 1 worker (1 CPU), however, this worker is sufficient to handle more than 100 simultaneous ReST API calls efficiently. Signed-off-by: Jonas Remmert <jremmert@gmx.net>
- Loading branch information
Showing
6 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This will make sure the app is always imported when | ||
# Django starts so that shared_task will use this app. | ||
from .celery import app as celery_app | ||
|
||
__all__ = ('celery_app',) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
from celery import Celery | ||
|
||
# Set the default Django settings module for the 'celery' program. | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings') | ||
|
||
app = Celery('server') | ||
|
||
# Using a string here means the worker doesn't have to serialize | ||
# the configuration object to child processes. | ||
# - namespace='CELERY' means all celery-related configuration keys | ||
# should have a `CELERY_` prefix. | ||
app.config_from_object('django.conf:settings', namespace='CELERY') | ||
|
||
# Load task modules from all registered Django apps. | ||
app.autodiscover_tasks() | ||
|
||
|
||
@app.task(bind=True, ignore_result=True) | ||
def debug_task(self): | ||
print(f'Request: {self.request!r}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters