first commit

This commit is contained in:
marys
2026-05-22 11:47:41 +02:00
commit 514cc3a6a9
4 changed files with 96 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
.venv
+65
View File
@@ -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/
+7
View File
@@ -0,0 +1,7 @@
[project]
name = "nats-python"
version = "0.1.0"
requires-python = ">=3.13"
dependencies = [
"nats-py>=2.14.0",
]
Generated
+23
View File
@@ -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" }]