[英]Python/Tornado - call to classmethod
Given this simple code where I declare a class and a functions inside it.鉴于这个简单的代码,我在其中声明了一个类和一个函数。 In the main I try to call the function but the call is not made.在 main 中,我尝试调用该函数,但未进行调用。 I don't get any error but if I put a print in order to know if the call has happened nothing occurs.我没有收到任何错误,但是如果我打印出来以了解呼叫是否发生,则什么也不会发生。
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))
What am I doing wrong?我究竟做错了什么? The sayHi function show its print but there's no reaction with createBalance. sayHi 函数显示其打印,但 createBalance 没有反应。
Decorate createBalance
with gen.coroutine
to run it on ioloop.使用gen.coroutine
装饰createBalance
以在gen.coroutine
上运行它。 To wait until balance is created invoke it like yield Balance.createBalance()
in RequestHandler
要等到创建余额,请像RequestHandler
yield Balance.createBalance()
一样调用它
models楷模
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"
main主要的
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))
Note: As I mentioned in snippet's comment, in motorengine the Document.save
returns Future
and probably you want to yield
it as well, to wait until it has finished.注:正如我在片断的评论中提到,在motorengine的Document.save
返回Future
,也许你想yield
一样好,要等到它完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.