Discuss / Python / add_route对于handler的判断

add_route对于handler的判断

Topic source

aiohttp文档中关于add_route的一段描述

Pay attention please: handler is converted to coroutine internally when it is a regular function.

所以源码中关于fn的判断是不是可以不用加

def add_route(app, fn):
    method = getattr(fn, '__method__', None)
    path = getattr(fn, '__route__', None)
    if path is None or method is None:
        raise ValueError('@get or @post not defined in %s.' % str(fn))
    if not asyncio.iscoroutinefunction(fn) and not inspect.isgeneratorfunction(fn):
        fn = asyncio.coroutine(fn)
    logging.info('add route %s %s => %s(%s)' % (method, path, fn.__name__, ', '.join(inspect.signature(fn).parameters.keys())))
    app.router.add_route(method, path, RequestHandler(app, fn))

写了篇文章总结了对这章代码的理解,有困惑的同学可以一起交流


  • 1

Reply