python - Converting web.asynchronous code to gen.coroutine in tornado -


i want convert current tornado app using @web.asynchronous @gen.coroutine. asynchronous callback called when particular variable change happens on ioloop iteration. current example in tornado docs solves i/o problem in case variable interested in. want coroutine wake on variable change. app looks code shown below.

note: can use python2.

# transaction db change can happen # process class transaction:     def __init__(self):         self.status = 'incomplete'         self.callback = none  # in this, checking status of db # before responding 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 request")         self.finish() # myapp monitors list of transactions , adds callback # 'transaction.callback' when transactions status changes  # complete state.  class myapp(application):     def __init__(self, settings):         self.settings = settings         self._url_patterns = self._get_url_patterns()         self.txn_list = [] # list of 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         transaction in self.txn_list:             transaction.status = is_transaction_complete()             if transaction.status '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' url_patterns     def _get_url_patterns(self):         urls import url_patterns         modified_url_patterns = []         url in url_patterns:             modified_url_patterns.append( url + ({ 'app_reference': self },))         return modified_url_patterns 

if understand right write using gen.coroutine should modified

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

my issue not sure how wake routine when status changes , cannot use loop block tornado thread. want notify ioloop iteration.

def check_status():     transaction in txn_list:         if transaction.status 'complete':             notify_coroutine 

sounds job new tornado.locks! released last week tornado 4.2:

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

use event this:

from tornado import locks, gen  event = locks.event()  @gen.coroutine def waiter():     print("waiting event")     yield event.wait()     print("done")  @gen.coroutine def setter():     print("about set event")     event.set() 

more info on event interface:

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


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -