无法使用异步psycopg3连接postgres数据库:
import asyncio
import psycopg
async def main():
async with await psycopg.AsyncConnection.connect('postgresql://xxxxxxxxxxx') as con:
async with con.cursor() as cur:
print(await cur.execute('select 1 a').fetchall())
if __name__ == '__main__':
asyncio.run(main())我得到了错误:
psycopg.InterfaceError: Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'在Windows 10、Python3.9、3.0.8上
发布于 2022-02-23 07:08:44
以下是工作解决方案
import asyncio
import sys
import psycopg
async def main():
async with await psycopg.AsyncConnection.connect('postgresql://xxxxxxxxxxx') as con:
async with con.cursor() as cur:
print(await (await cur.execute('select 1 a')).fetchall())
if __name__ == '__main__':
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())https://stackoverflow.com/questions/71219607
复制相似问题