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 :)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With