从Celery任务中提取运行时和完成时间:

时间:2015-11-22 21:12:10

标签: rabbitmq celery

在运行芹菜任务后,我将以下消息传递给stdout:

[2015-11-22 21:08:52,158: INFO/MainProcess] Task jobs.tasks.some_task[9c37f17b-dec4-4cb0-ab2b-fb5e997e430a] 
succeeded in 2.9128301960008685s: 0.9731072908536255

我想以编程方式提取完成时间(此处:2015-11-22 21:08:52)和运行时(2.9128301960008685s)并将其显示给用户。

如何从AsyncResult

中提取这些值

经纪人是RabbitMQ。

1 个答案:

答案 0 :(得分:0)

使用celery任务事件来获取任务运行时和完成时间。有关任务事件的更多详细信息,请参阅此link

from celery import Celery
import datetime
taskId_startTime = {}
taskId_addedTime = {}

def my_monitor():
    app = Celery('vwadaptor', broker='redis://workerdb:6379/0',backend='redis://workerdb:6379/0')
    state = app.events.State()

    def announce_task_succeeded(event):
        state.event(event)
        task = state.tasks.get(event['uuid'])
        print "runtime: ", task.runtime
        print "time of completion: ", datetime.datetime.now() 

    with app.connection() as connection:
        recv = app.events.Receiver(connection, handlers={
                'task-succeeded': announce_task_succeeded, 
        })
        recv.capture(limit=None, timeout=None, wakeup=True)


my_monitor()