関数 "runquery"は、プログラムのさまざまな部分から呼び出されます。 この場合、クエリ文では「準備」を使用しません。 私は "prepared statement already exists"に関する他の質問をすべて読んで、 "DEALLOCATE ALL"も試みました。しかし、それは反対のエラーになります。以下のエラーは、準備されたステートメントがすでに存在すると不平を言いますが、DEALLOCATE ALL結果が存在しないという苦情をもたらします。asyncpg.exceptions.DuplicatePreparedStatementError:prepared statement "stmt_5"は既に存在します
これは、asyncpgを使用してこのタイプのプログラムを実行する私の最初の試みです。私はpsycopg2を使っている間これを以前持っていませんでした。私はpsycopg2に戻るべきですか?
Could not complete query "select uuid from wos_2017_1.combined_name where combined_name = 'xxxxxxx';"
Traceback (most recent call last):
File "update2017.py", line 1206, in runquery
result = await con.fetch(query)
File "/usr/lib/python3/dist-packages/asyncpg/connection.py", line 268, in fetch
stmt = await self._get_statement(query, timeout)
File "/usr/lib/python3/dist-packages/asyncpg/connection.py", line 212, in _get_statement
state = await protocol.prepare(None, query, timeout)
File "asyncpg/protocol/protocol.pyx", line 140, in prepare (asyncpg/protocol/protocol.c:55210)
File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
raise self._exception
asyncpg.exceptions.DuplicatePreparedStatementError: prepared statement "stmt_7" already exists
$ grep -c INSERT /tmp/y.log
1006
$ grep -c SELECT /tmp/y.log
1364
$ grep -ci UPDATE /tmp/y.log
1044
$ grep -ci delete /tmp/y.log
2548
import asyncio,asyncpg
async def make_pool():
"""Create asyncpg connection pool to database"""
pool = await asyncpg.create_pool(database='wos',
host = 'localhost',
user = 'xx',
password='xxxxxx',
port=5432,
min_size=10,
max_size=50)
return pool
async def get_con(pool):
con = await pool.acquire()
return con
async def runquery(query):
con = await get_con(pool)
try:
if query.startswith('delete from') or query.startswith('insert'):
result = await con.execute(query)
else:
result = await con.fetch(query)
except:
print('Could not complete query "{}"'.format(query))
print(traceback.format_exc())
result = None
exit(1)
finally:
await pool.release(con)
return result, success