mrjoes / sockjs-tornado

WebSocket emulation - Python server
MIT License
849 stars 162 forks source link

error #103

Open wenxiaodong opened 8 years ago

wenxiaodong commented 8 years ago

RuntimeError: Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start_processes()

Can't use multiple processes?

thekeenant commented 8 years ago

https://groups.google.com/forum/#!topic/python-tornado/JNtWjFcVb7s

app = tornado.web.Application() 
http_server = tornado.httpserver.HTTPServer(app) 
http_server.bind(port) 
http_server.start(4) 
chat_routes = sockjs.tornado.SockJSRouter(connection.handler, '/im') 
app.add_handlers(".*", 
  [(r"/im/search", handlers.search_handler)] + 
  [(r"/favicon.ico", handlers.favicon_handler)] + 
  [(r"/test", handlers.TestHandler)] + 
  chat_routes.urls) 

Works, but take note of:

This won't work: SockJS protocol requires sticky sessions - client should come back to same instance of the SockJS server. With pre-forking requests will be distributed in round-robin fashion.

Advised approach:

  1. Spawn multiple sockjs-tornado processes, each listening on separate port
  2. Use haproxy to load-balance requests. Sample config: https://github.com/sockjs/sockjs-node/blob/master/examples/haproxy.cfg

Serge.