在运行一个芹菜任务后,我通过管道将以下消息发送到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。
使用芹菜任务事件来获取任务运行时间和完成时间。有关任务事件的更多详细信息,请参阅此链接。
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()