python Tornado websockets how to send message every X seconds?

后端 未结 2 1461
轻奢々
轻奢々 2021-02-14 02:17

I am trying to cobble together a test which allows websockets clients to connect to a Tornado server and I want the Tornado server to send out a message to all clients every X s

2条回答
  •  别那么骄傲
    2021-02-14 02:54

    Found that the accepted answer for this is almost exactly what I want:

    How to run functions outside websocket loop in python (tornado)

    With a slight modification, the accepted answer at the above link continually sends out ping messages. Here is the mod:

    Change:

    def test(self):
        self.write_message("scheduled!")
    

    to:

    def test(self):
        self.write_message("scheduled!")
        tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=5), self.test)
    

提交回复
热议问题