停止在Django中由Celery工作者当前正在运行的任务。

huangapple go评论67阅读模式
英文:

Stop the currently running task by celery worker in Django

问题

我有一个Django应用程序和React作为我的前端框架,我正在使用celery来运行需要很长时间的任务,一切都正常工作。

同时,我通过celery信号提供了每个任务的实时状态,我还创建了一个数据库表,用于存储所有正在运行的任务的状态,比如任务何时创建,我使用参数创建一行,它们是唯一的,所以如果具有相同参数的任务已经在运行或在队列中,则不会再次运行,因此用户界面很好。

但是有一些任务需要停止,用户也有权利和权限停止正在运行的任务,因为我正在更新每个用户的任务。

现在,如果任务在队列中,我可以使用revoke来阻止它被执行。

但问题是,我有一些非常耗时的任务,所以用户可能想要停止任务,专注于其他任务。

我想停止当前正在工作的任务,假设任务T1正在工作线程执行,我想要停止它,我已经查看了互联网的各个部分,但找不到方法来实现这一点。

用户可能希望停止任务,我希望为用户提供停止当前运行任务的功能。

我还查看了不同的任务管理模块,如Dramatiq,但仍然无法停止当前正在工作的任务。

任何帮助都将是很好的。谢谢。

英文:

I have a Django application and React as my frontend framework and I am using celery for running lengthy tasks which do take a lot of time and everything is working fine.

Also I am providing real time status of the each task running on the celery via celery signals and all . Also I have created a database table which stores the status of all the tasks that are running , like when the task is created , i create a row with the parameters and they are unique so that task with same parameters won't run if its already running or is in the queue , so the user interface is good .
But there are few tasks which need to stop and also the user have the authority and permission to stop the tasks that are running as i am updating every user for the tasks.
Now if the task is in the queue i can use revoke to stop the from being executed .

from celery.task.control import revoke
revoke(task_id, terminate=True)

But the thing is that , i have tasks which are very lengthy , so the user may want to stop the task and focus on other tasks .

I want to stop the current task which is currently being run by the worker .
Suppose a task T1 is being executed by the worker , i want to stop that , I have seen every part of internet but could not find a way to do so .

User may want to stop the task and I want to provide that functionality to user for stopping the current running task.

I have also viewed different task management modules like Dramatiq but still you cannot stop the current working task . I have all the data to stop the task like task id and all , but could not find a way to do so .

Any help would be great . Thanks .

答案1

得分: 1

发送SIGKILL信号给实际执行任务的子工作进程是一种粗暴的方法之一。如果子进程被杀死,主工作进程将重新启动子进程并执行其他任务。

要发送信号,您必须在运行celery的服务器上手动向PID发出SIGKILL。您必须有一些其他进程(可能是API)来监听您的命令并发送SIGKILL。

英文:

One of the dirty ways of doing this is by sending a SIGKILL to the child worker process that is actually executing the task. Main worker process will respawns the child if it gets killed and will execute other tasks.

For sending a signal, you have to manually issue a SIGKILL to the PID from the server where celery is running. You have to have some other process(probably API) listening for your commands and send SIGKILL.

huangapple
  • 本文由 发表于 2023年7月24日 15:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定