首页 > 解决方案 > 什么时候输入 await ?在 python 中使用 aiohttp

问题描述

我使用 aiohttp 。我需要在应用程序的哪些地方输入 await ?据我所知,有以下情况:

  1. 当代码是关于会话时

        async def get(self):
            session = await get_session(self.request)
    
    
  2. 当代码与数据库有关时

   def __init__(self, db, **kwargs):
           self.collection = db[MESSAGE_COLLECTION]

   async def save(self, user, msg, **kw):
           result = await self.collection.insert({'user': user, 'msg': msg, 'time': datetime.now()})
  1. 当代码与套接字有关时
   async def get(self):
           ws = web.WebSocketResponse()
           await ws.prepare(self.request)

标签: pythonaiohttp

解决方案


推荐阅读