using gen.task with Tornado for a simple function

2019-01-22 15:07发布

Just trying to use the async functions of Tornado - I want to invoke a method from my handler but it keeps telling me that it "got an unexpected keyword argument 'callback'".

class MyHandler(tornado.web.RequestHandler):

    @asynchronous
    @gen.engine
    def get(self):
        response = yield gen.Task(self.dosomething, 'argument')
        self.write(response)
        self.finish()

    def dosomething(self, myargument):
        pass

1条回答
Summer. ? 凉城
2楼-- · 2019-01-22 15:51

Non-blocking function requires callback, where it pass result.

class MyHandler(tornado.web.RequestHandler):

    @asynchronous
    @gen.engine
    def get(self):
        response = yield gen.Task(self.dosomething, 'argument')
        self.write(response)
        self.finish()

    def dosomething(self, myargument, callback):
        return callback(myargument)
查看更多
登录 后发表回答