Python/Tornado - 调用类方法
Python/Tornado - call to classmethod
鉴于这个简单的代码,我在其中声明了一个 class 和一个函数。主要是我尝试调用该函数,但未进行调用。我没有收到任何错误,但如果我打印以了解调用是否发生,则什么也没有发生。
型号
class Balance(Document):
gross_balance = FloatField(required=True, min_value=0, default=0)
@classmethod
def createBalance(cls, gross_balance):
result = yield Balance.objects.create(gross_balance = gross_balance)
result.save()
@classmethod
def sayHi(cls):
print "Hi there"
主要
from models import Balance
class CreateBalanceHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def post(self):
gross_balance = self.get_argument('gross_balance')
Balance.createBalance(gross_balance)
Balance.sayHi()
self.redirect('/query/{}'.format(gross_balance))
我做错了什么? sayHi 函数显示其打印,但对 createBalance 没有反应。
在 ioloop 上用 gen.coroutine
装饰 createBalance
到 运行。等到余额被创建调用它像 yield Balance.createBalance()
in RequestHandler
型号
class Balance(Document):
gross_balance = FloatField(required=True, min_value=0, default=0)
# classmethod must be the most outter decorator (as well as staticmethod)
@classmethod
@tornado.gen.coroutine
def createBalance(cls, gross_balance):
result = yield Balance.objects.create(gross_balance = gross_balance)
# AFAIR save returns future and also should be yielded
# yield. result.save()
result.save()
@classmethod
def sayHi(cls):
print "Hi there"
主要
from models import Balance
class CreateBalanceHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def post(self):
gross_balance = self.get_argument('gross_balance')
yield Balance.createBalance(gross_balance)
Balance.sayHi()
self.redirect('/query/{}'.format(gross_balance))
注意:正如我在代码片段的评论中提到的,在 motorengine 中 Document.save
returns Future
并且您可能也想 yield
它,等到它已完成。
鉴于这个简单的代码,我在其中声明了一个 class 和一个函数。主要是我尝试调用该函数,但未进行调用。我没有收到任何错误,但如果我打印以了解调用是否发生,则什么也没有发生。
型号
class Balance(Document):
gross_balance = FloatField(required=True, min_value=0, default=0)
@classmethod
def createBalance(cls, gross_balance):
result = yield Balance.objects.create(gross_balance = gross_balance)
result.save()
@classmethod
def sayHi(cls):
print "Hi there"
主要
from models import Balance
class CreateBalanceHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def post(self):
gross_balance = self.get_argument('gross_balance')
Balance.createBalance(gross_balance)
Balance.sayHi()
self.redirect('/query/{}'.format(gross_balance))
我做错了什么? sayHi 函数显示其打印,但对 createBalance 没有反应。
在 ioloop 上用 gen.coroutine
装饰 createBalance
到 运行。等到余额被创建调用它像 yield Balance.createBalance()
in RequestHandler
型号
class Balance(Document):
gross_balance = FloatField(required=True, min_value=0, default=0)
# classmethod must be the most outter decorator (as well as staticmethod)
@classmethod
@tornado.gen.coroutine
def createBalance(cls, gross_balance):
result = yield Balance.objects.create(gross_balance = gross_balance)
# AFAIR save returns future and also should be yielded
# yield. result.save()
result.save()
@classmethod
def sayHi(cls):
print "Hi there"
主要
from models import Balance
class CreateBalanceHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def post(self):
gross_balance = self.get_argument('gross_balance')
yield Balance.createBalance(gross_balance)
Balance.sayHi()
self.redirect('/query/{}'.format(gross_balance))
注意:正如我在代码片段的评论中提到的,在 motorengine 中 Document.save
returns Future
并且您可能也想 yield
它,等到它已完成。