adafruit / Adafruit_CircuitPython_TCA9548A

CircuitPython driver for the TCA9548A I2C Multiplexer.
MIT License
26 stars 15 forks source link

Wait for other thread to unlock when not able to lock #39

Closed GwnDaan closed 2 years ago

GwnDaan commented 2 years ago

This request adds a sleep call of 0 seconds in the try_lock function. This resolves an issue where the current thread is constantly trying to lock the i2c bus, without giving another thread the 'opportunity' to complete it's operation and unlock the i2c bus.

This sleep call drastically improves performance on my pi zero board: image ^ without the sleep call

image ^ using the sleep call

I'm not sure if this is the right way of 'unblocking' the i2c bus for other threads, but it's for sure 1000x better than before for me. Therefore I feel like it's worth sharing with others.

GwnDaan commented 2 years ago

Note that this issue only occurs when using multiple threads with all of them constantly (without sleeping) trying to read values from different i2c busses over the multiplexer.

tekktrik commented 2 years ago

Hm, @makermelissa and @tannewt this is interesting. This would imply that adafruit_bus_device should change as well.

tannewt commented 2 years ago

Ya, interesting. This is lightweight enough that I'd be ok with the change here and in bus device. This doesn't mean our drivers are threadsafe though. They are tested on micros without threads generally.