GAE:后端实例上的任务在没有警告的情况下被终止

2024-06-28 15:42:04 发布

您现在位置:Python中文网/ 问答频道 /正文

日间;夜间:

如何解决Appengine中的这个错误:有时is_shutting_down返回False,并且在一两秒钟内,实例将关闭?在

细节

我在googleappengine应用程序(Python)上有一个后端实例。后端实例用于生成报告,有时需要几分钟甚至几小时才能完成。在

为了处理意外关闭,我监视runtime.is_shutting_down(),并在is_shutting_down返回True时将报告的中间状态存储到数据库中。在

下面是我要检查的代码部分:

from google.appengine.api import runtime

#...

def my_report_function():
    #...
    # Check if we should interrupt and reschedule to avoid timeout error.
    duration_sec = time.time() - start
    too_long = MAX_SEC < duration_sec
    is_shutting_down = runtime.is_shutting_down()
    log.debug('Does this report iteration need to wrap it up soon? '
              'Too long? %s (%s sec). Shutting down? %s'
               % (too_long, duration_sec, is_shutting_down))
    if too_long or is_shutting_down:
        # save the state of report, reschedule next iteration, and return

有时它可以工作,但有时我在Appengine日志中看到以下内容:

^{pr2}$

显然,从我检查runtime.is_shutting_down()返回的值到Appengine终止后端之间,30秒的超时时间还没有过去。在

有人知道为什么会发生这种情况吗?是否有解决办法?在

提前谢谢你!在


Tags: and实例reportifis报告seclong
1条回答
网友
1楼 · 发布于 2024-06-28 15:42:04

这里有来自googleio的演示代码http://backends-io.appspot.com/

包含计数器“v3”和“写入”_落后.py演示一种模式:

'/_ah/start'上,通过

runtime.set_shutdown_hook(something_to_save_progress_and_requeue_task)

看起来你的代码是“你现在正在关闭吗,如果没有,去做一些可能需要一段时间的事情”。这个模式应该听“尽快关机,否则你会失去一切”。在

相关问题 更多 >