Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

How to start a async tornado @gen.coroutine

Thu Dec 01, 2016 7:39 pm

I do run a webserver by tornado.
It works fine.

I do try to start a async routine that don't start. I don't know why?
I am able to run a time controlled routine by:

Code: Select all

  @classmethod
    def write_to_clients(cls):
#        print ("Writing to clients: ", cls.clients)
        for client in cls.clients:
            client.write_message("Ben je er?")
#            print ("client : ", client)
        tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=300), WebSocketHandler.write_to_clients) # herstart deze procedure na elke 5 minuten

if __name__ == "__main__":
    con = mdb.connect('127.0.0.1', 'pi', 'raspberry', 'tempdb');
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[
            (r"/", WebSocketHandler)
        ]
    )
    httpServer = tornado.httpserver.HTTPServer(app)
    httpServer.listen(options.port)
    print ("Listening on port:", options.port)
    tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=15), WebSocketHandler.write_to_clients) # start sturen van een controle bericht naar de nodemcu's na 15 seonde. routine: write_to_clients
    tornado.ioloop.IOLoop.instance().start()
It start after 15 seconds routine "write_to_clients"and in this routine I do start the routine again after 5 minutes.
That works fine.

Now I do like to start an async gen.coroutine by:

Code: Select all

@classmethod
    def write_to_clients(cls):
#        print ("Writing to clients: ", cls.clients)
        for client in cls.clients:
            client.write_message("Ben je er?")
#            print ("client : ", client)
        tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=300), WebSocketHandler.write_to_clients) # herstart deze procedure na elke 5 minuten

    @gen.coroutine
    def minute_loop():
       while True:
          print( "Gen")
          yield WebSocketHandler.write_to_clients()
          yield gen.sleep(60)


if __name__ == "__main__":
    con = mdb.connect('127.0.0.1', 'pi', 'raspberry', 'tempdb');
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[
            (r"/", WebSocketHandler)
        ]
    )
    httpServer = tornado.httpserver.HTTPServer(app)
    httpServer.listen(options.port)
    print ("Listening on port:", options.port)
    tornado.ioloop.IOLoop.current().spawn_callback(WebSocketHandler.minute_loop)
    tornado.ioloop.IOLoop.instance().start()

This gives a lot off errors.:

Code: Select all

sudo python3 webserver.py
Listening on port: 222
Gen
[E 161201 20:37:44 ioloop:585] Exception in callback functools.partial(<function wrap.<locals>.null_wrapper at 0x76157e40>, <tornado.concurrent.Future object at 0x761539d0>)
    Traceback (most recent call last):
      File "/usr/local/lib/python3.4/dist-packages/tornado-4.0.2-py3.4-linux-armv7l.egg/tornado/ioloop.py", line 565, in _run_callback
        ret = callback()
      File "/usr/local/lib/python3.4/dist-packages/tornado-4.0.2-py3.4-linux-armv7l.egg/tornado/stack_context.py", line 275, in null_wrapper
        return fn(*args, **kwargs)
      File "/usr/local/lib/python3.4/dist-packages/tornado-4.0.2-py3.4-linux-armv7l.egg/tornado/ioloop.py", line 571, in <lambda>
        self.add_future(ret, lambda f: f.result())
      File "/usr/local/lib/python3.4/dist-packages/tornado-4.0.2-py3.4-linux-armv7l.egg/tornado/concurrent.py", line 109, in result
        raise_exc_info(self._exc_info)
      File "<string>", line 3, in raise_exc_info
      File "/usr/local/lib/python3.4/dist-packages/tornado-4.0.2-py3.4-linux-armv7l.egg/tornado/gen.py", line 631, in run
        yielded = self.gen.throw(*sys.exc_info())
      File "webserver.py", line 101, in minute_loop
        yield WebSocketHandler.write_to_clients()
      File "/usr/local/lib/python3.4/dist-packages/tornado-4.0.2-py3.4-linux-armv7l.egg/tornado/gen.py", line 628, in run
        value = future.result()
      File "/usr/local/lib/python3.4/dist-packages/tornado-4.0.2-py3.4-linux-armv7l.egg/tornado/concurrent.py", line 111, in result
        raise self._exception
    tornado.gen.BadYieldError: yielded unknown object None
new connection from : 192.168.2.133
^CTraceback (most recent call last):
  File "webserver.py", line 118, in <module>
    tornado.ioloop.IOLoop.instance().start()
  File "/usr/local/lib/python3.4/dist-packages/tornado-4.0.2-py3.4-linux-armv7l.egg/tornado/ioloop.py", line 812, in start
    event_pairs = self._impl.poll(poll_timeout)
KeyboardInterrupt
I don't understand the errors
What is wrong ?? And what is the change for the code I need to make

Return to “Python”