Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Celery NOT throw an Exception when the underlying task throws one

Celery doesn't seem to be handling exceptions properly.

If I have task:

def errorTest():     raise Exception() 

and then I call

r = errorTest.delay() In [8]: r.result  In [9]: r.state Out[9]: 'PENDING' 

And it will hang like this indefinitely.

Going and checking the logs shows that the error IS getting thrown in the task (and if you want the message, ask), and I know that the backend and everything is set up properly because other tasks just work and return results correctly.

Is there something funky that I need to do to catch exceptions in Celery?

/Celery version is 3.0.13, broker is RabbitMQ running on my local machine

like image 656
Kevin Meyer Avatar asked Feb 04 '13 20:02

Kevin Meyer


People also ask

How do you handle exceptions thrown by tasks?

Exceptions are propagated when you use one of the static or instance Task. Wait methods, and you handle them by enclosing the call in a try / catch statement. If a task is the parent of attached child tasks, or if you are waiting on multiple tasks, multiple exceptions could be thrown.

Can a Celery task call another task?

To answer your opening questions: As of version 2.0, Celery provides an easy way to start tasks from other tasks. What you are calling "secondary tasks" are what it calls "subtasks".

How does Celery task queue work?

Celery communicates via messages, usually using a broker to mediate between clients and workers. To initiate a task, the Celery client adds a message to the queue, and the broker then delivers that message to a worker. The most commonly used brokers are Redis and RabbitMQ.


1 Answers

If you are running Celery with the CELERY_ALWAYS_EAGER set to True, then make sure you include this line in your settings too:

CELERY_EAGER_PROPAGATES_EXCEPTIONS = True 

http://docs.celeryproject.org/en/latest/configuration.html#celery-eager-propagates-exceptions

like image 157
seddonym Avatar answered Oct 11 '22 15:10

seddonym