英文:
GAE Golang - how to configure a backend to be doing a task every X seconds?
问题
在我的Google App Engine Go应用程序中,我需要每5-10秒执行一次任务。我了解使用标准的Cron无法实现这一点,因为它只能每分钟调度任务。类似地,像这样在一个线程中睡眠可能不是最好的选择。据我所知,我需要使用一些后端实例来执行这些任务。
如何配置我的应用程序以便及时且高效地处理这些任务?我只需让后端实例全天候运行并进行计时,直到需要执行某些操作,还是有其他更高效的方法来执行一个任务,然后安排另一个任务在5-10秒后运行?
英文:
In my Google App Engine Go application I need to perform a task every 5-10 seconds. I understand that using the standard Cron won't work, since that can only schedule tasks every minute. Similarly, sleeping in a thread like that might not be the best option. To the best of my knowledge, I need to use some backend instances to perform those tasks.
How do I configure my app to handle such tasks in a timely and resource-efficient fashion? Do I just run the backend 24/7 and tick away until I need to do something, or is there some more efficient way to perform a task and then schedule another task to run in 5-10 seconds?
答案1
得分: 2
这个问题突显了处理效率和服务质量(时间准确性)之间的紧张关系。最高效的方法是使用延迟或预计到达时间选项的任务队列(推送队列风格),但是API没有提供关于准确性的服务级别协议。最准确的方法是您描述的全天候后端,但其开销要高得多[编辑-并且AppEngine不能保证其正常运行时间]。
我使用基于Java的推送队列的经验是,超过90%的任务在计划时间的2秒内执行,而不到1%的任务会在10秒后开始。没有任务似乎会提前运行。
编辑-正如Zig从经验中指出的那样,AppEngine可能随时关闭后端,并且没有自动重新启动的方式。因此,后端的及时性会受到不可预测的停机时间的影响。
总之,任务队列高效可靠,但比后端的及时性差,后端的成本更高,可靠性更低。在大多数情况下,使用任务队列而不是后端。
英文:
This question highlights a tension between processing efficiency and quality of service (timing accuracy). The most efficient approach is a Task Queue (Push Queue style) using Delay or ETA options but the API offers no Service Level Agreement about accuracy. The most accurate approach is the 24/7 backend you described but its overheads are much higher [Edit - and AppEngine does not guarantee its uptime].
My experience with a Java based Push Queue is that over 90% of tasks execute within 2 seconds of the scheduled time and that under 1% of tasks start more than 10 seconds late. No tasks ever seem to run early.
Edit - as Zig points out from experience, AppEngine may shut down a backend at any time and provides no automatic way to restart it. Thus the promptness of a backend is spoiled by unpredictable spells of downtime.
In summary Task Queues are efficient and reliable but less timely than backends which are more costly and less reliable. Use Task Queues rather than backends in most cases.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论