#启动lookup
nsqlookupd
#启动一个nsqd , 并指定lookup的地址
nsqd --lookupd-tcp-address=127.0.0.1:4160
#打开一个监控网址
nsqadmin --lookupd-http-address=127.0.0.1:4161
安装python依赖包
pip install pynsq
编写生产者:nsq.create.py
import nsq
import tornado.ioloop
import time
def pub_message():
writer.pub('test_topic', time.strftime('%H:%M:%S'), finish_pub)
def finish_pub(conn, data):
print data
writer = nsq.Writer(['127.0.0.1:4150'])
tornado.ioloop.PeriodicCallback(pub_message, 1000).start()
nsq.run()
编写消费者:nsq.consume.py
import nsq
def handler(message):
print message
print message.body
return True
r = nsq.Reader(message_handler=handler,nsqd_tcp_addresses=['127.0.0.1:4150'],topic='test_topic', channel='asdfxx', lookupd_poll_interval=15)
nsq.run() #tornado.ioloop.IOLoop.instance().start()
测试:
python nsq.consume.py;
python nsq.create.py;