在龙卷风中将web.asynchronous代码转换为gen.coroutine

时间:2015-06-02 20:54:33

标签: python python-2.7 tornado

我想将当前的龙卷风应用程序从使用@ web.asynchronous转换为@ gen.coroutine。当IOLoop迭代发生特定变量更改时,将调用我的异步回调。 Tornado文档中的当前示例解决了I / O问题,但在我的情况下,它是我感兴趣的变量。我希望协程唤醒变量。我的应用程序看起来像下面显示的代码。

注意:我只能使用Python2。

# A transaction is a DB change that can happen
# from another process
class Transaction:
    def __init__(self):
        self.status = 'INCOMPLETE'
        self.callback = None

# In this, I am checking the status of the DB
# before responding to the GET request
class MainHandler(web.RequestHandler):
    def initialize(self, app_reference):
        self.app_reference = app_reference

    @web.asynchronous
    def get(self):
        txn = Transaction()
        callback = functools.partial(self.do_something)
        txn.callback = callback

        self.app_reference.monitor_transaction(txn)

    def do_something(self):
        self.write("Finished GET request")
        self.finish()
# MyApp monitors a list of transactions and adds the callback
# 'transaction.callback' when transactions status changes to 
# COMPLETE state. 
class MyApp(Application):
    def __init__(self, settings):
        self.settings = settings
        self._url_patterns = self._get_url_patterns()
        self.txn_list = [] # list of all transactions being monitored
        Application.__init__(self, self._url_patterns, **self.settings)
        IOLoop.current().add_callback(self.check_status)

    def monitor_transaction(self, txn):
        self.txn_list.append(txn)

    def check_status(self):
        count = 0
        for transaction in self.txn_list:
            transaction.status = is_transaction_complete()
            if transaction.status is 'COMPLETE':
                IOLoop.current().add_callback(transaction.callback)
                self.txn_list.pop(count)
            count += 1
        if len(self.txn_list):
            IOloop.current().add_callback(self.check_status)


    # adds 'self' to url_patterns
    def _get_url_patterns(self):
        from urls import url_patterns
        modified_url_patterns = []
        for url in url_patterns:
            modified_url_patterns.append( url + ({ 'app_reference': self },))
        return modified_url_patterns

如果我理解使用gen.coroutine进行编写,则应将get修改为

@gen.coroutine
def get(self):
    txn = Transaction()
    response = yield wake_up_when_transaction_completes()
    # respond to GET here

我的问题是我不知道如何只在状态改变时才唤醒例程,我不能使用循环,因为它会阻止龙卷风线程。基本上我想从IOLoop迭代中通知。

def check_status():
    for transaction in txn_list:
        if transaction.status is 'COMPLETE':
            NOTIFY_COROUTINE

1 个答案:

答案 0 :(得分:2)

听起来像是新龙卷风的工作!上周发布了Tornado 4.2:

http://tornado.readthedocs.org/en/latest/releases/v4.2.0.html#new-modules-tornado-locks-and-tornado-queues

使用事件:

from tornado import locks, gen

event = locks.Event()

@gen.coroutine
def waiter():
    print("Waiting for event")
    yield event.wait()
    print("Done")

@gen.coroutine
def setter():
    print("About to set the event")
    event.set()

有关活动界面的更多信息:

http://tornado.readthedocs.org/en/latest/locks.html#tornado.locks.Event