Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask debug mode when using sockets

I'm building python Flask application which uses sockets (flask socketio). Basically, client will send some commands to server, which he wants to execute. Server will execute commands and also send sockets to client commands output.

There is handler function which receives request from user. This function will execute commands and is sending a lot of sockets back to client. To grab stdout and stderr threads are also used.

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

If flask app is in debug mode, sockets emitted inside function will reach client before function ends (which is desirable). But if debug is off all sockets are somehow queued and are send after function end. No real-time response from server, just

click execute -> wait a minute -> here's your output

instead of:

click execute -> here's a bit of output -> here's a another line -> ...

I've read Flask docs but description of debug is following:

If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong

Is there any way to tell Flask to send everything right away, or some ideas how to solve this? It may related to flask-socketio plugin for flask

Really much appreciate your feedback :)

like image 262
jan-hybs Avatar asked May 14 '15 11:05

jan-hybs


1 Answers

My guess is that the problem is that you haven't monkey patched the standard library, so all these calls you are making to support the monitoring of the asynchronous process are blocking. In debug mode Flask-SocketIO does the monkey patching because the Flask reloader does not work without it.

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

from gevent import monkey
monkey.patch_all()
like image 121
Miguel Avatar answered Oct 26 '22 23:10

Miguel