gpt4 book ai didi

Python3 与 asyncpg 的语法错误

转载 作者:行者123 更新时间:2023-12-01 09:15:22 26 4
gpt4 key购买 nike

我正在尝试创建一个连接池,如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”:

async with asyncpg.create_pool(dsn=cs) as pool:
^
SyntaxError: invalid syntax

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

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

最佳答案

您应该将代码包装在 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

关于Python3 与 asyncpg 的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51322347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com