feat: quit gracefully redis

This commit is contained in:
2020-08-28 13:34:29 +02:00
parent c934535c06
commit 114afb09ff

View File

@@ -1,4 +1,5 @@
import sched
import time
from threading import Thread
import json
@@ -14,12 +15,16 @@ INTERVAL_IMAGE = 0.05 # 20/s
class EventListener(Thread):
def __init__(self):
super(EventListener, self).__init__()
self.keep_running = True
self.dbcon = Redis()
self.pubsub = self.dbcon.pubsub()
self.pubsub.subscribe(['move', 'look'])
def run(self) -> None:
for item in self.pubsub.listen():
while self.keep_running:
item = self.pubsub.get_message()
if not item:
time.sleep(0.01)
channel = item['channel'].decode()
data = item['data']
if not isinstance(data, bytes):
@@ -29,9 +34,10 @@ class EventListener(Thread):
self.handle_message(channel, data)
except ValueError:
pass # todo
self.pubsub.close()
def stop(self):
self.pubsub.close()
self.keep_running = False
def handle_message(self, channel: str, data: bytes):
data_dict = json.loads(data.decode())