Ok, I got some time to work up what I believe is a representative test of OP's scenario, to satisfy my curiosity, and I'll admit my guess about blocking wasn't the problem. I tested this on a Pi 4B running Raspios 12/Bookworm 64-bit, fully up to date with Debian updates immediately before the testing, so Python version 3.11.2-6+deb12u3. I wasn't using a direct Pi-to-Pi Ethernet link, just Pi-to-switch (1000BASE-T), but I believe physical disconnection of the Ethernet cable is a sufficiently close simulation of OP's case. Here's my test script, intended to be a minimalist implementation of OP's described code.
The test address, 192.168.1.123 is on the Pi's local subnet (i.e. no routing involved, just ARP), and is a non-existant host (i.e. ARP will always fail). With Ethernet connected, I get a steady stream of sub-millisecond times printed (perf_counter() returns time in seconds as a float), nothing particularly remarkable in the data, but the first time and every 6th or 7th time was around 0.13ms with the remainder being between 0.057ms and 0.092ms (roughly). The longer duration calls might include sending ARP requests, or something like that, but I don't believe it's particularly significant.
With Ethernet disconnected, to simulate the remote Pi being off, the test script throws an exception:
This leads me to conclude that OP is calling their Send_Data function from within an exception handler, and the exception is being silently caught and ignored. That would perfectly explain the print() never being executed, but the script seemingly continuing to run without a problem. For the sake of completeness, here's a test script which catches the exception, prints the error, then carries on:
Code:
#!/usr/bin/pythonimport socketimport timesock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)data="message,test"while True: start = time.perf_counter() sock.sendto(data.encode(), ('192.168.1.123', 1234)) print(time.perf_counter() - start) time.sleep(1)
With Ethernet disconnected, to simulate the remote Pi being off, the test script throws an exception:
Code:
Traceback (most recent call last): File "/home/murph/./socket-test.py", line 14, in <module> sock.sendto(data.encode(), ('192.168.1.123', 1234))OSError: [Errno 101] Network is unreachable
Code:
#!/usr/bin/pythonimport socketimport timesock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)data="message,test"while True: start = time.perf_counter() try: sock.sendto(data.encode(), ('192.168.1.123', 1234)) except OSError as msg: print(msg) else: print(time.perf_counter() - start) time.sleep(1)
Statistics: Posted by Murph9000 — Thu Aug 29, 2024 3:02 am