From 348ee5e4ad32f6d5b2389c976daa24350527296c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Sat, 17 Feb 2024 17:28:02 +0100 Subject: [PATCH] made tube index async functipn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- server/app.py | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/server/app.py b/server/app.py index ba6ee4b..0020b7d 100644 --- a/server/app.py +++ b/server/app.py @@ -7,10 +7,8 @@ import os from getmac import get_mac_address import time import sys -from multiprocessing import Process, Manager - -thread_manager = Manager() -TUBE_INDEX = thread_manager.list() +from multiprocessing import Process +import asyncio app = Flask(__name__) @@ -116,13 +114,29 @@ def connect_mqtt(): client.connect("localhost", 1883) return client -def mqtt_publisher(): +async def tube_index_updater(): + while True: + cur = db.cursor() + cur.execute("SELECT mac_address, universe, dmx_address FROM tubes") + global TUBE_INDEX + TUBE_INDEX = cur.fetchall() + cur.close() + print("Updated tube index: "+str(TUBE_INDEX)) + time.sleep(1) + +if __name__ == "__main__": + flask_thread = Process(target=flask_api) + flask_thread.start() + + index_updater = asyncio.get_event_loop() + index_updater.run_until_complete(tube_index_updater()) + index_updater.close() + # Create and start a thread for each universe mqtt_client = connect_mqtt() artnetBindIp = get_eth0_ip() artNet = Artnet.Artnet(BINDIP = artnetBindIp, DEBUG = True, SHORTNAME = "PiXelTubeMaster", LONGNAME = "PiXelTubeMaster", PORT = 6454) while True: - global TUBE_INDEX try: # Gets whatever the last Art-Net packet we received is artNetPacket = artNet.readPacket() @@ -146,21 +160,4 @@ def mqtt_publisher(): mqtt_client.publish(p1_topic, str([str([p1_r, p1_g, p1_b]), str([p2_r, p2_g, p2_b]), str([p3_r, p3_g, p3_b]), str([p4_r, p4_g, p4_b]), str([p5_r, p5_g, p5_b]), str([p6_r, p6_g, p6_b])])) except KeyboardInterrupt: artNet.close() - sys.exit() - -def tube_index_updater(): - while True: - cur = db.cursor() - cur.execute("SELECT mac_address, universe, dmx_address FROM tubes") - TUBE_INDEX = cur.fetchall() - cur.close() - print("Updated tube index: "+str(TUBE_INDEX)) - time.sleep(1) - -if __name__ == "__main__": - tube_index_updater_thread = Process(target=tube_index_updater) - tube_index_updater_thread.start() - flask_thread = Process(target=flask_api) - flask_thread.start() - publisher_thread = Process(target=mqtt_publisher) - publisher_thread.start() \ No newline at end of file + sys.exit() \ No newline at end of file