小编fpb*_*bhb的帖子

如何使用 Python asyncio 在 asyncpg API 上建立同步外观?

想象一下一个异步aiohttpWeb 应用程序,它由通过连接的 Postgresql 数据库支持asyncpg,并且不执行其他 I/O。我怎样才能有一个托管应用程序逻辑的中间层,而不是异步的?(我知道我可以简单地使所有内容异步 - 但想象我的应用程序具有大量应用程序逻辑,仅受数据库 I/O 约束,并且我无法触及其中的所有内容)。

\n

伪代码:

\n
async def handler(request):\n    # call into layers over layers of application code, that simply emits SQL\n    ...\n\ndef application_logic():\n    ...\n    # This doesn\'t work, obviously, as await is a syntax\n    # error inside synchronous code.\n    data = await asyncpg_conn.execute("SQL")\n    ...\n    # What I want is this:\n    data = asyncpg_facade.execute("SQL")\n    ...\n
Run Code Online (Sandbox Code Playgroud)\n

如何asyncpg构建同步 fa\xc3\xa7ade 来允许应用程序逻辑进行数据库调用?async.run()在这种情况下,诸如 using或等浮动的配方asyncio.run_coroutine_threadsafe()不起作用,因为我们来自已经异步的上下文。我认为这不可能是不可能的,因为已经有一个事件循环原则上可以运行协 …

python python-asyncio aiohttp asyncpg

10
推荐指数
1
解决办法
1545
查看次数

标签 统计

aiohttp ×1

asyncpg ×1

python ×1

python-asyncio ×1