How to check task status in celery?

How to check if a task is performed in celery (in particular, I use celery django)?

I read the documentation and I googled, but I don't see a call like:

my_example_task.state() == RUNNING 

My use case is that I have an external (java) service for transcoding. When I send a document to be transcoded, I want to check if the work that runs this service is running, and if not, (restart).

I use the current stable versions - 2.4, I think.

+85
python web-services celery django-celery
Jan 27 2018-12-12T00:
source share
8 answers

Each Task object has a .request property that contains an AsyncRequest object. Accordingly, the following line gives the status of the Task task:

 task.AsyncResult(task.request.id).state 
+57
Jan 28 2018-12-12T00:
source share

Return task_id (which is specified from .delay ()) and then ask the celery instance for status:

 x = method.delay(1,2) print x.task_id 

When prompted, get a new AsyncResult using this task_id:

 from celery.result import AsyncResult res = AsyncResult("your-task-id") res.ready() 
+83
Jan 27 2018-12-12T00:
source share

Creating an AsyncResult object from a task identifier is the recommended method in the FAQ to get the status of a task when the only thing you have is a task identifier.

However, compared to Celery 3.x, there are significant reservations that can bite people if they do not pay attention to them. It really depends on the specific use case.

By default, celery does not record the "running" state.

In order for the celery to record that the task is running, you must set CELERY_TRACK_STARTED to True . Here is a simple task that tests this:

 @app.task(bind=True) def test(self): print self.AsyncResult(self.request.id).state 

If CELERY_TRACK_STARTED is False , the default is to show PENDING status, even if the task is running. If you set CELERY_TRACK_STARTED to True , then the state will be STARTED .

PENDING means "I don't know."

An AsyncResult with a PENDING state does not mean anything except that celery does not know the status of the task. This may be due to any number of reasons.

On the one hand, AsyncResult can be constructed with invalid task identifiers. Such "tasks" will be considered pending Celery:

 >>> task.AsyncResult("invalid").status 'PENDING' 

Ok, so no one will feed explicitly invalid identifiers until AsyncResult . Fairly enough, it also has the effect that AsyncResult also consider a task that has successfully completed, but that Celery has forgotten as PENDING . Again, in some use cases this can be a problem. Part of the problem depends on how Celery is configured to save task results, because it depends on the presence of “tombstones” in the results database. (“Tombstones” is the term used in the celery documentation for data blocks that record how the task ended.) Using AsyncResult will not work at all if CELERY_IGNORE_RESULT is True . A more annoying problem is that celery destroys tombstones by default. By default, CELEREY_TASK_RESULT_EXPIRES set to 24 hours. Therefore, if you run the task and record the identifier in the long-term storage, and more than 24 hours later, you create AsyncResult with it, the status will be PENDING .

All "real tasks" begin in the PENDING state. Therefore, getting a PENDING task may mean that the task was requested, but never moved further than this (for any reason). Or it may mean that the problem is solved, but Celery forgot about his condition.

Oh! AsyncResult will not work for me. What else can I do?

I prefer to track goals, but to keep track of my tasks. I keep some information about the task, but it really is secondary to tracking goals. The targets are stored in the vault independently of Celery. When a request needs to perform calculations, it depends on the achievement of a specific goal, it checks whether the goal has been achieved, if so, it uses this cached goal, otherwise it starts a task that will affect the goal and sends the client that made HTTP -request with an answer that indicates that he should wait for the result.

+55
Jul 08 '16 at 13:30
source share

You can also create custom states and update the execution of a task to complete a value task. This example is from the docs:

 @app.task(bind=True) def upload_files(self, filenames): for i, file in enumerate(filenames): if not self.request.called_directly: self.update_state(state='PROGRESS', meta={'current': i, 'total': len(filenames)}) 

http://celery.readthedocs.org/en/latest/userguide/tasks.html#custom-states

+15
Jan 03 '15 at 4:47
source share

Old question, but I recently ran into this problem.

If you are trying to get task_id, you can do it like this:

 import celery from celery_app import add from celery import uuid task_id = uuid() result = add.apply_async((2, 2), task_id=task_id) 

Now you know exactly what task_id is, and now you can use it to get AsyncResult:

 # grab the AsyncResult result = celery.result.AsyncResult(task_id) # print the task id print result.task_id 09dad9cf-c9fa-4aee-933f-ff54dae39bdf # print the AsyncResult status print result.status SUCCESS # print the result returned print result.result 4 
+9
Jul 10 '16 at 1:35
source share

Try:

task.AsyncResult(task.request.id).state

this will provide Celery Tasks status. If the Celery Task is already in FAILURE state, it will throw an exception:

raised unexpected: KeyError('exc_type',)

0
May 7 '16 at 5:44
source share

for simple tasks, we can use http://flower.readthedocs.io/en/latest/screenshots.html and http://policystat.imtqy.com/jobtastic/ to perform monitoring.

and for complex tasks, say, a task that deals with a large number of other modules. We recommend manually recording the progress and message on a specific task block.

0
Mar 31 '17 at 6:18
source share

I found useful information in

Celery Project Worker's Guide

In my case, I check if Celery works.

 inspect_workers = task.app.control.inspect() if inspect_workers.registered() is None: state = 'FAILURE' else: state = str(task.state) 

You can play with verification to suit your needs.

0
Jul 12 '17 at 22:53 on
source share



All Articles