gpt4 book ai didi

另一个函数内部的 Flask Celery update_state

转载 作者:行者123 更新时间:2023-12-03 15:55:20 25 4
gpt4 key购买 nike

我想从另一个函数更新我的 Celery 任务的状态。这是我现在所拥有的:

路线

@app.route('/my-long-function', methods=['POST'])
def my_long_function():

param1 = request.form['param1']
param2 = request.form['param2']

task = outside_function.delay(param1, param2)

return task.id

Celery Task - 在后台启动 some_python_script.handle
@celery.task(name='outside_function')
def outside_function(param1, param2):
with app.app_context():
some_python_script.handle(param1, param2)

some_python_script.handle:
def handle(param1, param2):
param1 + param2
# many, many different things

理想情况下,我希望能够 self.update_state celery 任务,以便我可以轻松地从我的应用程序请求其状态,如下所示:

some_python_script.handle(理想情况下):
def handle(param1, param2):
param1 + param2
# many, many different things
self.outside_function.update_state('PROGRESS', meta = {'status':'progressing'})

检查进度(理想情况下):
@app.route('/status/<task_id>')
def taskstatus(task_id):
task = outside_function.AsyncResult(task_id)
response = {
'state': task.state,
'id': task.id,
'status' : task.status,
}

return jsonify(response)

或者类似的东西。感谢您的帮助,我对 celery 很陌生!

最佳答案

您应该声明用于调用的任务 ID。
您可以查看 update_state .

下面的代码应该可以工作。

# capture id of celery task
ID = self.request.id

def handle(param1, param2):
param1 + param2
# many, many different things
# update the state of celery task with direct reference to it
self.update_state(task_id=ID, state='PROGRESS', meta = {'status':'progressing'})

关于另一个函数内部的 Flask Celery update_state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40259779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com