[Python-ideas] Maybe python should support arrow syntax for easier use async call ?

张沈鹏 375956667 at qq.com
Tue Sep 29 08:31:38 CEST 2015


# The example works with tornado dev verison & python3.5


import tornado
from tornado.httpclient import AsyncHTTPClient
from tornado.concurrent import Future 
from tornado.gen import convert_yielded
from functools import wraps
    
Future.__or__ = Future.add_done_callback

def future(func):
    @wraps(func)
    def _(*args, **kwds):
        return convert_yielded(func(*args, **kwds))
    return _



##############

@future
async def ping(url):
    httpclient = AsyncHTTPClient()
    r = await httpclient.fetch(url)
    return r.body.decode('utf-8')


ping("http://baidu.com") | (
    lambda r:print(r.result())
)

"""

Maybe python should support arrow syntax for easier use async call ?

Now lambda only can write one line and must have parentheses ...

FOR EXAMPLE

ping("http://baidu.com") | r ->
    print(r.result())
    print("something else")


I saw some discuss in https://wiki.python.org/moin/AlternateLambdaSyntax
"""


tornado.ioloop.IOLoop.instance().start()


More information about the Python-ideas mailing list