0

What I want is just run a ioloop in a thread, and write message to nsqd. Here is a try:

#!/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)

But it didn't work. Could you help me? Thank you!

Peng Qu
  • 379
  • 3
  • 12

1 Answers1

0

You never ran nsq.run() so no connections are opened and your Writer can't publish messages.
See the example in the docs: https://pynsq.readthedocs.org/en/latest/writer.html which uses PeriodicCallback for sending a message once a second.

Oliver
  • 11,857
  • 2
  • 36
  • 42
  • I have already read that doc. But it can't resolve my problem. In fact I want one thing: the writer can fetch message from a queue and waiting if no more message in the queue, but not sleep 1 second. Could you help me? – Peng Qu Mar 31 '16 at 13:19
  • The writer doesn't fetch messages from the queue, it writes them *to* a queue. Do you want a queue reader that then publishes each read messages to a different topic? It's not clear from your original question what you're trying to achieve. – Oliver Apr 12 '16 at 02:20
  • yes, that is what I want. Could you help me to implement a demo? Thanks a lot! – Peng Qu Apr 14 '16 at 12:04