0

I have a normal vanilla socket that can send and receive data. The issue arises when there is a high latency and it takes a while to get the data. For example I'll have:

s.recv(1024)

But by the time it executes, only 700 bytes will be available. So I have to do it again to get the rest. But here's the kicker: sometimes it will be faster and the second s.recv(1024) will be waiting for data when none is coming. In that case it gets stuck in a long, long loop that I can't even interrupt using Ctrl+C. The question is, how can I prevent this loop? Is there a way to raise an Exception if no data is available?

S. L.
  • 630
  • 8
  • 19
  • 1
    Possible duplicate of [How to set timeout on python's socket recv method?](https://stackoverflow.com/questions/2719017/how-to-set-timeout-on-pythons-socket-recv-method) – C.Nivs Sep 16 '19 at 14:22
  • Would it help to do the second `recv()` call only if there is not enough data? You could check if the first data chunk is complete, and only if it isn't, do the 2nd try (and maybe a 3rd, 4th etc.) – glglgl Sep 16 '19 at 14:30
  • It doesn't sound like you have a loop, you are trying to prevent your socket from blocking for more data. See this: https://stackoverflow.com/questions/16745409/what-does-pythons-socket-recv-return-for-non-blocking-sockets-if-no-data-is-r – lossleader Sep 16 '19 at 15:16

0 Answers0