Start 1 asyncio.run()
result = [[], [], []]
pool.is_closing() = False
pool.get_size() = 10
pool.get_idle_size() = 10
Start 2 asyncio.run()
Traceback (most recent call last):
File "c:\python310\lib\asyncio\base_events.py", line 753, in call_soon
self._check_closed()
File "c:\python310\lib\asyncio\base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\ProgProject\python\learn_asyncpg\111.py", line 7, in d
return await con.fetch("SELECT $1", c)
File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 690, in fetch
return await self._execute(
File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 1864, in _execute
result, _ = await self.__execute(
File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 1961, in __execute
result, stmt = await self._do_execute(
File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 2024, in _do_execute
result = await executor(stmt, None)
File "asyncpg\\protocol\\protocol.pyx", line 206, in bind_execute
File "asyncpg\\protocol\\protocol.pyx", line 192, in asyncpg.protocol.protocol.BaseProtocol.bind_execute
File "asyncpg\\protocol\\coreproto.pyx", line 1020, in asyncpg.protocol.protocol.CoreProtocol._bind_execute
File "asyncpg\\protocol\\coreproto.pyx", line 1008, in asyncpg.protocol.protocol.CoreProtocol._send_bind_message
File "asyncpg\\protocol\\protocol.pyx", line 967, in asyncpg.protocol.protocol.BaseProtocol._write
File "c:\python310\lib\asyncio\proactor_events.py", line 365, in write
self._loop_writing(data=bytes(data))
File "c:\python310\lib\asyncio\proactor_events.py", line 401, in _loop_writing
self._write_fut = self._loop._proactor.send(self._sock, data)
AttributeError: 'NoneType' object has no attribute 'send'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\ProgProject\python\learn_asyncpg\111.py", line 25, in
asyncio.run(main(["2", "3", "4"], pool))
File "c:\python310\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "c:\python310\lib\asyncio\base_events.py", line 649, in run_until_complete
return future.result()
File "d:\ProgProject\python\learn_asyncpg\111.py", line 13, in main
result = await asyncio.gather(*(d(i, pool) for i in input))
File "d:\ProgProject\python\learn_asyncpg\111.py", line 6, in d
async with pool.acquire() as con:
File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\pool.py", line 228, in release
raise ex
File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\pool.py", line 218, in release
await self._con.reset(timeout=budget)
File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 1562, in reset
await self.execute(reset_query)
File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 349, in execute
result = await self._protocol.query(query, timeout)
File "asyncpg\\protocol\\protocol.pyx", line 360, in query
File "asyncpg\\protocol\\protocol.pyx", line 745, in asyncpg.protocol.protocol.BaseProtocol._check_state
asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress
Почему я получаю сообщение об ошибке «Выполняется другая операция» при повторном использовании незакрытого пула с незакрытыми соединениями?
Какая операция имеется в виду?< /п>
Как я могу повторно использовать существующий пул соединений asyncpg? Среда выполнения: Windows 10 22 H2, Python 3.10.13 requirements.txt< /p> [code]async-timeout==5.0.1 asyncpg==0.30.0 [/code] Этот код работает: [code]import asyncio import random
async def main(input: list[any]): result = await asyncio.gather(*(d(i) for i in input)) return result
if __name__ == "__main__": print(f"Start 1 asyncio.run()") result = asyncio.run(main([1, 2, 3])) print(f"{result = }") print(f"Start 2 asyncio.run()") result = asyncio.run(main(result)) print(f"{result = }") [/code] Результат: [code]Start 1 asyncio.run() result = [1, 4, 9] Start 2 asyncio.run() result = [1, 16, 81] [/code] Я добавляю asyncpg.pool: [code]import asyncio import asyncpg
async def d(c: str, pool: asyncpg.Pool): async with pool.acquire() as con: return await con.fetch("SELECT $1", c)
async def main(input: list[str], pool: asyncpg.Pool = None): if not pool: pool = await asyncpg.create_pool(dsn="Connection arguments") result = await asyncio.gather(*(d(i, pool) for i in input)) print(f"{result = }") return pool
if __name__ == "__main__": print(f"Start 1 asyncio.run()") pool = asyncio.run(main(["1", "2", "3"])) print(f"{pool.is_closing() = }") print(f"{pool.get_size() = }") print(f"{pool.get_idle_size() = }") print(f"Start 2 asyncio.run()") asyncio.run(main(["2", "3", "4"], pool)) [/code] и получите ошибку: [code]Start 1 asyncio.run() result = [[], [], []] pool.is_closing() = False pool.get_size() = 10 pool.get_idle_size() = 10 Start 2 asyncio.run() Traceback (most recent call last): File "c:\python310\lib\asyncio\base_events.py", line 753, in call_soon self._check_closed() File "c:\python310\lib\asyncio\base_events.py", line 515, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "d:\ProgProject\python\learn_asyncpg\111.py", line 7, in d return await con.fetch("SELECT $1", c) File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 690, in fetch return await self._execute( File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 1864, in _execute result, _ = await self.__execute( File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 1961, in __execute result, stmt = await self._do_execute( File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 2024, in _do_execute result = await executor(stmt, None) File "asyncpg\\protocol\\protocol.pyx", line 206, in bind_execute File "asyncpg\\protocol\\protocol.pyx", line 192, in asyncpg.protocol.protocol.BaseProtocol.bind_execute File "asyncpg\\protocol\\coreproto.pyx", line 1020, in asyncpg.protocol.protocol.CoreProtocol._bind_execute File "asyncpg\\protocol\\coreproto.pyx", line 1008, in asyncpg.protocol.protocol.CoreProtocol._send_bind_message File "asyncpg\\protocol\\protocol.pyx", line 967, in asyncpg.protocol.protocol.BaseProtocol._write File "c:\python310\lib\asyncio\proactor_events.py", line 365, in write self._loop_writing(data=bytes(data)) File "c:\python310\lib\asyncio\proactor_events.py", line 401, in _loop_writing self._write_fut = self._loop._proactor.send(self._sock, data) AttributeError: 'NoneType' object has no attribute 'send'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "d:\ProgProject\python\learn_asyncpg\111.py", line 25, in asyncio.run(main(["2", "3", "4"], pool)) File "c:\python310\lib\asyncio\runners.py", line 44, in run return loop.run_until_complete(main) File "c:\python310\lib\asyncio\base_events.py", line 649, in run_until_complete return future.result() File "d:\ProgProject\python\learn_asyncpg\111.py", line 13, in main result = await asyncio.gather(*(d(i, pool) for i in input)) File "d:\ProgProject\python\learn_asyncpg\111.py", line 6, in d async with pool.acquire() as con: File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\pool.py", line 228, in release raise ex File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\pool.py", line 218, in release await self._con.reset(timeout=budget) File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 1562, in reset await self.execute(reset_query) File "d:\ProgProject\python\learn_asyncpg\venv\lib\site-packages\asyncpg\connection.py", line 349, in execute result = await self._protocol.query(query, timeout) File "asyncpg\\protocol\\protocol.pyx", line 360, in query File "asyncpg\\protocol\\protocol.pyx", line 745, in asyncpg.protocol.protocol.BaseProtocol._check_state asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress [/code] Почему я получаю сообщение об ошибке «Выполняется другая операция» при повторном использовании незакрытого пула с незакрытыми соединениями? Какая операция имеется в виду?< /п>
Я разрабатываю сервер fastapi, используя sqlalchemy и asyncpg для работы с базой данных postgres. Для каждого запроса создается новая сессия (через внедрение зависимостей fastapi, как в документации). Я использовал sqlite+aiosqlite до...
Мне интересно, когда поделиться подключением к асинкпг по сравнению с созданием нового. Предположим, у меня есть:
async def monitor(data_stream):
# ...
while True:
# ...
x, y = await data_stream.get_data()
await conn.execute( INSERT INTO table (x,...
Я пытаюсь отправить бассейн в строку, а затем вернуть его позже, чтобы закрыть/присоединиться к бассейну. Вот пример того, как дублировать то, что я делаю: