No callback in ioloop



  • There's a tcp-client on the tornado. Here's his launch code:

    @gen.coroutine
    def main():
        factory = TCPClient()
        # Подсоединяемся по сокету
        stream = yield factory.connect(af=socket.AF_INET, **options.options.group_dict("connect"))
        # Добавляем callback
        ioloop.IOLoop.instance().add_callback(notification, stream)
        # Запускаем приложение
        app = Application(stream)
        app.run()
    

    if name == 'main':
    try:
    main()
    ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
    pass

    The app is starting, it's okay, but there's a problem, because I don't know why I'm not starting. callbacknotification

    Introducing the function of the annex itself:

    @gen.coroutine
    def run(self):
    while True:
    try:
    s = input('> ')
    command, text = self._parse_command(s)
    handler = self.handler(self._stream, self)
    yield handler.execute_command(command, text)
    except Exception as e:
    print(e)

    It's a function of the application, it writes something in the socket after data entry and expects a user input.

    And I'm adding the code to the function. notification, she reads something from the sketch and turns it on the screen:

    @gen.coroutine
    def notification(stream):
    message_length = yield stream.read_bytes(2)
    length = struct.unpack("!H", message_length)[0]
    message = yield stream.read_bytes(length)
    # request = Message.unpack(message=message)
    sys.stdout.write('\r'+' '*(len(readline.get_line_buffer())+2)+'\r')
    print(message)
    sys.stdout.write('> ' + readline.get_line_buffer())
    sys.stdout.flush()
    ioloop.IOLoop.instance().add_callback(notification, stream)

    The question is, how am I supposed to start a background notification function so she could read something from a skate, so she could get the information to the console?



  • There was a question. Actually, you can't start coronutin in a separate flow.

    s = yield EXECUTOR.submit(lambda: input('> '))

    It solved the problem.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2