improve multiprocessing, wait for complete and handle remtoe moregraceful

This commit is contained in:
Chrys 2019-09-05 23:01:35 +02:00
parent 59c4a2beb4
commit 05165d2868
12 changed files with 94 additions and 78 deletions

View File

@ -195,7 +195,7 @@ driver=unixDriver
# tcp port # tcp port
port=22447 port=22447
# socket filepath # socket filepath
socketpath=/tmp/ socketFile=
# allow settings to overwrite # allow settings to overwrite
enableSettingsRemote=True enableSettingsRemote=True
# allow commands to be executed # allow commands to be executed

View File

@ -205,7 +205,7 @@ driver=unixDriver
# tcp port # tcp port
port=22447 port=22447
# socket filepath # socket filepath
socketpath=/tmp/ socketFile=
# allow settings to overwrite # allow settings to overwrite
enableSettingsRemote=True enableSettingsRemote=True
# allow commands to be executed # allow commands to be executed

View File

@ -205,7 +205,7 @@ driver=unixDriver
# tcp port # tcp port
port=22447 port=22447
# socket filepath # socket filepath
socketpath=/tmp/ socketFile=
# allow settings to overwrite # allow settings to overwrite
enableSettingsRemote=True enableSettingsRemote=True
# allow commands to be executed # allow commands to be executed

View File

@ -206,7 +206,7 @@ driver=unixDriver
# tcp port # tcp port
port=22447 port=22447
# socket filepath # socket filepath
socketpath=/tmp/ socketFile=
# allow settings to overwrite # allow settings to overwrite
enableSettingsRemote=True enableSettingsRemote=True
# allow commands to be executed # allow commands to be executed

View File

@ -206,7 +206,7 @@ driver=unixDriver
# tcp port # tcp port
port=22447 port=22447
# socket filepath # socket filepath
socketpath=/tmp/ socketFile=
# allow settings to overwrite # allow settings to overwrite
enableSettingsRemote=True enableSettingsRemote=True
# allow commands to be executed # allow commands to be executed

View File

@ -151,7 +151,7 @@ driver=unixDriver
# tcp port # tcp port
port=22447 port=22447
# socket filepath # socket filepath
socketpath=/tmp/ socketFile=
# allow settings to overwrite # allow settings to overwrite
enableSettingsRemote=True enableSettingsRemote=True
# allow commands to be executed # allow commands to be executed

View File

@ -205,7 +205,7 @@ driver=unixDriver
# tcp port # tcp port
port=22447 port=22447
# socket filepath # socket filepath
socketpath=/tmp/ socketFile=
# allow settings to overwrite # allow settings to overwrite
enableSettingsRemote=True enableSettingsRemote=True
# allow commands to be executed # allow commands to be executed

View File

@ -23,15 +23,15 @@ class processManager():
def terminateAllProcesses(self): def terminateAllProcesses(self):
for proc in self._Processes: for proc in self._Processes:
try: #try:
proc.terminate() # proc.terminate()
except KeyboardInterrupt: #except KeyboardInterrupt:
pass # pass
except: #except:
pass # pass
proc.join() proc.join()
for t in self._Threads: for t in self._Threads:
t.join() t.join()
def heartBeatTimer(self, active): def heartBeatTimer(self, active):
try: try:
time.sleep(0.5) time.sleep(0.5)

View File

@ -84,7 +84,7 @@ settingsData = {
'enabled': True, 'enabled': True,
'driver': 'unixDriver', 'driver': 'unixDriver',
'port': 22447, 'port': 22447,
'socketpath':'/tmp/', 'socketFile':'',
'enableSettingsRemote': True, 'enableSettingsRemote': True,
'enableCommandRemote': True, 'enableCommandRemote': True,
}, },

View File

@ -18,30 +18,36 @@ class driver(remoteDriver):
self.env['runtime']['processManager'].addCustomEventThread(self.watchDog, multiprocess=True) self.env['runtime']['processManager'].addCustomEventThread(self.watchDog, multiprocess=True)
def watchDog(self, active, eventQueue): def watchDog(self, active, eventQueue):
# echo "command say this is a test" | nc localhost 22447 # echo "command say this is a test" | nc localhost 22447
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.fenrirSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.fenrirSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.host = '127.0.0.1' self.host = '127.0.0.1'
self.port = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'port') self.port = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'port')
self.sock.bind((self.host, self.port)) self.fenrirSock.bind((self.host, self.port))
self.sock.listen(1) self.fenrirSock.listen(1)
while active.value == 1: while active.value == 1:
client_sock, client_addr = self.sock.accept() try:
if client_sock: r, _, _ = select.select([self.fenrirSock], [], [], 0.8)
# Check if the client is still connected and if data is available: except select.error:
try: break
r, w, e = select.select([client_sock,], [], []) if r == []:
except select.error: continue
return if self.fenrirSock in r:
if len(r) > 0: client_sock, client_addr = self.fenrirSock.accept()
rawdata = client_sock.recv(8129) try:
try: rawdata = client_sock.recv(8129)
data = rawdata.decode("utf-8").rstrip().lstrip() except:
eventQueue.put({"Type":fenrirEventType.RemoteIncomming, pass
"Data": data try:
}) data = rawdata.decode("utf-8").rstrip().lstrip()
except: eventQueue.put({"Type":fenrirEventType.RemoteIncomming,
pass "Data": data
})
except:
pass
try:
client_sock.close() client_sock.close()
if self.sock: except:
self.sock.close() pass
self.sock = None if self.fenrirSock:
self.fenrirSock.close()
self.fenrirSock = None

