如何在python(tornado)中运行websocket循环外的函数

时间:2012-09-18 14:28:10

标签: python websocket tornado

我试图通过websockets设置一个公共Twitter流的小例子。这是我的websocket.py,它正在运作。

我想知道的是:我如何与来自' outside'以外的websocket交互?类WSHandler(即不仅在从websocket.js接收消息时回答)?假设我想在同一个脚本中运行一些其他函数来发布"你好!"每隔五秒钟将其发送到websocket(浏览器),而无需客户端的任何交互。我怎么能这样做?

因此,我认为,这是一个基本的初学者问题,关于如何处理下面的课程。任何方向的任何指针将不胜感激!

import os.path
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web

# websocket
class FaviconHandler(tornado.web.RequestHandler):
    def get(self):
        self.redirect('/static/favicon.ico')

class WebHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("websockets.html")

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hi, client: connection is made ...")

    def on_message(self, message):
        print 'message received: \"%s\"' % message
        self.write_message("Echo: \"" + message + "\"")
        if (message == "green"):
            self.write_message("green!")

    def on_close(self):
        print 'connection closed'



handlers = [
    (r"/favicon.ico", FaviconHandler),
    (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': 'static'}),
    (r'/', WebHandler),
    (r'/ws', WSHandler),
]

settings = dict(
    template_path=os.path.join(os.path.dirname(__file__), "static"),
)

application = tornado.web.Application(handlers, **settings)

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

3 个答案:

答案 0 :(得分:19)

你可以打电话给

IOLoop.add_timeout(deadline, callback)

在指定的截止时间超时(一次拍摄,但您可以重新安排)调用回调,或使用

tornado.ioloop.PeriodicCallback如果你有更周期性的任务。

请参阅:http://www.tornadoweb.org/en/stable/ioloop.html#tornado.ioloop.IOLoop.add_timeout

更新:某些示例

import datetime

def test():
    print "scheduled event fired"
...

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    main_loop = tornado.ioloop.IOLoop.instance()
    # Schedule event (5 seconds from now)
    main_loop.add_timeout(datetime.timedelta(seconds=5), test)
    # Start main loop
    main_loop.start()

在5秒后调用test()

更新2:

import os.path
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web

# websocket
class FaviconHandler(tornado.web.RequestHandler):
    def get(self):
        self.redirect('/static/favicon.ico')

class WebHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("websockets.html")

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hi, client: connection is made ...")
        tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=5), self.test)

    def on_message(self, message):
        print 'message received: \"%s\"' % message
        self.write_message("Echo: \"" + message + "\"")
        if (message == "green"):
            self.write_message("green!")

    def on_close(self):
        print 'connection closed'

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

handlers = [
    (r"/favicon.ico", FaviconHandler),
    (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': 'static'}),
    (r'/', WebHandler),
    (r'/ws', WSHandler),
]

settings = dict(
    template_path=os.path.join(os.path.dirname(__file__), "static"),
)

application = tornado.web.Application(handlers, **settings)

import datetime

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

答案 1 :(得分:14)

我偶然发现了类似的问题。这是我的解决方案。希望这对那里的人有所帮助

wss = []
class wsHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'Online'
        if self not in wss:
            wss.append(self)

    def on_close(self):
        print 'Offline'
        if self in wss:
            wss.remove(self)

def wsSend(message):
    for ws in wss:
        ws.write_message(message)

要向您的websockets发送消息,只需使用:

wsSend(message)

wsSend update

我偶尔会遇到wsSend异常。为了解决这个问题,我将代码修改为以下内容:

def wsSend(message):
    for ws in wss:
        if not ws.ws_connection.stream.socket:
            print "Web socket does not exist anymore!!!"
            wss.remove(ws)
        else:
            ws.write_message(message)

答案 2 :(得分:1)

另一种方法是使用 pub-sub 模块。

意味着您已将订阅连接到它,而不是为每个连接设置超时,您只需在上述一段时间后设置一个超时发布。 / p>

可能最强的一个是 redis 。还有一些专门用于龙卷风的模块:例如toredisbrükva

当然,对于一个简单的页面来说,这可能不是必需的,但是在你设置它之后,它可以很好地扩展并且非常适合维护/扩展。