Hello
I got a Micropython card that awake each minute, connect to Wifi, open a socket and push a caracter string.
on the other side a raspberry with a python script that open a socket and try to read.
It works between somes minutes to some hour but stuck always…
If I stop and start the raspberry script it works again…
I try to run on raspberry side the script :
on a loop “while True”
with a crontab that execute the script without while loop
=> same behaviour
It llok like the socket become “full” after a certain time?
Or stuck during sending/receiving process and can’t manage a new one?
When in this state the process seem’s to still open in the port
You’re only accepting a single connection at a time, and waiting for the other end to talk to you. That means, if anything connects and holds it open, your server will stall out. You may want to consider supporting multiple connections at once, to avoid this.
Close. It means that you can have a backlog of two connections - there can be up to two connections waiting for you to accept them. Any more than that, and the system will start telling new clients that you aren’t available.
You can reject any connection from any other IP, but you’ll still need to process the sockets. But even if you lock it to a single IP, you could get stuck in this way, since it’s entirely possible for the other end to stall out.
I don’t know why you’d be doing from _thread import * rather than using the higher-level threading module, but broadly speaking this is one option. However, be aware that the code you have here - which, I am guessing, was written by ChatGPT, as it has multiple rather bizarre constructs in it - is designed for a quite different protocol from your original.
You may want to go back to your original version, and JUST add threading to it. Or alternatively, use asyncio instead, which scales quite efficiently. Either way will work.
Thanks, I found this code on a tuto on a web site but maybe indeed he came from ChatGPT
I like this structure of the code because a would like to avoid non allowed IP to connect and I add a test in the accept_connections() depending on the adress[0] value.
Not sure to understand your comment on from _thread import * ?
you mean it is beter to do like this : from _thread import start_new_thread
as I only use this function?
Ah. I would posit that it might be rather ancient, but it ALSO uses several more modern features, like f-strings, so whatever it is, it’s a bit of a mess and I wouldn’t recommend using it
Python has a very low level module called _thread, but for the most part, it is WAY better to use the higher level module threading. But yes, if you’re going to use _thread, I would very STRONGLY advise against using a star import. Just import the module itself (import _thread) or import specific names (from _thread import start_new_thread), rather than leave open the possibility of literally any unrecognized name coming from that module. Mainly, though, go with threading instead.
Or go with asyncio since your code isn’t going to run into the problems that demand threading. Either will work.