View File

@ -19,39 +19,49 @@ class driver(remoteDriver):
self.env['runtime']['processManager'].addCustomEventThread(self.watchDog, multiprocess=True) self.env['runtime']['processManager'].addCustomEventThread(self.watchDog, multiprocess=True)
def watchDog(self, active, eventQueue): def watchDog(self, active, eventQueue):
# echo "command say this is a test" | socat - UNIX-CLIENT:/tmp/fenrirscreenreader-deamon.sock # echo "command say this is a test" | socat - UNIX-CLIENT:/tmp/fenrirscreenreader-deamon.sock
socketFile = ''
if self.env['runtime']['settingsManager'].getSetting('screen', 'driver') =='vcsaDriver': try:
socketpath = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'socketpath') + 'fenrirscreenreader-deamon.sock' socketFile = self.env['runtime']['settingsManager'].getSetting('remote', 'socketFile')
else: except:
socketpath = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'socketpath') + 'fenrirscreenreader-' + str(os.getpid()) + '.sock' pass
if os.path.exists(socketpath): if socketFile == '':
os.remove(socketpath) if self.env['runtime']['settingsManager'].getSetting('screen', 'driver') =='vcsaDriver':
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) socketFile = '/tmp/fenrirscreenreader-deamon.sock'
self.sock.bind(socketpath) else:
self.sock.listen(1) socketFile = '/tmp/fenrirscreenreader-' + str(os.getppid()) + '.sock'
if self.env['runtime']['settingsManager'].getSetting('screen', 'driver') =='vcsaDriver': if os.path.exists(socketFile):
os.chmod(socketpath, 0o222) os.unlink(socketFile)
while active.value == 1: self.fenrirSock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client_sock, client_addr = self.sock.accept() self.fenrirSock.bind(socketFile)
if client_sock: os.chmod(socketFile, 0o222)
# Check if the client is still connected and if data is available: self.fenrirSock.listen(1)
try: while active.value:
r, w, e = select.select([client_sock,], [], []) # Check if the client is still connected and if data is available:
except select.error: try:
return r, _, _ = select.select([self.fenrirSock], [], [], 0.8)
if len(r) > 0: except select.error:
rawdata = client_sock.recv(8129) break
try: if r == []:
data = rawdata.decode("utf-8").rstrip().lstrip() continue
eventQueue.put({"Type":fenrirEventType.RemoteIncomming, if self.fenrirSock in r:
"Data": data client_sock, client_addr = self.fenrirSock.accept()
}) try:
except: rawdata = client_sock.recv(8129)
pass except:
pass
try:
data = rawdata.decode("utf-8").rstrip().lstrip()
eventQueue.put({"Type":fenrirEventType.RemoteIncomming,
"Data": data
})
except:
pass
try:
client_sock.close() client_sock.close()
except:
if os.path.exists(socketpath): pass
os.remove(socketpath) if self.fenrirSock:
if self.sock: self.fenrirSock.close()
self.sock.close() self.fenrirSock = None
self.sock = None if os.path.exists(socketFile):
os.unlink(socketFile)

View File

@ -179,13 +179,13 @@ class driver(screenDriver):
try: try:
msgBytes = self.readAll(sys.stdin.fileno(), len=4096) msgBytes = self.readAll(sys.stdin.fileno(), len=4096)
except (EOFError, OSError): except (EOFError, OSError):
active.value = False eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
break break
if self.shortcutType == 'KEY': if self.shortcutType == 'KEY':
try: try:
self.injectTextToScreen(msgBytes) self.injectTextToScreen(msgBytes)
except: except:
active.value = False eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
break break
else: else:
eventQueue.put({"Type":fenrirEventType.ByteInput, eventQueue.put({"Type":fenrirEventType.ByteInput,
@ -195,7 +195,7 @@ class driver(screenDriver):
try: try:
msgBytes = self.readAll(self.p_out.fileno(), interruptFd=sys.stdin.fileno()) msgBytes = self.readAll(self.p_out.fileno(), interruptFd=sys.stdin.fileno())
except (EOFError, OSError): except (EOFError, OSError):
active.value = False eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
break break
# feed and send event bevore write, the pyte already has the right state # feed and send event bevore write, the pyte already has the right state
# so fenrir already can progress bevore os.write what should give some better reaction time # so fenrir already can progress bevore os.write what should give some better reaction time
@ -206,7 +206,7 @@ class driver(screenDriver):
self.injectTextToScreen(msgBytes, screen=sys.stdout.fileno()) self.injectTextToScreen(msgBytes, screen=sys.stdout.fileno())
except Exception as e: # Process died? except Exception as e: # Process died?
print(e) print(e)
active.value = False eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
finally: finally:
os.kill(self.p_pid, signal.SIGTERM) os.kill(self.p_pid, signal.SIGTERM)
self.p_out.close() self.p_out.close()