首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法连接异步的Psycopg3

无法连接异步的Psycopg3
EN

Stack Overflow用户
提问于 2022-02-22 10:28:21
回答 1查看 663关注 0票数 0

无法使用异步psycopg3连接postgres数据库:

代码语言:javascript
复制
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())

我得到了错误:

代码语言:javascript
复制
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上

EN

回答 1

Stack Overflow用户

发布于 2022-02-23 07:08:44

以下是工作解决方案

代码语言:javascript
复制
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())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71219607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档