Celery - activate task via command line or HTTP requests

I have a predefined celery task in my code, say my_proj.tasks.my_celery_task

I want to activate a task using a command line / HTTP request (not through my application).

I have searched for docs (saw and curl options) but there is no real example of calling a predefined task. How do you achieve this?

+3


source to share


1 answer


Assuming you've installed Celery with Rabbitmq, here's a simple example.

Define the task: my_app.py

from celery import Celery

app = Celery('tasks', backend='amqp', broker='amqp://')

@app.task
def add(x, y):
    return x + y

      

Start the worker:

celery worker -l info -A my_app

      

Start flower



flower -A my_app

      

Add task to queue via command line

curl -X POST -d '{"args":[1,2]}' http://localhost:5555/api/task/async-apply/my_app.add

      

or through requests

import requests, json
api_root = 'http://localhost:5555/api'
task_api = '{}/task'.format(api_root)
args = {'args': [1, 2]}
url = '{}/async-apply/my_app.add'.format(task_api)
print(url)
resp = requests.post(url, data=json.dumps(args))
reply = resp.json()
reply

      

+13


source







All Articles