Python Forum
smbus contention handling - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: smbus contention handling (/thread-5182.html)



smbus contention handling - PatM - Sep-21-2017

I have several threads needing to use the Raspberry Pi3 single I2C bus. The threads aren't called all that frequently, the fastest being one per second and the slowest once every 10 minutes. Since the Pi3 has four processors I believe that there will be times when two (or more) threads will try to access I2C at the same time and mess things up.

What I've done is created a global variable: i2c_busy and in each called thread I do the following before actually attempting to use the bus.

while i2c_busy
    time.sleep(.1)

i2c_busy = True
some_value = some_i2c_call()
i2c_busy = False
First question: Is there actually going to be a bus contention problem? I think yes but I could be wrong - not an expert.
Second question: Does this seem like a reasonable way of dealing with said contention?

Thanks!


RE: smbus contention handling - PatM - Sep-21-2017

Well it looks like doing it this way isn't a good idea. Hadn't thought of it but "i2c_busy" has the correct value when the thread is created but it doesn't update while the thread is running (makes sense now that I think of it). TOP shows threads piling up whenever it actually hits the While statement and they don't go away until I kill the program.

Guess I'll redo this a bit and put the check in the main loop just before creating the thread.