在 django 中中止 Celery 中正在运行的任务

发布于 2024-09-15 23:52:58 字数 344 浏览 3 评论 0原文

我希望能够中止从 Celery 队列运行的任务(使用rabbitMQ)。我使用 AsyncBoot 来调用该任务,

task_id = AsyncBoot.apply_async(args=[name], name=name, connect_timeout=3)

其中 AsyncBoot 是已定义的任务。

我可以获取任务 ID(假设这是 apply_async 返回的长字符串)并将其存储在数据库中,但我不确定如何调用中止方法。我了解如何使用 Abortable 任务类使方法可中止,但如果我只有任务 ID 字符串,如何在任务上调用 .abort() ?谢谢。

I would like to be able to abort a task that is running from a Celery queue (using rabbitMQ). I call the task using

task_id = AsyncBoot.apply_async(args=[name], name=name, connect_timeout=3)

where AsyncBoot is a defined task.

I can get the task ID (assuming that is the long string that apply_async returns) and store it in a database but I'm unsure how to call an abort method. I see how to make methods abortable with the Abortable tasks class but if I only have the task-id string, how do I call .abort() on the task? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-09-22 23:52:58

apply_async 返回一个 AsyncResult 实例,或者在本例中为 AbortableAsyncResult。保存 task_id 并稍后使用它实例化新的 AbortableAsyncResult,如果您不使用 default_backend,请确保提供后端可选参数。

abortable_async_result = AsyncBoot.apply_async(args=[name], name=name, connect_timeout=3)
myTaskId = abortable_async_result.task_id

之后:

abortable_async_result = AbortableAsyncResult(myTaskId)
abortable_async_result.abort()

apply_async returns an AsyncResult instance, or in this case an AbortableAsyncResult. Save the task_id and use that to instantiate a new AbortableAsyncResult later, making sure you supply the backend optional argument if you're not using the default_backend.

abortable_async_result = AsyncBoot.apply_async(args=[name], name=name, connect_timeout=3)
myTaskId = abortable_async_result.task_id

Later:

abortable_async_result = AbortableAsyncResult(myTaskId)
abortable_async_result.abort()
花海 2024-09-22 23:52:58

您看到参考文档了吗?
http://celeryq.org/docs/reference/celery.contrib.abortable。 html

要中止任务,请使用 result.abort()

>>> result = AsyncBoot.apply_async(...)
>>> result.abort()

Did you see the reference documentation?
http://celeryq.org/docs/reference/celery.contrib.abortable.html

To abort the task use result.abort():

>>> result = AsyncBoot.apply_async(...)
>>> result.abort()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文