Flask debug mode when using sockets

I am creating a python flex app that uses sockets (flocket socketio). Basically, the client will send some commands to the server that it wants to execute. The server will execute commands as well as send sockets on client output commands.

There is a handler function that receives a request from the user. This function executes commands and sends many sockets back to the client. Stdout and stderr are also used to capture streams.

@socket.on ('run-code')
@authenticated_only
def socket_run_code_request (request):
  # run command
  # emit socket for each line of output

      

If the flash application is in debug mode , sockets emitted inside the function will reach the client before the function completes (which is desirable). But if debugging is disabled , all sockets are somehow queued and dispatched after the function completes. No real time response from the server, just

click execute → wait a minute → here is your exit

instead:

click execute -> here's some output -> here's another line -> ...

I've read the Flask docs, but the debug description is as follows :

If you enable debugging support, the server will reboot when the code changes, and it will also provide you with a useful debugger if things go wrong.

Is there a way to tell Flask to send everything at once, or any ideas how to solve this? It may refer to the flask-socketio flask plugin

I really appreciate your feedback :)

+3


source to share


1 answer


My guess is that the problem is that you don't have the monkey patched by the standard library, so all these calls you make to support monitoring the asynchronous process are blocked. In debug mode, Flask-SocketIO executes the monkey patch because the Flask reloader doesn't work without it.

For the monkey patch, just add the following at the very top of your main Python script:



from gevent import monkey
monkey.patch_all()

      

+2


source







All Articles