Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Created July 14, 2016 22:46
Show Gist options
  • Save wallyqs/1f4f793c2865aca6e82bed2ea0354a5a to your computer and use it in GitHub Desktop.
Save wallyqs/1f4f793c2865aca6e82bed2ea0354a5a to your computer and use it in GitHub Desktop.
Example of asyncio-nats and threads using coroutines
import asyncio
import time
from threading import Thread
from nats.aio.client import Client as NATS
from nats.aio.errors import ErrConnectionClosed, ErrTimeout
class Component(object):
def __init__(self, nc, loop):
self.nc = nc
self.loop = loop
def response_handler(self, msg):
print("--- Received: ", msg.subject, msg.data)
@asyncio.coroutine
def another_handler(self, msg):
print("--- Another: ", msg.subject, msg.data, msg.reply)
yield from self.nc.publish(msg.reply, b'I can help!')
def run(self):
yield from self.nc.connect(io_loop=self.loop)
yield from self.nc.subscribe("hello", cb=self.response_handler)
yield from self.nc.subscribe("another", cb=self.another_handler)
yield from self.nc.flush()
def another_thread(c):
# Should have ensured that we are connected by this point.
if not c.nc.is_connected:
print("Not connected to NATS!")
return
asyncio.run_coroutine_threadsafe(c.nc.subscribe("hi", cb=c.response_handler), loop=c.loop)
asyncio.run_coroutine_threadsafe(c.nc.flush(), loop=c.loop)
asyncio.run_coroutine_threadsafe(c.nc.publish("hello", b'world'), loop=c.loop)
asyncio.run_coroutine_threadsafe(c.nc.publish("hi", b'example'), loop=c.loop)
future = asyncio.run_coroutine_threadsafe(c.nc.timed_request("another", b'example'), loop=c.loop)
msg = future.result()
print("--- Got: ", msg.data)
def go():
# Starting the NATS client in this thread...
nc = NATS()
loop = asyncio.get_event_loop()
component = Component(nc, loop)
# Wait for coroutine to finish and ensure we are connected
loop.run_until_complete(component.run())
# Example using NATS client from another thread.
thr = Thread(target=another_thread, args=(component,))
thr.start()
loop.run_forever()
if __name__ == '__main__':
go()
@wallyqs
Copy link
Author

wallyqs commented Jul 14, 2016

Expected output, something like:

--- Another:  another b'example' _INBOX.1d03a119fc98b65f370f8a9
--- Received:  hello b'world'
--- Received:  hi b'example'
--- Got:  b'I can help!'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment