Tornado服务器卡在循环中,不接受客户端

时间:2013-07-12 12:31:29

标签: python stream websocket tornado infinite

我在龙卷风中编写了一个代码,该代码连接到推送无限数据流的服务器,处理数据流并将其发送到websocket服务器上。 问题是我实现它的方式服务器在特定功能上被阻止,并且不再接受任何更多客户端,因为它永远不会退出向websocket提供数据的功能。我希望连接到服务器,并且从中检索的数据只处理一次,但是将处理后的数据发送到连接到龙卷风服务器的所有客户端。有人可以帮助我,我无法想办法做到这一点。这是删除数据处理的代码:

import socket
import ssl
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web

websockets = []

class WSHandler(tornado.websocket.WebSocketHandler):


  def readData(self):
    while True:
        line = self.ssl_sock.read()

        #PROCESS THE READ LINE AND CONVERT INTO RESULTING DATA


        if(toSend):
                self.write_message(result)

  def makeConnection(self):
      self.ssl_sock.connect(self.address)
      self.readData()



  def open(self):
    print 'New connection was opened'
    self.s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.ssl_sock=ssl.wrap_socket(self.s, cert_reqs=ssl.CERT_NONE)
    self.address=('SERVER_ADDRESS',5000)
    self.nodes=[]
    self.edges=[]
    if self not in websockets:
        print ('added')
        websockets.append(self)

    if(len(websockets)==1):
        print('executing make conn')
        self.makeConnection()
    else:
        self.readData()
        print('executing read data')


  def on_message(self, message):
    print 'Incoming message:', message
    self.write_message("You said: " + message)

  def on_close(self):
    print 'Connection was closed...'


application = tornado.web.Application([
  (r'/ws', WSHandler),
])

if __name__ == "__main__":

  http_server = tornado.httpserver.HTTPServer(application)
  http_server.listen(8888)
  tornado.ioloop.IOLoop.instance().start()

1 个答案:

答案 0 :(得分:0)

Tornado是一个异步框架,也就是说,所有IO必须在其事件循环中运行,否则整个服务器都会卡住。

试着查看Tornado Async Client