使用 Redis 延迟执行/调度?
基于Redis做延迟任务执行(即调度)有什么技巧吗?
也许有一些聪明的方法可以将 BLPOP 延迟给定的秒数?...
Any tricks to do delayed task execution (i.e. scheduling) based on Redis?
Perhaps some clever way to delay BLPOP for a given number of seconds?..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想使用 Redis 进行调度,我建议使用排序集(z*)命令:
http://code.google.com/p/redis/wiki/SortedSets
你可以做的是这样的:
例如:
然后,每隔一段时间(最多每秒)你可以拉出预定的作业应该运行(或者现在应该已经运行):
繁荣,你有你的工作要运行。当然,请确保从排序集中删除已完成的作业。
If you want to do scheduling with redis, i would suggest using sorted set (the z*) commands:
http://code.google.com/p/redis/wiki/SortedSets
what you can do is something like this:
e.g:
Then, every so often (up to every second) you can pull scheduled jobs that should run (or should have run by now):
Boom, you got your jobs to run. Of course, make sure to delete done jobs from the sorted set.
您可以使用由多个列表组成的环,这些列表的名称中包含时间部分。作为时间部分,您可以采用当前秒数 (0-59)。
您始终将当前秒的任务添加到列表中。为了获得作业,您只能在那些保证内容早于给定秒数的列表上执行 BLPOP(具有低超时)。
如果您在多个主机上工作,则必须注意时钟同步(NTP)。
You can work with a ring of multiple LISTs that have a time component in their name. As time component you can take the current second (0-59).
You always add tasks to the list for the current second. To get the jobs you do a BLPOP (with low timeout) only on those lists where it is guaranteed, that the content is older than the given number of seconds.
If you work from multiple hosts you have to take care, that the clocks are in sync (NTP).
虽然 @efalcao 的答案非常好,但您的问题可能表明 redis 并不完全适合您的应用程序需求。
如果您的应用程序具有消息框的性质,请考虑使用 rabbitMQ,具有延迟消息功能;如果您胆子大的话,也可以使用 akka
While the @efalcao's answer is a very good one, your question might indicate that redis does not perfectly suit your application needs.
if your application has the nature of a message box, please consider using rabbitMQ, which features delayed messages or akka if you feel bold