would python3.3 make things change in tornado?

78 views
Skip to first unread message

momiji1

unread,
Nov 7, 2012, 10:05:16 PM11/7/12
to python-...@googlegroups.com
python3.3 released days ago. the biggest new feature is yield from


Would that make coding easier?

in previous version, if you want to make gen.task work at somewhere, you must modify all functions at the very beginning passing a callback. though most of the callback is meaningless.

in 3.3. you can simply use yield from to call the function, you don't have to make all functions become asynchronous ,isn't it?

Ben Darnell

unread,
Nov 8, 2012, 10:06:16 AM11/8/12
to Tornado Mailing List
Python 3.3 does offer some improvements for generator-based code, especially the fact that you can have "yield" and "return" in the same function, so you don't need to pass callbacks around explicitly.  You can use some parts of this with @gen.engine today:

  @gen.engine
  def foo(args, callback):
    # Entry points must still take callbacks and be decorated with @gen.engine
    yield gen.Task(...)

    # Undecorated generators can be called with yield from
    result = yield from async_helper(args)
    callback(result)

  def async_helper(args):
    # Functions called with "yield from" do not take a callback and are not decorated.
    # Objects yielded here will be passed through to the caller's gen.engine wrapper
    x = yield gen.Task(...)
    return result
 
Unfortunately, to take full advantage of this (and remove the dichotomy between entry points that need decorators and callbacks, and inner functions that must not have either) you need a generator scheduler that was designed with "yield from" in mind.  Such a scheduler would only work in Python 3.3+, so it will be a while before I'm willing to put something like that in Tornado itself.  However, there is talk of adding a "yield from"-based async framework to the standard library in python 3.4.  

-Ben
Reply all
Reply to author
Forward
0 new messages