Created
July 27, 2020 16:18
-
-
Save wallyqs/05f050deac57a367d37673e3e07965d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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