python - 页面并不总是呈现

标签 python google-app-engine

在 Google App Engine 中,我有以下代码,显示了一个简单的 HTML 页面。

import os

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp

class IndexHandler(webapp.RequestHandler):
  def get(self):
    template_values = { }

    path = os.path.join(os.path.dirname(__file__), '../templates/index.html')
    self.response.out.write(template.render(path, template_values))

问题是页面并不总是呈现。 index.html 是一个简单的“Hello World!”。刷新几次页面后,页面将正确显示(即找到index.html文件......)。我试图在最后调用flush,但没有帮助。我可以使用 SDK 并在他们的服务器上重现这一点。

我错过了什么吗?有人知道发生了什么吗?

谢谢

最佳答案

您的处理程序脚本(app.yaml 引用的脚本)有一个 main() 函数,但最后需要这一节:

if __name__ == '__main__':
  main()

发生的情况是,您的脚本第一次在给定的解释器中运行时,它会解释您的主脚本,但它不执行任何操作(因此返回空白响应)。在后续调用中,解释器只需执行 main() (已记录的优化),这会按预期生成页面。添加上面的节也会导致它在初始导入时执行 main。

关于python - 页面并不总是呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/985017/

相关文章:

python - 合并具有不同 left_on 和 right_on 长度列表的两个数据帧

Python - 使用ffprobe获取视频的持续时间

python - 是否有一个 groupby 函数可以创建一个从名称到下划线的新数据框?

java - Spring Boot - 无法确定数据库类型 NONE 的嵌入式数据库驱动程序类

python - 将 jpg、gif、png 等存储到 gae-datastore

python - python 获取循环数字的总和

python - PyCharm:为什么只显示退出代码行?

node.js - 在 AppEngine Node.js 标准环境中稳定快速 session

java - GAE 阿拉伯语支持

node.js - Google App Engine 上的 NestJS 项目显示错误 "Could not write file"&& "EROFS: read-only file system"