我正在尝试创建一个连接池,如文献资料所示,以测试模块。
,这是我最后一次尝试:
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行得到一个SyntaxError,指向“with”:
async with asyncpg.create_pool(dsn=cs) as pool:
^
SyntaxError: invalid syntax在终端上运行Python解释器上的代码会产生相同的结果。
Python为3.6.5,使用python3 script.py从终端运行脚本
发布于 2018-07-13 10:13:05
您应该将代码包装在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()更多细节:例子-链-协同线
https://stackoverflow.com/questions/51322347
复制相似问题