您好我使用AsyncIOMotorClient異步數據庫調用到mongoDb。 以下是我的代碼。RuntimeError:任務附加到不同的循環
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()
雖然運行此我得到一個異常
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
在上面的程序insertManyFn將被調用10秒後,做的插入操作。但是,當它第一次調用insertMany時,我得到一個異常。
試過這樣做,但得到一個錯誤,類型錯誤:「_UnixSelectorEventLoop」對象不是可迭代 – ANegi
對不起,呼叫應該是:'AsyncIOMotorClient(io_loop =循環)' – Udi
再次我得到異常「任務attachted到不同的循環「 – ANegi