You can run multiple HTTPServers (or other servers) on the same IOLoop.
It is possible to create multiple IOLoops and give each one its own thread, but...
import threadingimport timeimport tornado.httpserverimport tornado.ioloopimport tornado.webimport tornado.websocketclass HttpServer(threading.Thread):def __init__(self):super(self.__class__, self).__init__()self.start()def run(self):class HelloHandler(tornado.web.RequestHandler):def get(self):self.write('Hello!')tornado_handlers = [('/hello', HelloHandler),]tornado_app = tornado.web.Application(tornado_handlers)http_server = tornado.httpserver.HTTPServer(tornado_app, no_keep_alive=True)host = 'localhost'port = 8040http_server.bind(port, address=host)tornado_process_count = 1http_server.start(tornado_process_count)#app.listen(port)ioloop = tornado.ioloop.IOLoop.instance()print 'Serving HTTP on %s:%d' % (host, port)print dir(ioloop)ioloop.start()class WebsocketClient(threading.Thread):def __init__(self):super(self.__class__, self).__init__()self.start()@tornado.gen.coroutinedef _connect_to_server(self):client = tornado.httpclient.HTTPRequest('ws://localhost:8030/websocket')self.conn = yield tornado.websocket.websocket_connect(client, on_message_callback=self._on_message)def run(self):ioloop = tornado.ioloop.IOLoop.instance()ioloop.run_sync(self._connect_to_server)print 'Connected to websocket.'time.sleep(100000)def _on_message(self, msg):self.conn.write_message('Echo: ' + msg)http_server_thread = HttpServer()ws_server_thread = WebsocketClient()
import tornado.httpserverimport tornado.ioloopimport tornado.webimport tornado.websocket
class EchoWebSocket(tornado.websocket.WebSocketHandler):def open(self):print("WebSocket opened")def on_message(self, message):print 'Recv: ' + messageself.write_message(u"You said: " + message)def on_close(self):print("WebSocket closed")tornado_handlers = [('/websocket', EchoWebSocket),
]tornado_app = tornado.web.Application(tornado_handlers)http_server = tornado.httpserver.HTTPServer(tornado_app, no_keep_alive=True)
http_server.bind(8030, address='localhost', reuse_port=True)tornado_process_count = 1http_server.start(tornado_process_count)ioloop = tornado.ioloop.IOLoop.instance()ioloop.start()
Here's a clue from Ben at http://stackoverflow.com/a/30362810/487992You can run multiple HTTPServers (or other servers) on the same IOLoop.It is possible to create multiple IOLoops and give each one its own thread, but...Here's at attempt. The websocket client connects to the websocket but ioloop.start() in HttpServer produces "RuntimeError: IOLoop is already running".Switching both calls to IOLoop.instance() to IOLoop() runs w/o error, the websocket client connects to the websocket but the HTTP server does not respond.
Here's a clue from Ben at http://stackoverflow.com/a/30362810/487992You can run multiple HTTPServers (or other servers) on the same IOLoop.It is possible to create multiple IOLoops and give each one its own thread, but...
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import timeimport errnoimport socketimport tornado
import tornado.httpserverimport tornado.ioloopimport tornado.webimport tornado.websocket
class HelloHandler(tornado.web.RequestHandler):def get(self):try:ws_client.conn.write_message(('test'))except:print 'Tried to send websocket msg but failed.'
self.write('Hello!')tornado_handlers = [('/hello', HelloHandler),]tornado_app = tornado.web.Application(tornado_handlers)http_server = tornado.httpserver.HTTPServer(tornado_app, no_keep_alive=True)host = 'localhost'port = 8040
http_server.bind(port, address=host, reuse_port=True) # TODO: reuse_port might require a modern kernel?tornado_process_count = 1http_server.start(tornado_process_count)
print 'Serving HTTP on %s:%d' % (host, port)
class WebsocketClient(object):def __init__(self, conn_pause=1):self.conn_pause = conn_pauseself.client = tornado.httpclient.HTTPRequest('ws://localhost:8030/websocket')@tornado.gen.coroutinedef connect_to_server(self):self.conn = Nonewhile self.conn is None:try:t1 = time.time()self.conn = yield tornado.websocket.websocket_connect(self.client, on_message_callback=self.on_message)elapsed = (time.time() - t1) * 1000print 'Connection elapsed: %.1f msec' % elapsedexcept socket.error as serr: # https://goo.gl/xchRcAif serr.errno == errno.ECONNREFUSED:print 'Websocket connection refused. Sleeping...'yield tornado.gen.sleep(self.conn_pause) # Returns immediately?else:raisedef on_message(self, msg):if msg is None:# Reconnect.self.connect_to_server()else:print 'Received: %s' % `msg`ioloop = tornado.ioloop.IOLoop.current()ws_client = WebsocketClient()ioloop.spawn_callback(ws_client.connect_to_server) # https://goo.gl/kFcF7Xioloop.start()
import tornado.httpserverimport tornado.ioloopimport tornado.webimport tornado.websocket
class EchoWebSocket(tornado.websocket.WebSocketHandler):def open(self):#self.set_nodelay(True) # "Usually doesn't help". https://goo.gl/NzveYq
print("WebSocket opened")def on_message(self, message):
#print 'Recv: ' + message
self.write_message(u"You said: " + message)def on_close(self):print("WebSocket closed")tornado_handlers = [('/websocket', EchoWebSocket),]
tornado_app = tornado.web.Application(tornado_handlers)http_server = tornado.httpserver.HTTPServer(tornado_app, no_keep_alive=True)
http_server.bind(8030, address='localhost', reuse_port=True) # TODO: reuse_port might require a modern kernel?
Here is an example WebSocket handler that echos back all received messages back to the client:
class EchoWebSocket(tornado.websocket.WebSocketHandler): def open(self):
print("WebSocket opened") def on_message(self, message):
self.write_message(u"You said: " + message) def on_close(self): print("WebSocket closed")
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-tornado/cd796c71-3cac-4ed4-affc-ec0c8bd29525n%40googlegroups.com.