pyList

python写的nsq 发送者和消费者

基于tornado pip 镜像 http://pypi.gocept.com/simple/ 3 pip install pynsq

首先执行命令:

1.nsqlookupd
2.nsqd --lookupd-tcp-address=127.0.0.1:4160

然后写py脚本并运行: (文档:https://pynsq.readthedocs.org/en/latest/writer.html 8)

#消费者:

import nsq

def handler(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() #这run调用了tornado.ioloop.IOLoop.instance().start()

#生产者:

import nsq
import tornado.ioloop
import time

def pub_message():
    def finish_pub(conn, data):
        print data
    writer.pub('test_topic', time.strftime('%H:%M:%S'), finish_pub)

writer = nsq.Writer(['127.0.0.1:4150'])
tornado.ioloop.PeriodicCallback(pub_message, 5000).start()
nsq.run()
本文网址: https://pylist.com/t/1421145926 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles