You can use the current_worker_task property from the Celery application instance class. The docs are here.
Having defined the following task:
# whatever_app/tasks.py celery_app = Celery(app) @celery_app.task def test_task(): if celery_app.current_worker_task: return 'running in a celery worker' return 'just running'
You can run the following in a python shell:
In [1]: from whatever_app.tasks import test_task In [2]: test_task() Out[2]: 'just running' In [3]: r = test_task.delay() In [4]: r.result Out[4]: u'running in a celery worker'
Note. Obviously, for test_task.delay() to succeed, you need to have at least one celery worker who works and is configured to load tasks from whatever_app.tasks .
source share