运行时错误:任务附加到不同的循环

ANe*_*egi 6 mongodb python-asyncio tornado-motor

嗨,我正在使用 AsyncIOMotorClient 对 mongoDb 进行异步数据库调用。下面是我的代码。

xyz.py
async def insertMany(self,collection_name,documents_to_insert):
    try:
        collection=self.database[collection_name]
        document_inserted = await collection.insert_many(documents_to_insert)
        return document_inserted
    except Exception:
        raise

def insertManyFn(self,collection_name,documents_to_insert):
    try:
        loop=asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        loop1=asyncio.get_event_loop()
        inserted_documents_count = loop1.run_until_complete(self.insertMany(collection_name, documents_to_insert))
        if inserted_documents_count==len(documents_to_insert):
            document_to_insert={Config.DB_JOB_COLUMN:Job.job_id,Config.DB_JOB_RESULT_COLUMN:Config.DB_JOB_RESULT_SUCCESS}
            loop1.run_until_complete(self.insertOne(Config.DB_JOB_COLLECTION, document_to_insert))
    except Exception:
        raise

xyz1.py
t=Timer(10,xyz.insertManyFn,\
                (collection_name,documents_to_insert))
t.start()   
Run Code Online (Sandbox Code Playgroud)

运行此程序时出现异常

RuntimeError: Task <Task pending coro=<xyz.insertMany() running at <my workspace location>/xyz.py:144> cb=[_run_until_complete_cb() at /usr/lib64/python3.5/asyncio/base_events.py:164]> got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/lib64/python3.5/asyncio/futures.py:431]> attached to a different loop
Run Code Online (Sandbox Code Playgroud)

在上面的程序中,insertManyFn 将在 10 秒后被调用并进行插入操作。但是当它第一次调用 insertMany 时,我得到了一个异常。

kol*_*pto 25

我仍然希望我的 MotorClient 位于模块的顶层,所以这就是我所做的:我修补MotorClient.get_io_loop以始终返回当前循环。

import asyncio
import motor.core

from motor.motor_asyncio import (
    AsyncIOMotorClient as MotorClient,
)

# MongoDB client
client = MotorClient('mongodb://localhost:27017/test')
client.get_io_loop = asyncio.get_running_loop

# The current database ("test")
db = client.get_default_database()


# async context
async def main():
    posts = db.posts
    await posts.insert_one({'title': 'great success!')


# Run main()
asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,我遇到了与 op 相同的错误,并添加 `client.get_io_loop = asyncio.get_running_loop` 有所帮助,但我不明白为什么会这样? (2认同)
  • @y_159 显然,一个应用程序可以初始化多个循环。但是,如果您的运行循环与电机创建的循环不同,那么您就会遇到麻烦:) 这种技巧可确保每个人都使用相同的循环 (2认同)

Udi*_*Udi 7

根据文档AsyncIOMotorClient如果您不使用默认值,则应传递一个 ioloop。创建事件循环后尝试创建客户端:

loop=asyncio.new_event_loop()
asyncio.set_event_loop(loop)
client = AsyncIOMotorClient(io_loop=loop)
Run Code Online (Sandbox Code Playgroud)

  • 我再次收到异常“附加到不同循环的任务” (6认同)