question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

emit() in loop not sending until loop completion when using eventlet

See original GitHub issue

I am working on a simple Flask-SocketIO app that streams data data from a server to the users browser in real time. I am stuck though when trying to use emit() in a loop. The behavior I am getting is that emit() does not actually send any data to the client until the entire loop is finished executing.

Here is some example code that shows the same behavior:

@socketio.on('rcv_args_and_run')
def rcv_args_and_run(args):
    numy = 1
    while numy < 10:
        socketio.emit('results','Message from server #' + str(numy))
        print('Message from server #' + str(numy))
        time.sleep(5)
        numy += 1
    disconnect()

I am developing this locally using the flask/flask-socketio dev server and when I do not have the eventlet package installed it performs as expected, I believe because it then uses long polling. When I install eventlet though I start to get this behavior.

I have looked around at similar issues but can’t exactly tell if the behavior I am getting would be solved by the steps you recommended they take.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
miguelgrinbergcommented, Jan 18, 2017

Try switching time.sleep(5) to socketio.sleep(5). The sleep function from the Python library is not compatible with eventlet, you have to either monkey patch it, or use eventlet’s own sleep function. The socketio.sleep() function routes the sleep call to the proper place, depending on what asynchronous framework you are using.

1reaction
TimothyBramlettcommented, Jan 19, 2017

Hum…it seems like the monkey patching might have worked! I used this document and it does seem to explain things very well: http://eventlet.net/doc/patching.html.

I guess the idea solution would have been to wrote my code from the perspective of using Eventlet from the beginning and to only use eventlet/flask socket io approved io methods.

I need to learn more about Evenlet and asynchronous programming with Python in the future!

I am still confused as to good debugging techniques to identify what line of code is actually causing the blocking in the future.

Regardless, thanks so much for you quick responses! Do you have a donation link or something to help support Flask-socketio?

Read more comments on GitHub >

github_iconTop Results From Across the Web

client doesnt receive emitted value when server is stuck in ...
The problem is that your while loop blocks the whole server, as you are running under eventlet. ... while not code: socketio.sleep(0).
Read more >
Ansible - 'until' loop | - TTL255 - Przemek Rogala's blog |
This loop is used for retrying task until certain condition is met. To use this loop in task you essentially need to add...
Read more >
The Socket.IO Server — python-socketio documentation
Eventlet provides a monkey_patch() function that replaces all the blocking functions in the standard library with equivalent asynchronous versions. While python ...
Read more >
Do...Loop - Definite, Indefinite Loops - FORTH, Inc.
a loop structure in which the words contained within the loop continue to repeat until some truth condition changes state (true-to-false or false-to-true)....
Read more >
Asynchronous updates to a webpage with Flask and Socket.io
We'll use Python Flask and the Flask-SocketIO plug-in to achieve this in this ... loop of magical random numbers print("Making random numbers") while...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found