Marek Ant
Marek Ant

Reputation: 67

Python - infinite loops in threads and IO access

I have a device that runs on a NanoPi board with Armbian. My app runs a main loop, and a separate thread using the Python's built in threading lib. Both of them need access to GPIO - one polls several inputs, the other controls some outputs. I ran into a problem - when the thread is running my Python interpreter returns an error: No access to /dev/mem. Try running as root! Seems like one thread can access the /dev/mem (virtual?) file at a time. How should I solve that? I need constant access to GPIO, since I don't want to miss any incoming impulses.

def impulse_counter():
    global counter

    while 1:

        if GPIO.event_detected(INPUT_1):
            print("Current cnt state: %d" % (counter))
            counter += 1

This is in a separate module:

    input_counter = threading.Thread(target=mbox_io.impulse_counter)

    if __name__ == "__main__":
    
        input_counter.start()
    
        while 1:
            <Some long code is running here, irrelevant>
            mbox_io.red_off()   

Upvotes: 0

Views: 139

Answers (2)

alagner
alagner

Reputation: 4062

Just a guess, but isn't /dev/mem read-only for non-root user? If so, doesn't the poller thread run fine, while the one switching the IOs returns that error? What does ls -l /dev/mem show? And does running as root actually help, because you have not stated that.

Upvotes: 0

Hannu
Hannu

Reputation: 12205

I'll post this as an answer as it is too long for comments.

I am not familiar with that particular system and problem but there are many occasions where something cannot be accessed safely from many threads. This usually means you have to redesign your application.

One possible way of solving these is to add another thread that handles all communication with the shared resource. You could have for example a queue where other threads give "tasks" to the GPIO thread that then executes those tasks and returns results if results are expected. This would restrict the shared resource to one thread while you could still do a lot of data processing in other threads.

This may not work with your application for various reasons.

Upvotes: 1

Related Questions