Replies: 1 comment
-
I'm no maintainer but I think that this example cannot get much simpler that what it already is https://natsbyexample.com/examples/messaging/request-reply/python, it is both simple and well documented. Yes there is a single client, but it should be a good exercice for beginners to split the example into two different scripts. You can start by copy/pasting the example into two different scripts, remove everything related to requesting from one script, and remove everything related to subscribing in the other. nats sub
exampleimport os
import asyncio
import nats
# Get the list of servers.
servers = os.environ.get("NATS_URL", "nats://localhost:4222").split(",")
async def main():
# Create the connection to NATS which takes a list of servers.
nc = await nats.connect(servers=servers)
# In addition to vanilla publish-request, NATS supports request-reply
# interactions as well. Under the covers, this is just an optimized
# pair of publish-subscribe operations.
# The _request handler_ is a subscription that _responds_ to a message
# sent to it. This kind of subscription is called a _service_.
# We can use the `cb` argument for asynchronous handling.
async def greet_handler(msg):
# Parse out the second token in the subject (everything after
# `greet.`) and use it as part of the response message.
name = msg.subject[6:]
reply = f"hello, {name}"
await msg.respond(reply.encode("utf8"))
sub = await nc.subscribe("greet.*", cb=greet_handler)
# Waiting for OS signal is left to reader, instead we simply sleep 1 hour
try:
await asyncio.sleep(3600)
finally:
await nc.drain()
if __name__ == '__main__':
asyncio.run(main()) nats req
exampleimport os
import asyncio
import nats
from nats.errors import TimeoutError, NoRespondersError
# Get the list of servers.
servers = os.environ.get("NATS_URL", "nats://localhost:4222").split(",")
async def main():
# Create the connection to NATS which takes a list of servers.
nc = await nats.connect(servers=servers)
# Now we can use the built-in `request` method to do the service request.
# We simply pass a empty body since that is being used right now.
# In addition, we need to specify a timeout since with a request we
# are _waiting_ for the reply and we likely don't want to wait forever.
rep = await nc.request("greet.joe", b'', timeout=0.5)
print(f"{rep.data}")
rep = await nc.request("greet.sue", b'', timeout=0.5)
print(f"{rep.data}")
rep = await nc.request("greet.bob", b'', timeout=0.5)
print(f"{rep.data}")
await nc.close() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, Sorry to ask here, but I'm trying to get my head around NATS for a new project.
I'm trying to learn more about NATS and I'm working through the python examples here and here I started with the command line and tested the Request-Response pattern using these commands.
This worked as per the documentation and the two
reply
services respond in a kind ofround-robin
way.I now need the Python equivalents to continue my testing, but all of the examples are trying to be way to clever (at least for where I am currently in my learnings process ) - i.e. they include both the
request
andreply
logic in the same example (e.g. request-reply) which makes it much harder and confusing to understand what's happening.I can't seem to find any simple examples that break out the Python logic into separate components that behave in the same way as the CLI commands. Can anybody help me out here please.
Beta Was this translation helpful? Give feedback.
All reactions