Revert "change pipeListener to unblocking poll loop"
This reverts commit 6ab3ecbf34
.
This commit is contained in:
parent
825570cf20
commit
130f7f73f2
40
listeners.py
40
listeners.py
|
@ -1,5 +1,4 @@
|
||||||
import logging, os, sys, stat, time
|
import logging, os, sys, stat
|
||||||
from threading import Event
|
|
||||||
from exceptionThreading import ExceptionThread
|
from exceptionThreading import ExceptionThread
|
||||||
from evdev import InputDevice, ecodes
|
from evdev import InputDevice, ecodes
|
||||||
from select import select
|
from select import select
|
||||||
|
@ -114,36 +113,14 @@ class KeypadListener(ExceptionThread):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# TODO: these are not threadsafe
|
||||||
|
# TODO: this code gets really confused if the pipe is deleted
|
||||||
class PipeListener(ExceptionThread):
|
class PipeListener(ExceptionThread):
|
||||||
def __init__(self, callback, path):
|
def __init__(self, callback, path):
|
||||||
self._path = path
|
self._path = path
|
||||||
self._stopper = Event()
|
|
||||||
self._makeFIFO()
|
|
||||||
|
|
||||||
def listen():
|
|
||||||
while not self._stopper.isSet():
|
|
||||||
try:
|
|
||||||
fd = os.open(self._path, os.O_RDONLY | os.O_NONBLOCK)
|
|
||||||
msg = os.read(fd, 1024).decode().rstrip()
|
|
||||||
if msg != '':
|
|
||||||
callback(msg, logger)
|
|
||||||
os.close(fd)
|
|
||||||
except BlockingIOError:
|
|
||||||
pass
|
|
||||||
except FileNotFoundError:
|
|
||||||
# TODO: this might be easier with a watchdog
|
|
||||||
self._makeFIFO()
|
|
||||||
finally:
|
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
super().__init__(target=listen, daemon=False)
|
|
||||||
self.start()
|
|
||||||
logger.debug('Started pipe listener at path %s', self._path)
|
|
||||||
|
|
||||||
def _makeFIFO(self):
|
|
||||||
if os.path.exists(self._path):
|
if os.path.exists(self._path):
|
||||||
if not stat.S_ISFIFO(os.stat(self._path)[0]):
|
if not stat.S_ISFIFO(os.stat(self._path)[0]):
|
||||||
logger.warn('%s exists but is not a pipe. Deleting', self._path)
|
|
||||||
os.remove(self._path)
|
os.remove(self._path)
|
||||||
os.mkfifo(self._path)
|
os.mkfifo(self._path)
|
||||||
else:
|
else:
|
||||||
|
@ -151,8 +128,17 @@ class PipeListener(ExceptionThread):
|
||||||
|
|
||||||
os.chmod(self._path, 0o0777)
|
os.chmod(self._path, 0o0777)
|
||||||
|
|
||||||
|
def listenForSecret():
|
||||||
|
while 1:
|
||||||
|
with open(self._path, 'r') as f:
|
||||||
|
msg = f.readline()[:-1]
|
||||||
|
callback(msg, logger)
|
||||||
|
|
||||||
|
super().__init__(target=listenForSecret, daemon=True)
|
||||||
|
self.start()
|
||||||
|
logger.debug('Started pipe listener at path %s', self._path)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self._stopper.set()
|
|
||||||
if os.path.exists(self._path):
|
if os.path.exists(self._path):
|
||||||
os.remove(self._path)
|
os.remove(self._path)
|
||||||
logger.debug('Cleaned up pipe listener at path %s', self._path)
|
logger.debug('Cleaned up pipe listener at path %s', self._path)
|
||||||
|
|
Loading…
Reference in New Issue