How to use Python main() function in GAE (Google App Engine)?

前端 未结 3 430
谎友^
谎友^ 2021-01-16 09:51

I\'d like to use a main() function in my GAE code
(note: the code below is just a minimal demonstration for a much larger program, hence the need for a

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-16 10:46

    GAE apps are not designed to be standalone apps, a main() function doesn't make a lot of sense for them.

    Basically GAE apps are really just collections of handler code and rules/configurations designed to extend and customize the behaviour of the generic GAE infra/sandbox code so that it behaves your app. You can see that from your backtrace - other code is invoking your handler code (and the stack before reaching your code can be a lot deeper than that).

    In your particular case the app variable must be a global in main.py to match the script: main.app config line in the app.yaml config file. This is what the traceback is about.

    As for organizing the code for huge apps, there are other ways of doing it:

    • splitting the app in multiple modules/services, each with their own app.yaml config file. For example: Can a default service/module in a Google App Engine app be a sibling of a non-default one in terms of folder structure?

    • splitting a service/module into multiple "scripts" - primary entry points into the app.yaml file similar to your main.py file, each with their own app config` - which really are just mappers between routes and handlers. For example: App Engine throws 404 Not Found for any path but root

    • splitting the handlers for one app mapper into multiple files, using webapp2's lazy loaded handler technique. Examples:

      • App Engine: Few big scripts or many small ones?
      • What determines start up time of dynamic instance and can it vary between weeks if code is same

    In an extreme case a main.py file could contain just the app variable - that is really the only requirement.

提交回复
热议问题