aiohttp.web + aiopg + sqlalchemy:random" cursor.execute()调用,而另一个协程已在等待传入数据"负载不足

时间:2016-11-23 17:05:14

标签: python postgresql sqlalchemy python-asyncio aiohttp

我有一个简单的aiohttp.web应用程序,它通过aiopg sqlalchemy集成执行SQL请求。它很简单:

import aiohttp.web
from aiopg.sa import create_engine

app = aiohttp.web.Application()


async def rows(request):
    async with request.app["db"].acquire() as db:
        return aiohttp.web.json_response(list(await db.execute("SELECT * FROM table")))

app.router.add_route("GET", "/rows", rows)


async def init(app):
    app["db"] = await create_engine(host="postgres", user="visio", password="visio", database="visio")

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    handler = app.make_handler()

    loop.run_until_complete(init(app))

    loop.run_until_complete(loop.create_server(handler, "0.0.0.0", 80))
    loop.run_forever()

当服务器负载达到100 rps时,此错误开始随机出现:

RuntimeError: cursor.execute() called while another coroutine is already waiting for incoming data
  File "aiohttp/server.py", line 261, in start
    yield from self.handle_request(message, payload)
  File "aiohttp/web.py", line 88, in handle_request
    resp = yield from handler(request)
  File "visio_longer/views/communicate/__init__.py", line 72, in legacy_communicate
    device = await query_device(db, access_token)
  File "visio_longer/views/communicate/__init__.py", line 31, in query_device
    (Device.access_token == access_token)
  File "aiopg/utils.py", line 72, in __await__
    resp = yield from self._coro
  File "aiopg/sa/connection.py", line 103, in _execute
    yield from cursor.execute(str(compiled), post_processed_params[0])
  File "aiopg/cursor.py", line 103, in execute
    waiter = self._conn._create_waiter('cursor.execute')
  File "aiopg/connection.py", line 186, in _create_waiter
    'already waiting for incoming data' % func_name)

随机时间随机查询,几天一次,有时这些错误会出现在4或2中。我的代码有问题吗? aiohttpaiopg版本来自pip

1 个答案:

答案 0 :(得分:0)

它认为您忘了获取光标

 async with conn.cursor() as cur:
     await cur.execute("SELECT 1")