gpt4 book ai didi

python - 从未知任务中检索 Celery 中 'task_id' 的结果

转载 作者:太空狗 更新时间:2023-10-29 20:42:16 26 4
gpt4 key购买 nike

如果我之前不知道执行了哪个任务,如何提取任务的结果?这是设置:给定以下来源('tasks.py'):

from celery import Celery

app = Celery('tasks', backend="db+mysql://u:p@localhost/db", broker = 'amqp://guest:guest@localhost:5672//')

@app.task
def add(x,y):
return x + y


@app.task
def mul(x,y):
return x * y

在本地运行 RabbitMQ 3.3.2:

marcs-mbp:sbin marcstreeter$ ./rabbitmq-server

RabbitMQ 3.3.2. Copyright (C) 2007-2014 GoPivotal, Inc.
## ## Licensed under the MPL. See http://www.rabbitmq.com/
## ##
########## Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log
###### ## /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log
##########
Starting broker... completed with 10 plugins.

在本地运行 Celery 3.1.12:

 -------------- celery@Marcs-MacBook-Pro.local v3.1.12 (Cipater)
---- **** -----
--- * *** * -- Darwin-13.2.0-x86_64-i386-64bit
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x105dea3d0
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: disabled
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery

然后我可以导入该方法并使用“task_id”检索结果:

from tasks import add, mul
from celery.result import AsyncResult

result = add.delay(2,2)
task_id = result.task_id
result.get() # 4

result = AsyncResult(id=task_id)
result.get() # 4

result = add.AsyncResult(id=task_id)
result.get() # 4

# and the same for the 'mul' task. Just imagine I put it here

在下一个示例中,我在进程之间拆分了这些步骤。在一个过程中,我像这样检索“task_id”:

from tasks import add

result = add.delay(5,5)
task_id = result.task_id

在另一个进程中,如果我使用相同的“task_id”(复制并粘贴到另一个 REPL,或在不同的 HTTP 请求中),如下所示:

from celery.result import AsyncResult

result = AsyncResult(id="copied_task_id", backend="db+mysql://u:p@localhost/db")
result.get() # AttributeError: 'str' object has no attribute 'get_task_meta'
result.state # AttributeError: 'str' object has no attribute 'get_task_meta'
result.status # AttributeError: 'str' object has no attribute 'get_task_meta'

如果我这样做,在另一个过程中:

from task import add # in this instance I know that an add task was performed

result = add.AsyncResult(id="copied_task_id")
result.status # "SUCCESSFUL"
result.state # "SUCCESSFUL"
result.get() # 10

我希望能够在事先不知道生成结果的任务的情况下获得结果。在我的真实环境中,我计划将此 task_id 返回给客户端,让他们通过 HTTP 请求查询他们的工作状态。

最佳答案

好的 - 所以我一直在寻找解决方案很长一段时间,现在我终于正式发布了这个并查看了 documentation我找到了 this gem :

class celery.result.AsyncResult(id, backend=None, task_name=None, app=None, parent=None)

Query task state.

Parameters:

id – see id.

backend – see backend.

异常 TimeoutError

Error raised for timeouts.

AsyncResult.app = 无

因此,我没有提供后端参数,而是像这样提供了“app”参数:

from celery.result import AsyncResult
from task import app

# Assuming add.delay(10,10) was called in another process
# and that I'm using a 'task_id' I retrieved from that process

result = AsyncResult(id='copied_task_id', app=app)
result.state # 'SUCCESSFUL'
result.get() # 20

这对很多人来说可能是显而易见的。这不是给我的。现在我只能说这个解决方案“有效”,但如果我知道这是被认可的方式,我会感觉更舒服。如果您知道文档中的某个部分使这一点更清楚,请将其张贴在评论中或作为答案,如果可以的话,我会选择它作为答案。

关于python - 从未知任务中检索 Celery 中 'task_id' 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24391054/

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