How to prevent concurrent access to a resource such as a USB device?
Not exactly an answer to your question, but maybe another idea about how to tackle this with out using globals.
Why don't you write a small program for controlling your USB device. This script runs once (one instance) on your server and takes care of communicating with the device in the manner you need. It also takes care of concurrency.
Now communicate from your web application via pipes, sockets, whatever with this script, send commands to it and receive results from it.
I would like to be able to use a USB device from several applications (for instance I run a Flask web application), making sure only one uses it at a time.
In my case I am using a relay to open / close a door. The door takes about 20 seconds to open. During that time the relay should not be activated, because this would lock the door in the middle.
Thanks in advance!