我的python版本是3.4,我的龙卷风版本是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)。这是怎么发生的? 以及如何解决它?
答案 0 :(得分:2)
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()