From 514cc3a6a9e89f00237802d1c3e09bbe55026c23 Mon Sep 17 00:00:00 2001 From: marys Date: Fri, 22 May 2026 11:47:41 +0200 Subject: [PATCH] first commit --- .gitignore | 1 + main.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 7 ++++++ uv.lock | 23 ++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 pyproject.toml create mode 100644 uv.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d17dae --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv diff --git a/main.py b/main.py new file mode 100644 index 0000000..72aecc7 --- /dev/null +++ b/main.py @@ -0,0 +1,65 @@ +import os +import asyncio +from xmlrpc import server + +import nats +from nats.errors import TimeoutError as NATSTimeoutError + + +NATS_SERVERS = os.getenv('NATS_URL', 'nats://localhost:4222').split(',') +NATS_TOKEN = os.getenv('NATS_TOKEN', '') + + +async def nats_subscribe(nc, subject): + sub = await nc.subscribe(subject) + while True: + try: + msg = await sub.next_msg(timeout=0.1) + print(f"Received a message on '{msg.subject}': {msg.data}") + except NATSTimeoutError: + continue + except KeyboardInterrupt: + await sub.unsubscribe() + return True + + +async def nats_publish(nc, subject, data): + count = 0 + while True: + try: + await nc.publish(subject, (f"{data}_{count}").encode()) + count += 1 + await asyncio.sleep(5) + except KeyboardInterrupt: + return True + + +async def main(): + print(f'Startting ...') + print(f'NATS_SERVER: {NATS_SERVERS}') + print(f'NATS_TOKEN: {NATS_TOKEN}') + + + nc = await nats.connect(servers=NATS_SERVERS, + token=NATS_TOKEN) + + asyncio.create_task(nats_subscribe(nc, "greet.*")) + asyncio.create_task(nats_publish(nc, "greet.bob", "hello")) + + + while True: + await asyncio.sleep(1) + + await nc.drain() + + + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + try: + asyncio.run(main()) + except KeyboardInterrupt: + pass + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..f497a01 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,7 @@ +[project] +name = "nats-python" +version = "0.1.0" +requires-python = ">=3.13" +dependencies = [ + "nats-py>=2.14.0", +] diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..22422f2 --- /dev/null +++ b/uv.lock @@ -0,0 +1,23 @@ +version = 1 +revision = 1 +requires-python = ">=3.13" + +[[package]] +name = "nats-py" +version = "2.14.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/c3/f8/b956c4621ba88748ed707c52e69f95b7a50c8914e750edca59a5bef84a76/nats_py-2.14.0.tar.gz", hash = "sha256:4ed02cb8e3b55c68074a063aa2687087115d805d1513297da90cb2068fb07bed", size = 120751 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f9/39/0e87753df1072254bac190b33ed34b264f28f6aa9bea0f01b7e818071756/nats_py-2.14.0-py3-none-any.whl", hash = "sha256:4116f5d2233ce16e63c3d5538fa40a5e207f75fcf42a741773929ddf1e29d19d", size = 82259 }, +] + +[[package]] +name = "nats-python" +version = "0.1.0" +source = { virtual = "." } +dependencies = [ + { name = "nats-py" }, +] + +[package.metadata] +requires-dist = [{ name = "nats-py", specifier = ">=2.14.0" }]