Search code examples
pythongoogle-app-enginegoogle-cloud-platformklein-mvc

Google App Engine Application - 502 bad gateway error with klein micro web framework


I developed an python webcrawler application based on scrapy and packaged it as a klein application (klein framework)

When I test it locally it everything works as expected, however when I deploy it to google app engine I get a "502 bad gateway". I found other mentions of the 502 error but nothing in relation to the klein framework I am using. So I was just wondering if app engine is maybe incompatible with it.

This is my folder structure

    app
    --app.yaml
    --main.py
    --requirements.txt

The contents of app.yaml

    runtime: python37
    
    instance_class: F2
    
    handlers:
    - url: /.*
      secure: always
      redirect_http_response_code: 301
      script: auto

The contents of main.py


    from klein import route, run
    
    @route("/")
    def landing_page(request):
        return "HELLO"
    
    if __name__== "__main__":
        run(host='127.0.0.1', port=8080)


Solution

  • App Engine requires your main.py file to declare an app variable which corresponds to a WSGI Application.

    Since Klein is an asynchronous web framework, it is not compatible with WSGI (which is synchronous).

    Your best option would be to use a service like Cloud Run, which would allow you to define your own runtime and use an asynchronous HTTP server compatible with Klein.