0

我的处理程序中有一些异步代码,我需要将此部分作为外部方法并从不同的处理程序运行,但每个方法都有异步代码。你能帮助我吗?

简单的例子:

#!/usr/bin/python

import asyncmongo
import tornado.web    

class Handler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        self.db = asyncmongo.Client(pool_id='mypool', host='localhost', 
                                          port=27107, dbname='mydb')

        self.db.first_names.find_one({'user_id': 1 }, 
                                     callback=self._on_response)


    def _on_response(self, response, error):
        first_name = response['first_name']
        data = {
              'first_name': first_name
        }

        # use processor
        first_name_html = self.generate_html("firstname.html").generate(**data)
        last_name_html = foo()
        self.write(first_name_html + last_name_html)
        self.finish()

    # this part of code is wrong!
    # I have question about it
    @tornado.web.asynchronous
    def foo(self):
        self.db.last_names.find_one({'user_id': 1 }, 
                                 callback=self._on_response_two)

    def _on_response_two(self, response, error):
        last_name = response['last_name']
        data = {
              'last_name': last_name
        }

        # use processor
        last_name_html = self.generate_html("lastname.html").generate(**data)
        return last_name_html
4

2 回答 2

0

我会建议

#!/usr/bin/python

import asyncmongo
import tornado.web    

class Handler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        self.db = asyncmongo.Client(pool_id='mypool', host='localhost', 
                                          port=27107, dbname='mydb')

        self.db.first_names.find_one({'user_id': 1 }, 
                                     callback=self._on_response)


    def _on_response(self, response, error):
        first_name = response['first_name']
        data = {
              'first_name': first_name
        }

        # use processor
        self.first_name_html = self.generate_html("firstname.html").generate(**data)
        foo()

    def foo(self):
        self.db.last_names.find_one({'user_id': 1 }, 
                                 callback=self._on_response_two)

    def _on_response_two(self, response, error):
        last_name = response['last_name']
        data = {
              'last_name': last_name
        }

        # use processor
        last_name_html = self.generate_html("lastname.html").generate(**data)
        self.write(self.first_name_html + last_name_html)
        self.finish()
于 2012-05-15T17:51:32.977 回答
0

为了清楚起见,我会选择 gen.engine 版本。

#!/usr/bin/python

import asyncmongo
import tornado.web
from tornado import gen

class Handler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @gen.engine
    def get(self):
        self.db = asyncmongo.Client(pool_id='mypool', host='localhost',
                                          port=27107, dbname='mydb')

        response, error = yield gen.Task(self.db.first_names.find_one, {'user_id': 1 })

        first_name = response['first_name']
        data = {
              'first_name': first_name
        }

        # use processor
        self.first_name_html = self.generate_html("firstname.html").generate(**data)

        self.foo()

    @gen.engine
    def foo(self):
        response, error = yield gen.Task(self.db.last_names.find_one, {'user_id': 1 })

        last_name = response['last_name']
        data = {
              'last_name': last_name
        }

        # use processor
        last_name_html = self.generate_html("lastname.html").generate(**data)
        self.write(self.first_name_html + last_name_html)
        self.finish()
于 2012-05-15T19:43:51.117 回答