0

I'm looking to unit test Python SocketIO's emit function's output (verify the output messages) to resolve the below issue and avoid needing to re-run a frontend process each time.

Is this possible?

I have a Python SocketIO application that passes down the emit function deep into the call stack but is not sending an emission back to the frontend when several functions deep, and I'm trying to narrow where the program is not waiting on the emit to complete.

Ryan Cocuzzo
  • 3,109
  • 7
  • 35
  • 64
  • The emit function is asynchronous. When you call it, it just puts the event in a send queue. The actual transmission of the event to the client happens in a background task. In other words, when the emit function returns, the event might not have been sent yet. – Miguel Grinberg Aug 15 '20 at 16:31
  • Interesting, is there any way to wait on that message being sent? I'm looking for a progress bar effect, but at a certain point these `emit`s pile up and all get sent at once upon completion of the process. @Miguel – Ryan Cocuzzo Aug 16 '20 at 22:48
  • The reason why the emits all sent at the end is because you are blocking, so the background task in charge of sending does not get to run. Add a socketio.sleep(0) in your send loop to give a chance to other tasks to run concurrently. Also, to wait for an event to be sent you can use the callback function, or else use call() instead of emit(). – Miguel Grinberg Aug 16 '20 at 22:55
  • I'd actually tried `socketio.sleep(0) `and it didn't change the result, a non-zero sleep amount actually seemed to produce a less-clustered output, although I'm not sure why. I haven't tried using `call()` but it's docs say it will wait for a response, and I don't send a response back each progress bar update. Will that block all emissions until it hears back from the front-end? And/or wait indefinitely for a confirmation message that won't come? @Miguel – Ryan Cocuzzo Aug 16 '20 at 23:11
  • It's hard for me to comment on code I haven't seen. The sleep should have helped, but maybe something particular to your implementation is causing a different behavior. If you want to use callbacks or the call() method you have to have the other side acknowledge each message with a response. – Miguel Grinberg Aug 17 '20 at 22:13

0 Answers0