取消Python RQ中已执行的任务?

时间:2013-05-28 13:53:05

标签: python heroku redis python-rq

我正在使用http://python-rq.org/在Heroku工作人员dynos上排队和执行任务。这些是长时间运行的任务,偶尔我需要在执行中期取消它们。我如何从Python中做到这一点?

from redis import Redis
from rq import Queue
from my_module import count_words_at_url

q = Queue(connection=Redis())
result = q.enqueue(
             count_words_at_url, 'http://nvie.com')

以后我想做的另一个过程:

from redis import Redis
from rq import Queue
from my_module import count_words_at_url

q = Queue(connection=Redis())
result = q.revoke_all() # or something

谢谢!

2 个答案:

答案 0 :(得分:3)

如果你手边有工作实例

job.cancel()

或者,如果您可以确定哈希:

from rq import cancel_job
cancel_job('2eafc1e6-48c2-464b-a0ff-88fd199d039c')

http://python-rq.org/contrib/

但是这只是将它从队列中删除;我不知道如果已经执行它会杀死它。

您可以让它记录墙上时间,然后定期检查自己并在一段时间后引发异常/自毁。

对于手动,临时风格,死亡:如果您安装了redis-cli,您可以做一些像flushall队列和工作那样激烈的事情:

$ redis-cli
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> exit

I'm still digging around the documentation试图找到如何进行精确杀戮。

不确定这是否对任何人都有帮助,因为这个问题已经有18个月了。

答案 1 :(得分:0)

我认为最常见的解决方案是让工作人员生成另一个线程/进程来执行实际工作,然后定期检查作业元数据。要终止任务,请在元数据中设置一个标志,然后让工作人员终止正在运行的线程/进程。