Google Groups

Re: Local Socket + Tornadio Multi-Threading

Serge S. Koval Oct 31, 2011 12:14 PM
Posted in group: Tornado Web Server

 There's no problem to use threads in the Tornado (or Tornadio), but you have to follow some rules:

 Anyway, if you want to use thread to poll your device, that's fine, but you have to separate polling thread from your Tornado application by using io_loop.add_callback() to send data to main thread.

 So, you can do something like:
1. Declare your TornadIO connection class:

class MyConnection(SocketConnection):
    participants = set()


    def broadcast(cls, msg):
        for p in cls.participants:

    def controller_msg(cls, msg):

2. In your device polling thread, do something like:

    while True: 
            datum = file.readline() 
            if len(datum) > 2: 
                t = json.loads(datum) 
                def callback():


3. ...
4. Profit!

Basically, callback() will be executed in Tornado main thread and will call TornadIO handler class, which will then broadcast message to everyone else.

Hope this helps.