From e6931f3c8aa949f3b16c171c2376bc2d0fcfeaac Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 2 Dec 2024 12:30:46 +0000 Subject: [PATCH] try with nng --- nice.py | 43 +++++++++++++++++++++++++++++++++++++++++-- test_nng.py | 6 ++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test_nng.py diff --git a/nice.py b/nice.py index 5f9b495..006a1bd 100644 --- a/nice.py +++ b/nice.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 import asyncio +import pynng +#asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) from collections import defaultdict from pathlib import Path @@ -56,7 +58,7 @@ from starlette.middleware.base import BaseHTTPMiddleware # ui.html("

Uploader tool

") # ui.button('Choose file', on_click=pick_file, icon='folder') -SOCKET_PATH = 'tcp://localhost:9975' +SOCKET_PATH = 'tcp://localhost:9976' EXPORT_PATH = Path("./test/export") PROCESS_PATH = Path("./test/to_process") @@ -83,7 +85,8 @@ DELETE_FILES_ON_UPLOAD = False rqst = None -use_zmq = True +use_zmq = False +use_nng = True loaded_files: dict = {} loaded_series = defaultdict(list) loaded_series_data = {} @@ -91,6 +94,7 @@ uploaded_files: dict = {} duplicate_series = set() + if use_zmq: context = zmq.asyncio.Context() socket = context.socket(zmq.PULL) @@ -138,6 +142,38 @@ if use_zmq: case _: logger.error("invalid message") +if use_nng: + async def read_nng_messages(): + print("start nng") + try: + with pynng.Pair0(listen=SOCKET_PATH) as sub: + #sub.subscribe('') + while not app.is_stopped: + msg = await sub.arecv_msg() + data = msg.bytes.decode() + logger.debug(f'Received msg {data}') + match data: + case "loaded": + for client in Client.instances.values(): + if not client.has_socket_connection: + continue + with client: + ui.notify("Export done", color='positive') + global REFRESH_TRIGGERED + REFRESH_TRIGGERED = True + logger.debug("loaded") + case _: + logger.error("invalid message") + except pynng.exceptions.AddressInUse: + logger.debug("Address in use") + with pynng.Pair0(dial=SOCKET_PATH) as sub: + await sub.asend(b"loaded") + sys.exit(0) + + #asyncio.create_task(read_nng_messages()) + + + anonymizer = Anonymizer(ANON_PATH) async def upload_files_start(progress, upload_queue): @@ -547,6 +583,7 @@ def main_page(): with ui.expansion('Extra', icon='build').classes('w-full'): ui.button("Load files", on_click=partial(load_files_start, anon_progress, queue)) + ui.button("Load files from folder", on_click=partial(load_files_start, anon_progress, queue)) ui.button("Reload anon", on_click=partial(reload_anonymized_start, anon_progress, queue)) with ui.expansion('File status', icon='view_list').classes('w-full'): @@ -583,6 +620,8 @@ if __name__ in {"__main__", "__mp_main__"}: if use_zmq: app.on_startup(read_loop) + if use_nng: + app.on_startup(read_nng_messages) ui.run(storage_secret="123456", reload=False, show=False) diff --git a/test_nng.py b/test_nng.py new file mode 100644 index 0000000..85063c1 --- /dev/null +++ b/test_nng.py @@ -0,0 +1,6 @@ +import pynng + +with pynng.Pair0(listen='tcp://127.0.0.1:54321') as s1: + + with pynng.Pair0(listen='tcp://127.0.0.1:54321') as s2: + pass \ No newline at end of file