Search code examples
pythonwebsocketapp-engine-flexible

App Engine Flexible can't use datastore with websockets example


I tried adding the websockets example project to the datastore project and the websockets work but when a page queries the datastore or tries to put a new entity I get a 502 response. In the logs it shows a critical error on the service worker. If I remove the websocket code the datastore code works as intended. The only difference I can see is the entrypoints for the app samples slightly differ

the websocket sample uses

entrypoint: gunicorn -b :$PORT -k flask_sockets.worker main:app

while the datastore sample uses

entrypoint: gunicorn -b :$PORT main:app

websocket sample https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/flexible/websockets

datastore sample https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/flexible/datastore


Solution

  • As you said it seems there is a problem with the flask_socket.worker, I have test it and it does not work with the datastore client.

    I have tried with the Flask-SocketIO framework using the eventlet worker and the datastore queries work fine.

    entrypoint: gunicorn -b :$PORT --worker-class eventlet -w 1 main:app

    Also you need to add the eventlet module in the requirements.txt file eventlet==0.24.1

    The downside of this is that it breaks the compatibility with the websocket code so you need to rewrite this part. Keep in mind that code samples are just intended to show in a few lines how to use the Google Cloud products and copy-paste them without modifying the configuration undelied in the app.yaml is not a good idea.