我正在使用 http://python-rq.org/在 Heroku worker 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
谢谢!
最佳答案
如果您手头有作业实例
job.cancel()
或者如果您可以确定散列:
from rq import cancel_job
cancel_job('2eafc1e6-48c2-464b-a0ff-88fd199d039c')
但这只是将它从队列中移除;我不知道它是否会在已经执行的情况下杀死它。
您可以让它记录挂钟时间,然后定期检查自身并在一段时间后引发异常/自毁。
对于手动的、临时的风格,死亡:如果你安装了 redis-cli
,你可以做一些像 flushall queues 和 jobs 这样激烈的事情:
$ redis-cli
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> exit
I'm still digging around the documentation尝试找出如何进行精确杀伤。
不确定这是否对任何人有帮助,因为这个问题已经 18 个月了。
关于python - 取消 Python RQ 中已经执行的任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793879/