dap
Posts: 2
Joined: Wed May 15, 2019 5:05 pm

Raspberry pi socket connection delay for receiving message.

Wed May 15, 2019 5:21 pm

Hi peeps

Problem
Looking to see if anyone provide any insight or solution to my issue. I have a raspberry pi thats running a python script that connects to a socket channel (well http long polling keep alive to be exact). It is listening for a specific message. When it gets the message, it needs to do something. It needs to do that said something in a reasonable amount of time: say 1-3 seconds. The problem I am having is that it takes up to 6-10 seconds for the raspi to receive the message. This interestingly only happens if the raspi client has not received messages in a while. If it is consistently receiving messages, it'll receive and process the message instantly. Any insights?

I've been lurking for some time and reading on both real time linux and nice. Will soon be experimenting with both and could post results here. Anyone run into a similar issue?


Experiments
I've ran a few experiments to narrow down the issue.
Its not the raspi network because I have ran on different networks, with different sims, and tried both lte and wifi with the same results
Its not the python socket client code because I ran it the client code on both mac and ubuntu and the messages will be received and processed immediately.
Its not the 'do said something' post message action because running that on its own happens instantly.

Any help would be appreciated and regardless will report my findings

knute
Posts: 424
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Raspberry pi socket connection delay for receiving message.

Wed May 15, 2019 9:54 pm

I'm not sure what is causing your problem but you could send some dummy messages every so often and maybe that would keep things tuning along nicely when you need it?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6895
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Raspberry pi socket connection delay for receiving message.

Thu May 16, 2019 9:02 pm

TCP is a stream connection. There are no guarantees over the exact timing of messages, nor on how they may be split up. There is also the Nagle algorithm designed to combine writes efficiently into the stream.
If you wish to ensure that a write is sent immediately then you need to flush the connection.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

PiGraham
Posts: 3553
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Raspberry pi socket connection delay for receiving message.

Thu May 16, 2019 9:27 pm

I second the Nagle comment by 6by9. Read up on TCP_NODELAY.
If you want quick responses and your data is n smallish chunks you could use UDP instead of TCP streams.

dap
Posts: 2
Joined: Wed May 15, 2019 5:05 pm

Re: Raspberry pi socket connection delay for receiving message.

Tue Jun 04, 2019 8:54 pm

Wow! didn't know there was any responses to this! So @knute I am currently sending a message to the device every 5 seconds. This "keeps the device alive and has no delay." This is not the ideal solution. @6by9 and @pigraham, I'll have a look at udp but i don't believe tcp is the issue as we can get it to work without a delay under certain circumstances still using tcp. ie intermittently on wifi and with a script sending messages to the device every 5 seconds as mentioned earlier

Return to “Advanced users”