Python3 SyntaxError与asyncpg

2024-05-15 04:33:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个如the documentation所示的连接池来测试模块。在

这是我最后一次尝试:

import asyncpg
cs = "user:password@my_postgresql_server_ipaddr:port/database?name=db_name"

async with asyncpg.create_pool(dsn=cs) as pool:
    print("pool created")

我在第4行得到一个语法错误,指向'with':

^{2}$

在终端上从Python解释器运行代码会产生相同的结果。在

Python版本是3.6.5,使用python3 script.py从终端运行脚本


Tags: 模块thenameimport终端serverpostgresqlmy
1条回答
网友
1楼 · 发布于 2024-05-15 04:33:47

您应该将代码包装在async函数中,并在循环中调用它,例如:

import asyncio
import asyncpg

async def test():
    cs = "user:password@my_postgresql_server_ipaddr:port/database?name=db_name"
    async with asyncpg.create_pool(dsn=cs) as pool:
        print("pool created")

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(test())
    loop.close()

更多详细信息:example-chain-coroutines

相关问题 更多 >

    热门问题