0

I'm trying to use coroutine in order to send data to NATS (nats.io messaging system).

When I try to call this code without arguments and for loop it's working perfectly.

But when I am adding arguments, the yield nc.connect function wouldn't return anything and continue to main function.

How can I call any coroutine with arguments?

@tornado.gen.coroutine
def process_events_list(events):  
    try: 
        nc = NATS()
        parser = SafeConfigParser()
        conf = os.path.realpath(
        os.path.join(os.getcwd(),'ev_nats\\ev_nats.ini'))
        parser.read(conf)
        endpoints = ast.literal_eval(parser.get('Nats', 'Servers'))
        subject = parser.get('Nats', 'Subject')
        opts = {"servers": endpoints}
        **yield nc.connect(**opts)**  # wont connect return to main        
        for ev in events:
            yield nc.publish(subject, ev)
        yield nc.flush()
        log("Published")
    except Exception, e:
        log(e)

if __name__=='__main__': # if run directly, not called by event_dispatcher.py
   evt = ['1','2','3']
   tornado.ioloop.IOLoop.instance().run_sync(lambda : process_events_list(evt))
MIkCode
  • 2,655
  • 5
  • 28
  • 46

1 Answers1

0

I would try to debug further whether the list of endpoints that you are passing to the server is valid. Otherwise, doing something like this should work if you can connect to the server.

# coding: utf-8
import tornado.ioloop
import tornado.gen
from nats.io.client import Client as NATS

@tornado.gen.coroutine
def main(events):
    print("Args:", events)
    nc = NATS()

    # Establish connection to the server.
    options = { "servers": ["nats://127.0.0.1:4222"] }
    yield nc.connect(**options)

    for e in events:
        yield nc.publish("example", "event:{}".format(e))

if __name__ == '__main__':
    events = ['1', '2', '8']
    tornado.ioloop.IOLoop.instance().run_sync(lambda: main(events))
wallyqs
  • 7,456
  • 5
  • 26
  • 25