From 5350f8327554832f97e0b530936923a5c0af463e Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 31 Aug 2016 16:48:12 +0800 Subject: [PATCH] Add celery usage more --- apps/common/README.md | 7 +++++++ apps/common/celery.py | 1 - apps/ops/tasks.py | 8 +++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/common/README.md b/apps/common/README.md index 6305cd50e..6aac06589 100644 --- a/apps/common/README.md +++ b/apps/common/README.md @@ -22,6 +22,7 @@ from __future__ import absolute_import import time from celery import shared_task +from common import celery_app @shared_task @@ -31,6 +32,12 @@ def longtime_add(x, y): time.sleep(5) print 'long time task finished' return x + y + + +@celery_app.task(name='hello-world') +def hello(): + print 'hello world!' + ``` #### Run celery in development diff --git a/apps/common/celery.py b/apps/common/celery.py index 09afd03b5..9d006fe4a 100644 --- a/apps/common/celery.py +++ b/apps/common/celery.py @@ -17,6 +17,5 @@ app = Celery('jumpserver') # pickle the object when using Windows. app.config_from_object('django.conf:settings') -print(settings.BROKER_URL) app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS]) diff --git a/apps/ops/tasks.py b/apps/ops/tasks.py index 867efbcaa..ae4b23f01 100644 --- a/apps/ops/tasks.py +++ b/apps/ops/tasks.py @@ -2,6 +2,7 @@ from __future__ import absolute_import import time from celery import shared_task +from common import celery_app @shared_task @@ -10,4 +11,9 @@ def longtime_add(x, y): # sleep 5 seconds time.sleep(5) print 'long time task finished' - return x + y \ No newline at end of file + return x + y + + +@celery_app.task(name='hello-world') +def hello(): + print('hello world!')