Added python example

This commit is contained in:
Xackery 2018-03-10 19:47:02 -08:00
parent 22aef8ad79
commit 3bc9e4bc24
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,7 @@
Hello World NATS example
# helloworld2.py
* run `pip install nats-client`
* run `pip install protobuf`

View File

@ -0,0 +1,47 @@
# coding: utf-8
import tornado.ioloop
import tornado.gen
import time
import sys
sys.path.insert(0, "../../../protobuf/python/proto")
from datetime import datetime
from nats.io.utils import new_inbox
from nats.io import Client as NATS
import message_pb2
@tornado.gen.coroutine
def main():
nc = NATS()
# Establish connection to the server.
options = { "verbose": True, "servers": ["nats://127.0.0.1:4222"] }
yield nc.connect(**options)
def discover(msg=None):
channel_message = message_pb2.ChannelMessage()
channel_message.ParseFromString(msg.data)
print("[Received]: %s" % channel_message)
send_message = message_pb2.ChannelMessage()
send_message.chan_num = 5
#send_message.from = "python"
send_message.message = "Hello, World!"
sid = yield nc.subscribe("world.channel_message", "", discover)
yield nc.publish("world.channel_message", send_message.SerializeToString())
loop = tornado.ioloop.IOLoop.instance()
yield tornado.gen.Task(loop.add_timeout, time.time() + 20)
try:
start = datetime.now()
# Make roundtrip to the server and timeout after 1 second
yield nc.flush(1)
end = datetime.now()
print("Latency: %d µs" % (end.microsecond - start.microsecond))
except tornado.gen.TimeoutError, e:
print("Timeout! Roundtrip too slow...")
if __name__ == '__main__':
tornado.ioloop.IOLoop.instance().run_sync(main)