Pypy fork which implements the 'await' keyword for Python for asyncronous programming.
Extension Author: Jonathan Slenders, Mobile Vikings
This extension aims to provide a more user-friendly syntax for asyncronous programming. It was inspired by c#, which also supports the async/await keywords.
The idea is that 'await', just like 'yield' creates a generator, only is this generator flagged as a special one, which creates an async scope, and that it allows a return statement inside.
Two methods were added to the interpreter: sys.setawaithandler and sys.setawaitresultwrapper, they both accept one paramter which should be a callable. The first should be used to register an I/O schedular. This function will receive the generator object when such an async method has been called; the result of this method will be returned instead. The second wrapper function is used to wrap the return value of the generator. It's impossible to return values from a generator, but the return value will be turned into an additional yield statement, first wrapped through this function, so that the I/O schedular can recognize this final yield as the result value.
>>>> import sys >>>> def wrapper(generator): .... print 'before' .... for i in generator: .... print i .... print 'after' .... # return AsyncTask() .... >>>> def result_wrapper(result): .... return '< %s >' % result .... >>>> def g(): .... a = await 1 .... b = await 2 .... return 'result' .... >>>> g() <generator object g at 0x0ae9272c> >>>> sys.setawaithandler(wrapper) >>>> sys.setawaitresultwrapper(result_wrapper) >>>> g() before 1 2 < result > after
This can be used for all kinds of asyncronous frameworks where you prefer to have at least some kind of syntax to indicate delegation to the asyncronous I/O loop.
For Twisted Matrix for instance, the following code:
@defer.inlineCallbacks def async_function(deferred_param): a = yield deferred_param b = yield some_call(a) yield defer.returnValue(b)
can now be written as:
def async_function(deferred_param): a = await deferred_param b = await some_call(a) return b
PyPy: Python in Python Implementation
Welcome to PyPy!
PyPy is both an implementation of the Python programming language, and an extensive compiler framework for dynamic language implementations. You can build self-contained Python implementations which execute independently from CPython.
The home page is:
The getting-started document will help guide you:
It will also point you to the rest of the documentation which is generated from files in the pypy/doc directory within the source repositories. Enjoy and send us feedback!
the pypy-dev team <firstname.lastname@example.org>