Jannis95
Posts: 7
Joined: Thu Jul 24, 2014 6:16 pm

Python Script stopps after some time...

Mon Nov 23, 2020 1:46 pm

Hey Guys,

i have the following python script. It checks if my phone is nearby by reading/scanning for my phones bluetooth mac id.
It runs on a raspberry zero. When i start the script with:

Code: Select all

python inoutboard.py
it just works fine for like an hour. After that the script doesn't work anymore. I have to reboot the pi and start the script again.
It should just work forever...

Does somebody have an idear why?

Image

Thanks for your help!

hippy
Posts: 8952
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Python Script stopps after some time...

Mon Nov 23, 2020 2:16 pm

If it is hanging, neither printing "Jannis: in" or "Jannis: out", suspicion would have to fall on 'bluetooth.lookup_name' never returning.

What's your timeout value ? It's best to include full code as text rather than as a screen shot.

If it should be returning but it isn't, it's always hard to say why that would be. I have seen similar issues with 'urllib.urlopen' fetching web pages where that will simply hang forever. I can only guess that the libraries don't see something they are expecting and get jammed-up forever waiting for whatever it is which never happens. When the library code uses poorly written 'try-except' traps it's often not even possible to Ctrl-C out of the library code.

There may be some mileage in refactoring the code to use threading so you can check if a function has returned in a timely manner and, if not, terminate and restart the program or invoke a second function call and hope that works, that the code can keep running.

The problem there is terminating the program may not always work if a thread has hung, and invoking the same function in parallel might not work if the hung code is holding on to resources, and leaving hung code running in the background could eventually consume all available memory.

The proper course would be to dig down into the library code, identify where it is hanging, and find a way to prevent that. That will be easier where the hang is reproducible but will still require some effort.

Another option is to find a different library and hope that doesn't exhibit the same behaviour. Or use a different programming language. You could try using Python 3 and its libraries to see if that performs better.

Jannis95
Posts: 7
Joined: Thu Jul 24, 2014 6:16 pm

Re: Python Script stopps after some time...

Mon Nov 23, 2020 9:06 pm

Thanks for your Answer. Im not the best programmer. I got that script ready to use and just did some small modifications...

The Project Link:

https://www.instructables.com/Raspberry ... -Whos-Hom/

Timeout is 5.

Return to “Python”