I have a python script that I'm running on a remote device. There will be two different threads created. The first thread is created to monitor for USB connections to the device.
class USBDetector(threading.Thread):
''' Monitor udev for detection of usb '''
def run(self):
''' Runs the actual loop to detect the events '''
self.context = pyudev.Context()
self.monitor = pyudev.Monitor.from_netlink(self.context)
self.monitor.filter_by(subsystem='usb')
self.monitor.start()
for device in iter(self.monitor.poll, None):
if device.action == 'add':
# some action to run on insertion of usb
I've tried to insert a break statement if a global variable state changes. But it didn't work. something simple like
if TERMINATE == True:
break
I looked at https://pyudev.readthedocs.io/en/latest/api/pyudev.html and through the reading it looks like this section of code
for device in iter(self.monitor.poll, None):
if device.action == 'add':
# some function to run on insertion of usb
is an endless loop, unless a timeout is inserted instead of None. I want to kill the thread when the other thread ends. If I give the quit command for my main thread, this usbdetector just keeps running. Any suggestions on how to stop it?
(UPDATE)
Hey,
sorry I went with a low tech way of solving my problem for now.
If anyone knows how to break out of this for loop without the need of a second loop, let me know
def run(self):
''' Runs the actual loop to detect the events '''
global terminate
self.rmmod_Module()
self.context = pyudev.Context()
self.monitor = pyudev.Monitor.from_netlink(self.context)
self.monitor.filter_by(subsystem='usb')
self.monitor.start()
count = 0
while not terminate:
count = count + 1
print count
for device in iter(partial(self.monitor.poll, 3), None):
if device.action == 'add':
# some function to run on insertion of usb
obviously I have the for loop nested in a while loop waiting for terminate to be true. Its simple and works, however would still like to know if there is a way to kick out of the for device in iter() loop.