python 3.x - What is mean yield None (tornado.gen.moment) -


i need async subprocess lock in web application. writes next code:

r = redis.redis('localhost') pipe = r.pipeline() is_locked = false while not is_locked:     try:         pipe.watch(lock_name)         current_locked = int(pipe.get(lock_name))          if current_locked == 0:             pipe.multi()             pipe.incr(lock_name)             pipe.execute()             is_locked = true         else:             yield none     except redis.watcherror:         yield none return true 

in documentation writen tornado.gen.moment (yield none since version 4.5) special object may yielded allow ioloop run 1 iteration. how works? next iteration other feature object (from other request) or not? correct yield none usage?

the gen.moment resolved future object added ioloop callback. allows run 1 iteration of ioloop.

the yield none converted gen.moment using convert_yielded in coroutine's gen.runner.

the ioloop (basically while true) each iteration things like:

  1. run callbacks scheduled ioloop's add_callback or add_callback_from_signal

  2. run callbacks scheduled ioloop's add_timeout

  3. poll fd events (e.g. wait file descirptor ready write or read). of course not block ioloop poll has timeout.

  4. run handler of ready fds

so getting point yield gen.moment allow things above 1 time (one iteration).

as example let's schedule async task - httpclient fetch requires running ioloop finished. on other hand there blocking function (time.sleep).

import time tornado import gen tornado.ioloop import ioloop tornado.httpclient import asynchttpclient   @gen.coroutine def fetch_task():     client = asynchttpclient()     yield client.fetch('http://google.com')     print('fetch_task finished')   @gen.coroutine def blocking():     start_time = time.time()     counter = 1      while true:         time.sleep(5)         print('blocking %f' % (time.time() - start_time))         yield gen.moment         print('gen.moment counter %d' % counter)         counter += 1   @gen.coroutine def main():     fetch_task()     yield blocking()   ioloop.instance().run_sync(main) 

observation:

  • without yield gen.moment, fetch_task won't finished
  • increase/decrease value of time.sleep not affect number of required iteration of ioloop fetch_task completed. means asynchttpclient.fetch n + 1 (gen.moments + task schedule) interactions ioloop (handling callbacks, polling fd, handling events).
  • gen.moment not mean, other tasks finished, rather opportunity 1 step closer completeness.

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -