Reputation: 3952
I am using python with Raspian on the Raspberry pi. I have a peripheral attached that causes my interrupt handler function to run. Sometimes the interrupt get fired when the response to the first interrupt has not yet completed. So I added a variable that is set when the interrupt function is entered and reset when exited, and if upon entering the function, it finds that the lock is set it will immediately exit.
Is there a more standard way of dealing this kind of thing.
def IrqHandler(self, channel):
if self.lockout: return
self.lockout = True;
# do stuff
self.lockout = False;
Upvotes: 0
Views: 574
Reputation: 304255
You have a race condition if the IrqHandler
is called twice sufficiently close together, both calls can see self.lockout
as False
and both proceed to set it to True
etc.
The threading
module has a Lock()
object. Usually (the default) this is used to block a thread until the lock is released. This means that all the interrupts would be queued up and have a turn running the Handler.
You can also create a Lock(False)
which will just return False
if the Lock has been acquired. This is close to your use here
from threading import Lock
def __init__(self):
self.irq_lock = Lock(False)
def IrqHandler(self, channel):
if not self.irq_lock.acquire():
return
# do stuff
self.irq_local.release()
Upvotes: 2
Reputation: 7806
You can tie that in with a borg pattern. This way you can have several interrupt instances paying attention to one state. There is another one called singleton but here is a discussion on the two. Why is the Borg pattern better than the Singleton pattern in Python
Upvotes: 0