0

我想要的只是在一个线程中运行一个 ioloop,然后将消息写入 nsqd。这是一个尝试:

#!/usr/bin/env python2
# coding=utf-8

import tornado
import time
from nsq.writer import Writer

w = Writer(["bj1:4150", "bj2:4150"], reconnect_interval=15)
w.connect()


@tornado.gen.coroutine
def future_pub(topic, msg):
    result = w.pub(topic, msg)
    print(result)
    time.sleep(3)
    raise tornado.gen.Return(result)


@tornado.gen.coroutine
def main():
    while True:
        try:
            future = future_pub("test", "message")
            yield future
        except Exception as e:
            print(e.message)


tornado.ioloop.IOLoop.instance().run_sync(main)

但它没有用。你可以帮帮我吗?谢谢!

4

1 回答 1

0

你从来没有跑过nsq.run(),所以没有打开连接,你Writer不能发布消息。
请参阅文档中的示例:https ://pynsq.readthedocs.org/en/latest/writer.html ,它PeriodicCallback用于每秒发送一次消息。

于 2016-03-29T14:43:50.743 回答