-
-
Notifications
You must be signed in to change notification settings - Fork 575
Description
Hi !
While working with my ws server I encountered something weird, ServerConnection.send() seems to have a delay under some circonstances. The client and the server are on the same machine.
I'm sorry I don't have code to reproduce the issue, I'm not sure to be able to reproduce it consistently.
Here is the setup
- The client sends a message
- The server iterates over some list (here length == 3)
- The server computes something (can take several seconds)
- The server sends back a text message for each element
- The server send a final closing message
The problem, is that the first message is received immediatly by the client, but the last 2 messages are received at the same time (while they were sent at a few seconds interval).
It looks like the packet is waiting for some buffering or it does not have time to execute.
At first, I didn't know if it was due to my client or the server, but I managed to "fix" the issue, so I'm pretty sure that this is due to the server code.
I found two ways to solve it, either :
await ws.send(data)or waiting for a pong response (with a 0.0 latency) :
pong_waiter = await ws.ping()
latency = await pong_waiterFor sleep, looking at the asyncio doc, it says that might help for long-running tasks, so that might be it
Setting the delay to 0 provides an optimized path to allow other tasks to run. This can be used by long-running functions to avoid blocking the event loop for the full duration of the function call.
Is this a normal behaviour, or am I using the library wrong ?