From 44cb97b44e062020c51fc6a3b6ca3d977dc96d05 Mon Sep 17 00:00:00 2001 From: Rick Rongen Date: Fri, 28 Aug 2020 13:39:21 +0200 Subject: [PATCH] feat: split camera worker and tick worker --- cameraworker.py | 28 ++++++++++++++++++++++++++++ worker.py => tickworker.py | 11 +---------- 2 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 cameraworker.py rename worker.py => tickworker.py (87%) diff --git a/cameraworker.py b/cameraworker.py new file mode 100644 index 0000000..8a152a6 --- /dev/null +++ b/cameraworker.py @@ -0,0 +1,28 @@ +import sched + +from redis import Redis + +from control.camera import Camera + +INTERVAL_TICK = 0.0125 # 20/s +INTERVAL_IMAGE = 0.05 # 20/s + + +scheduler = sched.scheduler() +camera = Camera() + + +def camera_image(): + scheduler.enter(INTERVAL_IMAGE, 1, camera_image) + image = camera.get_image() + redisdb.publish('image', image) + + +scheduler.enter(INTERVAL_IMAGE, 1, camera_image) + +if __name__ == '__main__': + redisdb = Redis() + try: + scheduler.run() + except KeyboardInterrupt: + raise diff --git a/worker.py b/tickworker.py similarity index 87% rename from worker.py rename to tickworker.py index 646ad7d..1ba0a7f 100644 --- a/worker.py +++ b/tickworker.py @@ -6,10 +6,8 @@ import json from redis import Redis from control.walle import WallE -from control.camera import Camera INTERVAL_TICK = 0.0125 # 20/s -INTERVAL_IMAGE = 0.05 # 20/s class EventListener(Thread): @@ -51,23 +49,16 @@ class EventListener(Thread): scheduler = sched.scheduler() -camera = Camera() walle = WallE() walle.setup() + def walle_tick(): scheduler.enter(INTERVAL_TICK, 1, walle_tick) walle.tick() -def camera_image(): - scheduler.enter(INTERVAL_IMAGE, 1, camera_image) - image = camera.get_image() - redisdb.publish('image', image) - - scheduler.enter(INTERVAL_TICK, 1, walle_tick) -scheduler.enter(INTERVAL_IMAGE, 1, camera_image) if __name__ == '__main__': redisdb = Redis()