socket server

Socket implementation starts PeriodicCallback to return different data when receiving different messages, and only closes one of the timers when closing

First, you need to modify MainHandler to add a dictionary callbacks to save the callback function and other related information corresponding to each message, for example:

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    callbacks = {}

    def on_message(self, message):
        if message == "start_1":
            self.start_callback(1)
        elif message == "start_2":
            self.start_callback(2)
        elif message == "stop_1":
            self.stop_callback(1)
        elif message == "stop_2":
            self.stop_callback(2)
        else:
            self.write_message("Unknown command")

    def start_callback(self, callback_id):
        if callback_id not in self.callbacks:
            pc = tornado.ioloop.PeriodicCallback(lambda: self.write_message(f"Callback {callback_id}: some data"), 1000)
            pc.start()
            self.callbacks[callback_id] = {"callback": pc, "data": None}
            self.write_message(f"Callback {callback_id} started")
        else:
            self.write_message(f"Callback {callback_id} already started")

    def stop_callback(self, callback_id):
        if callback_id in self.callbacks:
            self.callbacks[callback_id]["callback"].stop()
            self.write_message(f"Callback {callback_id} stopped")
            del self.callbacks[callback_id]
        else:
            self.write_message(f"Callback {callback_id} not started")

app = tornado.web.Application([(r"/", MainHandler)])
app.listen(8888)

tornado.ioloop.IOLoop.current().start()

In the above code, we added a callbacks dictionary to save the callback function and other related information corresponding to each message. In the on_message method, when the message "start_1" or "start_2" is received, call the self.start_callback method to start the corresponding timer, and save the timer and other related information into the callbacks dictionary. In the start_callback method, if there is no corresponding callback function in the callbacks dictionary, create a new PeriodicCallback object and start it; otherwise, return the started timer directly. In the callback function executed by the timer, we return the corresponding data through the self.write_message method. When receiving the message "stop_1" or "stop_2", call the self.stop_callback method to stop the corresponding timer, and delete the corresponding information from the callbacks dictionary. In the stop_callback method, if there is a corresponding callback function in the callbacks dictionary, call its stop method to stop the timer, and delete the corresponding information from the dictionary; otherwise, return the corresponding error message directly.

It should be noted that in the above code, we use lambda expression to create the callback function of the timer, so that the callback_id parameter can be accessed in the callback function, so as to return different messages according to different messages.

Author: Yan Chun

Guess you like

Origin blog.csdn.net/ekcchina/article/details/130562902