【问题标题】:Start and Stop Handlers in python3.7python3.7中的启动和停止处理程序
【发布时间】:2020-10-13 17:04:57
【问题描述】:

一个用python2.7编写的app engine应用在app.yaml中有如下结构

handlers:
- url: /_ah/push-handlers/.*
  script: main.app
  #login: admin
  secure: always

- url: /_ah/start
  script: start.app

- url: /_ah/stop
  script: stop.app

start.py 和 stop.py 分别由 /-ah/start 和 /_ah/stop 处理,它们 包含启动和停止 appengine 应用程序以处理 pubsub 消息时的相应方法。对于 ex start.py 包含此代码

app = webapp2.WSGIApplication([
    ('/_ah/start', ReceiveStart)
], debug=True)

这个类包含默认的get方法来处理

class ReceiveStart(webapp2.RequestHandler):
    

    def get(self):
        last_end_time_str = ""
        try:
            # get the App Engine default bucket name to store a GCS file with last end_time
            project_id = app_identity.get_application_id()

现在如果将此应用程序转换为 python3.7(尚未测试) app.yaml 变为

处理程序:

- url: /_ah/push-handlers/.*
  script: auto
  #login: admin
  secure: always

- url: /_ah/start
  script: auto

- url: /_ah/stop
  script: auto
  
- url: /.*
  secure: always
  redirect_http_response_code: 301
  script: auto

当消息被发送和接收到这个应用程序引擎时,这种重定向是否会像在 python2.7 中那样发生,对于 start.app 和 stop.app 的 url 是否正确,因为脚本设置为 auto 而不是 start.app和 stop.app 这会起作用吗?

python 和 app-engine 的新手,感谢帮助,在此先感谢。

【问题讨论】:

    标签: python-2.7 google-app-engine google-cloud-platform python-3.7


    【解决方案1】:

    使用第 2 代 AppEngine,网络服务器与平台更加无关,您可以使用所需的库运行所需的框架。

    在 Python 中,Flask 常用于 API。 Django 更多的 web 应用程序。

    无论如何,现在,您的处理程序不会将请求路由到特定脚本,而是将请求路由到必须处理请求的网络服务器。

    /_ah/start/__ah/stop 的用法相同,但您必须在您的网络服务器中定义这些路由。这是烧瓶中的一个例子

    from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.route('/_ah/start')
    def start():
        print("start called")
        return "",200
    
    @app.route('/_ah/stop')
    def stop():
        print("stop called")
        return "",200
    
    

    【讨论】:

    • 谢谢,这些方法 @app.route('/_ah/start') 是否应该在 main.py 文件中定义,当你的意思是 webserver 时,start.py 和 stop.py 不是必需的?
    • 我已经用一个类和一个方法定义了这样的方法。 class class ReceiveStart(webapp2.RequestHandler): def get(self):, 应该用不需要类的东西代替吗? @app.route('/_ah/start') def get(self)
    • 我从来没有在 Flask 中使用过类。我想你可以,但你应该使用blueprint
    猜你喜欢
    • 2017-07-27
    • 1970-01-01
    • 2017-06-08
    • 2011-10-26
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    相关资源
    最近更新 更多