rm-hull / luma.led_matrix

Python module to drive LED Matrices & 7-segment displays (MAX7219) and RGB NeoPixels (WS2812 / APA102)
https://luma-led-matrix.readthedocs.io
MIT License
520 stars 157 forks source link

Is this library thread safe or thread compatible? #253

Closed dachshund-digital closed 3 years ago

dachshund-digital commented 3 years ago

Is this library thread safe or thread compatible?

rm-hull commented 3 years ago

When interfacing with i2c or spi it is up to you to manage access to those resources. The core api and driver code aren’t really stateful so should be these safe. However, the diff-to-previous framebuffer is, so you would need to be careful there.

what application are you considering (or issue you are experiencing) that makes you ask this question?

dachshund-digital commented 3 years ago

I noticed that the luma library fails to drive a display if the library is not initialized in the same thread context. This was actually a code design issue in my code, initially, I was initializing the library in one context, but trying to display text via a different context, a sub thread. This failed. Even using proper shared memory or variables, no output on display.

The fix was simple, just making sure I initialize the library in the thread call back and use a queue to send display text strings to the thread call back, that goes into a controlled loop, that dequeues each message from the queue, thus, everything works. So your suggestion appears true, as long as I have initialization done in the same thread that writes to the display, works. I happen to be using SPI based, but i2c should be similar.