Question

I was trying to obtain the wait time of a Celery queued task. As far as I know, I can do so via the Event stream published by the Celery workers. In curiosity, why a simple timestamp is not embedded in the queued task? Is this related to distributed system design?

NOTE: celery==3.0.12

Was it helpful?

Solution

You cannot rely on physical wall time in distributed systems so such a field would not be very useful.

With event streams this is different since the task-started and task-succeeded timestamps are both from the same clock source.

That said, there are times when an approximate value can be useful so you can add this information yourself:

add.apply_async((4, 4), headers={'time_sent': time.time()})

You have access to the message headers in the task (but note that this requires Celery 3.1):

@app.task(bind=True)
def add(self, x, y):
    print('Approximate queue time: {0}'.format(
        time.time() - self.request.headers.get('time_sent', 0),
    ))
    return x + y
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top