使用龙卷风时如何将多个端口与多处理绑定
how to bind multiple port with multiprocessing when using tornado
我的python版本是3.4,我的tornado版本是4.3。
我有2台服务器,他们必须在运行时共享一些数据,我的代码是这样的,
from tornado.web import gen, asynchronous, RequestHandler, Application
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
class HelloHandler(RequestHandler):
@asynchronous
@gen.engine
def get(self):
self.write('hello')
self.finish()
class MainHandler(RequestHandler):
@asynchronous
@gen.engine
def get(self):
self.write('main')
self.finish()
helloApp = Application([
(r'/hello', HelloHandler),
])
mainApp = Application([
(r'/main', MainHandler),
])
if __name__ == "__main__":
hello_server = HTTPServer(helloApp)
hello_server.bind(8881)
hello_server.start()
# hello_server.start(0)
main_server = HTTPServer(mainApp)
main_server.bind(8882)
main_server.start()
# main_server.start(0)
IOLoop.current().start()
它有效,但是当我尝试使用 server.start(0) 支持多个进程时,我收到一个错误:'OSError: [Errno 98] 地址已在使用中',我已经使用了不同的端口(8881、8882)。这是怎么发生的?
以及如何修复它?
start(n)
仅适用于单个服务器。要使用多个,必须分别使用 bind_sockets
、fork_processes
和 add_sockets
(示例改编自 http://www.tornadoweb.org/en/stable/tcpserver.html):
from tornado.netutil import bind_sockets
hello_sockets = bind_sockets(8881)
main_sockets = bind_sockets(8882)
tornado.process.fork_processes(0)
hello_server = HTTPServer(helloApp)
hello_server.add_sockets(hello_sockets)
main_server = HTTPServer(mainApp)
main_server.add_sockets(main_sockets)
IOLoop.current().start()
我的python版本是3.4,我的tornado版本是4.3。 我有2台服务器,他们必须在运行时共享一些数据,我的代码是这样的,
from tornado.web import gen, asynchronous, RequestHandler, Application
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
class HelloHandler(RequestHandler):
@asynchronous
@gen.engine
def get(self):
self.write('hello')
self.finish()
class MainHandler(RequestHandler):
@asynchronous
@gen.engine
def get(self):
self.write('main')
self.finish()
helloApp = Application([
(r'/hello', HelloHandler),
])
mainApp = Application([
(r'/main', MainHandler),
])
if __name__ == "__main__":
hello_server = HTTPServer(helloApp)
hello_server.bind(8881)
hello_server.start()
# hello_server.start(0)
main_server = HTTPServer(mainApp)
main_server.bind(8882)
main_server.start()
# main_server.start(0)
IOLoop.current().start()
它有效,但是当我尝试使用 server.start(0) 支持多个进程时,我收到一个错误:'OSError: [Errno 98] 地址已在使用中',我已经使用了不同的端口(8881、8882)。这是怎么发生的? 以及如何修复它?
start(n)
仅适用于单个服务器。要使用多个,必须分别使用 bind_sockets
、fork_processes
和 add_sockets
(示例改编自 http://www.tornadoweb.org/en/stable/tcpserver.html):
from tornado.netutil import bind_sockets
hello_sockets = bind_sockets(8881)
main_sockets = bind_sockets(8882)
tornado.process.fork_processes(0)
hello_server = HTTPServer(helloApp)
hello_server.add_sockets(hello_sockets)
main_server = HTTPServer(mainApp)
main_server.add_sockets(main_sockets)
IOLoop.current().start()