如何使用 init 参数为处理程序设置龙卷风路由?
How to set the tornado routing for handler with init parameters?
我正在构建一个龙卷风网络服务器。其中一个处理程序需要初始化参数。如何设置路由列表以便我可以传递参数值?
class ActionHandler():
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
async def post(self, action):
x = self.p1
y = self.p2
# ....
app = tornado.web.Application([
('/action', ActionHandler) # How to pass init parameters?
documentation notes that you should not override the __init__
method of a RequestHandler
subclass. If you want to pass initial arguments to the handler, use the initialze
method.
class ActionHandler():
def initialze(self, p1, p2):
self.p1 = p1
self.p2 = p2
# then pass these arguments in a dict
# when you register the route
app = tornado.web.Application([
('/action', ActionHandler, {'p1': 'Hello', 'p2': 'World'}),
])
我正在构建一个龙卷风网络服务器。其中一个处理程序需要初始化参数。如何设置路由列表以便我可以传递参数值?
class ActionHandler():
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
async def post(self, action):
x = self.p1
y = self.p2
# ....
app = tornado.web.Application([
('/action', ActionHandler) # How to pass init parameters?
documentation notes that you should not override the __init__
method of a RequestHandler
subclass. If you want to pass initial arguments to the handler, use the initialze
method.
class ActionHandler():
def initialze(self, p1, p2):
self.p1 = p1
self.p2 = p2
# then pass these arguments in a dict
# when you register the route
app = tornado.web.Application([
('/action', ActionHandler, {'p1': 'Hello', 'p2': 'World'}),
])