python - 如何调用异步函数并阻止所有其他协程直到它完成?
问题描述
我有一个不和谐的机器人,在它的on_connect
功能中,我循环并检查机器人的 MongoDB 数据库(例如,检查在它停机时加入的新公会)。那段代码如下所示:
# inside the client class
async def on_connect(self):
await self.init_database() # I need this to block everything
# ...
async def init_database(self):
# loop through everything in the db with a custom data manager that uses Motor to connect
# with MongoDB (requires async/await).
# takes ~1 minute to run if I don't block other coroutines.
有什么办法可以阻止所有其他协程直到init_database
完成运行?
编辑:我刚刚意识到,如果我不让所有的协程都在init_database
阻塞中,那么只有init_database
阻塞是没有用的。如果我可以在一个 coro 阻塞中制作所有 coros,那将是最好的,否则我将不得不同时使用 PyMongo 和 Motor。
解决方案
推荐阅读
- c - 使用 malloc 给了我比预期更多的内存?
- python - 如何将数字舍入到 10、100、1000、10000、100000
- r - 你可以在 R 中聚合多个不同的 LHS 变量吗?
- javascript - 在 Google Maps API 上实现地形图图块
- minikube - minikube / kvm2 的问题
- amazon-web-services - 为什么 IO 99.99 % 即使磁盘读写看起来很小
- npm - 一段时间后,我的 npm 模块似乎部分消失了
- kubernetes - 如何将配置文件放在 pod 中?
- apache-spark - 如果块大小为 128 MB,则需要大的执行器内存
- identityserver4 - IdentityServer4:调用 AddConfigurationStore 扩展方法时重用 SqlConnection