Python evdev-终止读取循环

Python evdev-终止读取循环,python,terminate,evdev,Python,Terminate,Evdev,我正在树莓Pi上开发一个小应用程序。对于这个问题,我可以把它归结为旋转编码器的使用。我发现dtoverlay的使用对我来说是最方便的方式。因此,我在python3中使用了evdev模块。 我当前的代码如下所示: import time from threading import Thread from evdev import InputDevice, events import evdev rotary_dev = InputDevice('/dev/input/event1') def

我正在树莓Pi上开发一个小应用程序。对于这个问题,我可以把它归结为旋转编码器的使用。我发现dtoverlay的使用对我来说是最方便的方式。因此,我在python3中使用了evdev模块。 我当前的代码如下所示:

import time
from threading import Thread
from evdev import InputDevice, events
import evdev


rotary_dev = InputDevice('/dev/input/event1')

def rotaryAction(dev):
   for ev in dev.read_loop():
       ev = evdev.util.categorize(ev)
       if isinstance(ev, events.RelEvent):
           if ev.event.value == 1:
               print("Value: ",ev.event.value)
           elif ev.event.value == -1:
               print("Value: ",ev.event.value)

t = Thread(target=rotaryAction, args=(rotary_dev,))
t.start()

while True:
   try:
       time.sleep(5)
   except KeyboardInterrupt:
       print("Interrupt")
       #Do something to stop the wait for device events
       break
现在我的问题是,我怎样才能以最好的方式终止这个程序。我无法停止线程,因为for循环等待设备上的事件。 我想以某种方式模仿一个事件,但我不知道该怎么做。 我在网上没有找到任何参考资料,我想知道为什么

我感谢你的帮助