Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Created July 27, 2020 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallyqs/05f050deac57a367d37673e3e07965d5 to your computer and use it in GitHub Desktop.
Save wallyqs/05f050deac57a367d37673e3e07965d5 to your computer and use it in GitHub Desktop.
import asyncio
from nats.aio.client import Client as NATS
async def run(loop):
nc = NATS()
await nc.connect("nats://demo.nats.io:4222", loop=loop)
future = asyncio.Future()
async def cb(msg):
nonlocal future
future.set_result(msg)
await nc.subscribe("updates", cb=cb)
await nc.publish("updates", b'OK')
await nc.flush()
# Wait for message to come in
msg = await asyncio.wait_for(future, 1)
print("[Received]:", msg)
await nc.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(run(loop))
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment