-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkv.py
More file actions
46 lines (32 loc) · 1.03 KB
/
kv.py
File metadata and controls
46 lines (32 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
from natsrpy import Nats
from natsrpy.js import KVConfig
async def main() -> None:
"""Main function to run the example."""
nats = Nats(["nats://localhost:4222"])
await nats.startup()
js = await nats.jetstream()
kv = await js.kv.create_or_update(KVConfig(bucket="kv-example"))
watcher = await kv.watch("test-key")
await kv.put("test-key", "one")
await kv.put("test-key", b"two")
# To obtain bytes value.
value = await kv.get("test-key")
if value:
print("[VALUE]", value.decode()) # noqa: T201
# To get kv-entry with all
# the metadata.
entry = await kv.entry("test-key")
if entry:
print("[ENTRY]", entry) # noqa: T201
await kv.delete("test-key")
# Alternatively you can
# use await watcher.next()
async for event in watcher:
print("[EVENT]", event) # noqa: T201
break
await js.kv.delete(kv.name)
# Don't forget to call shutdown.
await nats.shutdown()
if __name__ == "__main__":
asyncio.run(main())