V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bestehen
V2EX  ›  Python

Python aiohttp 问题

  •  
  •   bestehen · 2018-06-17 19:34:32 +08:00 · 1666 次点击
    这是一个创建于 2138 天前的主题,其中的信息可能已经有所发展或是发生改变。

    import asyncio

    from aiohttp import web

    async def index(request): await asyncio.sleep(0.5) return web.Response(body=b'

    Index

    ')

    async def hello(request): await asyncio.sleep(0.5) text = '

    hello, %s!

    ' % request.match_info['name'] return web.Response(body=text.encode('utf-8'))

    async def init(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', index) app.router.add_route('GET', '/hello/{name}', hello) srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000) print('Server started at http://127.0.0.1:8000...') return srv

    loop = asyncio.get_event_loop() loop.run_until_complete(init(loop)) loop.run_forever()

    这里的我感觉 只执行了 init 啊 hello 和 index 函数没有执行把

    2 条回复    2018-06-18 01:23:52 +08:00
    matsuz
        1
    matsuz  
       2018-06-17 19:55:32 +08:00   ❤️ 1
    init 是 web 服务器的启动器,程序启动时就执行了; hello 和 index 是被注册到 URL 路由表中,它们是接到相应的 HTTP 请求后才执行。

    另外,你的代码排版很差
    bestehen
        2
    bestehen  
    OP
       2018-06-18 01:23:52 +08:00
    你是怎么看出来的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1909 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:24 · PVG 00:24 · LAX 09:24 · JFK 12:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.