Search code examples
tornadopython-3.5ioloop

How to get the number of pending task in ioloop in tornado?


I'm testing a gateway server for websocket client connection. I currently open 10000 clients to connect and send messages to my gateway server, and it echos back all the messages. When I type 'ctrl + c' to stop the process, it takes a long time to stop, and so much stacktrace info. So I want to count the number of the pending task in the loop, and find a solution to stop ioloop quicker without that stacktrace info

I can't find any api about ioloop task counting in https://www.tornadoweb.org/en/stable/ioloop.html , so I ask here.

Server Side Code:

def create_application():
    return tornado.web.Application([(r'/', WebSocketHandler)])

if __name__ == '__main__':
    app = create_application()
    app.listen(60017)

Client Side Code:

if __name__=='__main__':
    try:
        for i in range(1, 10000):
            wsc = WSClient('ws://127.0.0.1:60017', 'pname_' + str(i))
            wsc.start()
            ws_list.append(wsc)
            print('ws_client pname[%s] started!!!'%('pname_' + str(i)))
        tornado.ioloop.IOLoop.current().add_timeout(time.time() + 1, counting_packs)
        tornado.ioloop.IOLoop.current().start()

    except Exception as e:
        print(str(e))

I expect to count the num of pending task every second... It would be appreciated if anyone can tell me how to do it. Thanks a lot!!!


Solution

  • Tornado does not currently offer any way to find out how many tasks are pending.

    If you just want to suppress the stack traces when you press ctrl-c and exit as quickly as possible, you can run signal.signal(signal.SIGINT, signal.SIG_DFL). This will also mean that you can't catch KeyboardInterrupt and recover from it.