简体   繁体   English

查询任务状态-Celery和Redis

[英]Query task state - Celery & redis

Okay so I have a relatively simple problem I think, and it's like I'm hitting a brick wall with it. 好的,我认为我有一个相对简单的问题,就像我用砖墙撞墙一样。 I have a flask app, and a webpage that allows you to run a number of scripts on the server side using celery & redis(broker). 我有一个flask应用程序,还有一个网页,允许您使用celery&redis(broker)在服务器端运行许多脚本。

All I want to do, is when I start a task to give it a name/id (task will be portrayed as a button on the client side) ie 我要做的就是在启动任务时为其命名/ ID(任务将在客户端显示为按钮),即

@app.route('/start_upgrade/<task_name>')
def start_upgrade(task_name):
    example_task.delay(1, 2, task_name=task_name)

Then after the task has kicked off I want to see if the task is running/waiting/finished in a seperate request, preferably like; 然后,在任务启动之后,我想查看任务是否正在单独的请求中运行/等待/完成,最好是这样;

@app.route('/check_upgrade_status/<task_name>')
def get_task_status(task_name):
    task = celery.get_task_by_name(task_name)  
    task_state = task.state
    return task_state # pseudocode

But I can't find anything like that in the docs. 但是我在文档中找不到类似的东西。 I am very new to celery though just FYI so assume I know nothing. 我对芹菜非常陌生,尽管仅供参考,所以假设我一无所知。 Also just to be extra obvious, I need to be able to query the task state from python, no CLI commands please. 同样要特别明显的是,我需要能够从python查询任务状态,请不要使用CLI命令。

Any alternative methods of achieving my goal of querying the queue are also welcome. 也欢迎实现我的查询队列目标的任何其他方法。

When you start a task with delay or apply_async an object AsyncResult is created and contains the id of the task. 当您启动具有delayapply_async的任务时,将创建一个对象AsyncResult ,其中包含该任务的ID。 To get it you just have to store it in a variable. 要获取它,您只需要将其存储在变量中即可。

For example 例如

@app.route('/start_upgrade/<task_name>')
def start_upgrade(task_name):
    res = example_task.delay(1, 2, task_name=task_name)
    print res.id

You can store this id and maybe associate it with something else in a database (or just print it like I did in the example). 您可以存储此ID,也可以将其与数据库中的其他ID相关联(或像在示例中一样打印它)。

Then you can check the status of your task in a python console with : 然后,您可以使用以下命令在python控制台中检查任务的状态:

from celery.result import AsyncResult
AsyncResult(your_task_id).status

Take a look at the result documentation you should get what you need there : http://docs.celeryproject.org/en/latest/reference/celery.result.html 看看结果文档,您应该在那里找到所需的内容: http : //docs.celeryproject.org/en/latest/reference/celery.result.html

I ended up figuring out a solution for my question from arthur's post. 我最终从亚瑟的岗位上找到了解决我的问题的方法。

In conjunction with redis I created these functions 结合redis我创建了这些功能

import redis
from celery.result import AsyncResult

redis_cache = redis.StrictRedis(host='localhost', port=6379, db=0)

def check_task_status(task_name):
    task_id = redis_cache.get(task_name)
    return AsyncResult(task_id).status

def start_task(task, task_name, *args, **kwargs):
    response = task.delay(*args, **kwargs)
    redis_cache.set(task_name, response.id)

Which allows me to define specific names to tasks. 这使我可以为任务定义特定的名称。 Note I haven't actually tested this yet but it makes sense so. 请注意,我还没有实际测试过,但这是有道理的。

Example usage; 用法示例;

start_task(example_task, "example_name", 1, 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM