You shouldn't write a message on every IOLoop cycle: you'll overwhelm your system. You want to send it every few milliseconds or seconds. A coroutine will do nicely:
import datetime
from tornado.ioloop import IOLoop
from tornado import gen
handlers = set()
@gen.coroutine
def auto_loop():
while True:
for handler in handlers:
handler.write_message('automatic message')
yield gen.Task(
IOLoop.current().add_timeout,
datetime.timedelta(milliseconds=500))
if __name__ == '__main__':
# ... application setup ...
# Start looping.
auto_loop()
IOLoop.current().start()
In MyHandler.open(), do handlers.add(self)
, and in MyHandler.on_close() do handlers.discard(self)
.