How can I tell if my multithreaded Python program is crashing on my Pi 3 because there are too many threads? (Or poorly written ones?)

My multithreaded program involves three looping threads: a main loop, a talking UDP socket and listening UDP socket. The code runs fine on Ubuntu in a virtual box but crashes in a non-reproducible manner on the Raspberry Pi running a RT Raspbian kernel (from Emlid's blog). However, I haven't actually leveraged the RT scheduler at all (nor am I sure it is utilizable anymore since we're using Python and not C).

I plan to use Htop while running my code to view threads and CPU usage but I am unsure of what I should be looking for.

Would too many Python threads on a Pi 3 crash the program? How could I tell? Is it the GIL? How could this be overcome? Should I use another OS besides Raspbian? Ubuntu Snappy or Ubuntu Mate?

Please help. Thanks in advance.

  • 1
    No six threads will not crash a Pi. I doubt htop will have any visibility of Python threads, I'm not even sure if they are threads. I'd run it on ordinary Raspbian to rule out your "RT" kernel being the problem. – joan Nov 14 '16 at 22:53
  • The answer to the non-Pi related part of this question -- essentially, "Can I crash a program by using too many threads?" -- is not really, although if you started a fork bomb-esque nest of them you could bog things down. The fact that your code runs okay in VBox on Ubuntu does not mean your code is okay, threading often involves a degree of indeterminacy such that flaws and errors can be difficult to reproduce, but that doesn't mean they aren't there and won't pop up -- indeterminately. But these are not Pi related problems. – goldilocks Nov 14 '16 at 22:59
  • WRT the issue that may be pi related (the RT kernel) I guess a way to check this would be to try a stock kernel. Although that still won't prove much one way or the other. – goldilocks Nov 14 '16 at 22:59
  • Thank you both. I did try the code on a standard Raspbian image and that seemed to stop the crashing. However, my inability to explain why this works bothers me. Is there a way to confirm this or debug further despite the indeterminate behavior of threads? Any reading on GIL or how GIL/Python works on a Pi? – mago3421 Nov 14 '16 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.