fork download
  1. import asyncio
  2.  
  3. from aiohttp import web
  4.  
  5. async def root_handler(request):
  6. return web.Response(text="root")
  7.  
  8.  
  9. async def long_polling_1(request):
  10. q = request.app['q']
  11. try:
  12. data = await asyncio.wait_for(q.get(), 15.0)
  13. text = f"recieved data: {data}"
  14. except asyncio.TimeoutError:
  15. text = f"timeout! no data"
  16. return web.Response(text=text)
  17.  
  18. async def handler_msg(request):
  19. q = request.app['q']
  20. msg = request.match_info['msg']
  21. q.put_nowait(f" === message: {msg} ======")
  22. return web.Response(text=f"message {msg} sent to queue")
  23.  
  24. if __name__ == '__main__':
  25. app = web.Application()
  26. app.add_routes([
  27. web.get('/', root_handler),
  28. web.get('/long_polling/', long_polling_1),
  29. web.get('/msg/{msg}/', handler_msg),
  30. ])
  31. app['q'] = asyncio.Queue()
  32. web.run_app(app, port=8040)
  33.  
Runtime error #stdin #stdout #stderr 0.23s 25284KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 3, in <module>
    from aiohttp import web
ModuleNotFoundError: No module named 'aiohttp'