运行 成功执行一个函数后的另一个任务使用 Celery
Run another task after executing one function successfully Using Celery
我是 celery
模块的新手,我想在成功执行特定功能后执行一个任务。
我在我的 Django 应用程序中做了以下更改:
更改settings.py
:
import djcelery
djcelery.setup_loader()
BROKER_URL = 'amqp://rahul:amvarish@127.0.0.1:5672//'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_IMPORTS = ('projectmanagement.tasks',)
创建 tasks.py
:
from celery import task
@task()
def add(x, y):
print (x+y)
return x + y
我的view.py
:
class Multiply(APIView):
def get(self,request):
x = request.GET['x']
y = request.GET['y']
try:
z= x*y
data = {'success':True,'msg':'x and y multiply','result':z}
return HttpResponse(json.dumps(data),content_type="application/json")
except Exception,e:
print str(e)
data = {'success':False,'msg':'Error in multiplying x and y'}
return HttpResponse(json.dumps(data),content_type="application/json")
现在我希望我的 celery 任务在我的 multiply
方法成功执行后被调用。
我应该在我的视图函数中的什么地方调用我的任务,这样我的 API 响应将独立于 celery 任务执行?
您可以使用 .apply_async
调用您的任务,使调用异步,结果如下执行图:
|
|
normal flow
|
| async
my_task.apply_async -------> do my task_stuff
| call
|
flow continues
without waiting on my_task execution
|
...
从上述推导,在你的代码中你应该调用你的添加方法如下:
from path.to.your.tasks import add
class Multiply(APIView):
def get(self,request):
x = request.GET['x']
y = request.GET['y']
try:
z= x*y
add.apply_async(x, y) # will execute independently
data = {'success':True,'msg':'x and y multiply','result':z}
return HttpResponse(json.dumps(data),content_type="application/json")
except Exception,e:
print str(e)
data = {'success':False,'msg':'Error in multiplying x and y'}
return HttpResponse(json.dumps(data),content_type="application/json")
我是 celery
模块的新手,我想在成功执行特定功能后执行一个任务。
我在我的 Django 应用程序中做了以下更改:
更改settings.py
:
import djcelery
djcelery.setup_loader()
BROKER_URL = 'amqp://rahul:amvarish@127.0.0.1:5672//'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_IMPORTS = ('projectmanagement.tasks',)
创建 tasks.py
:
from celery import task
@task()
def add(x, y):
print (x+y)
return x + y
我的view.py
:
class Multiply(APIView):
def get(self,request):
x = request.GET['x']
y = request.GET['y']
try:
z= x*y
data = {'success':True,'msg':'x and y multiply','result':z}
return HttpResponse(json.dumps(data),content_type="application/json")
except Exception,e:
print str(e)
data = {'success':False,'msg':'Error in multiplying x and y'}
return HttpResponse(json.dumps(data),content_type="application/json")
现在我希望我的 celery 任务在我的 multiply
方法成功执行后被调用。
我应该在我的视图函数中的什么地方调用我的任务,这样我的 API 响应将独立于 celery 任务执行?
您可以使用 .apply_async
调用您的任务,使调用异步,结果如下执行图:
|
|
normal flow
|
| async
my_task.apply_async -------> do my task_stuff
| call
|
flow continues
without waiting on my_task execution
|
...
从上述推导,在你的代码中你应该调用你的添加方法如下:
from path.to.your.tasks import add
class Multiply(APIView):
def get(self,request):
x = request.GET['x']
y = request.GET['y']
try:
z= x*y
add.apply_async(x, y) # will execute independently
data = {'success':True,'msg':'x and y multiply','result':z}
return HttpResponse(json.dumps(data),content_type="application/json")
except Exception,e:
print str(e)
data = {'success':False,'msg':'Error in multiplying x and y'}
return HttpResponse(json.dumps(data),content_type="application/json")