I am writing a code in Python to send three POST requests consecutively if certain conditions are met. The POST requests are sent to the FTX Exchange (which is a crypto exchange) and each request is a 'buy' order.
The second order is triggered as soon as the first is filled, and the third as soon as the second is filled. In order to speed up the code (I need the orders to be executed very close to each other in time), I am sending all POST requests to a subprocess (with multiprocessing.Process()) and, instead of waiting for the request response, I wait for an update from a websocket connection to the walletchannel that notifies each new filled order. This websocket connection is opened at the very beginning of the code, in a subprocess.
So, the timeline of the code is the following

Open Websocket connection to the wallet channel
Loop until conditions are met
If True, exit loop and send first order through POST request
Wait until the first order is filled (i.e. update from the websocket)
Send second order through POST request
Wait until the second order is filled (i.e. update from the websocket)
Send third order through POST request
Wait until the third order is filled (i.e. update from the websocket)
Return "Orders submitted and filled"

I have the small problem that in step (4) the update from the websocket takes too much time to arrive (of the order of 1 second), while steps (6) and (8) are pretty fast (of the order of milliseconds).
It looks like the websocket connection is somehow sleeping before the steps (3)-(4) and it takes some time to receive messages but, as soon as the first message is received, all the subsequent messages arrive very fast. I am not a network expert... how can I avoid such delay in receiving the first message from the websocket?
I am pinging the websocket connection every 20 seconds and waiting for a pong within 10 seconds.
I think it is not the problem with your code but the problem with the service that you are using.
For example, if the server is getting a simple request such as pinging, it takes very little time to process the request and respond. However, if it is a complex request such as buying crypto, it may take some time at the server to process your request, and this is what I believe creates some delay with your first request.

I think that happens with the only first request because the server has some caching that saves the recent user.
Delay in receiving first message from a websocket connection