Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用motor连接MongoDB时,异步IO(Quart)抛出任务附加到不同的循环错误_Mongodb_Python 3.7_Quart_Motor Asyncio - Fatal编程技术网

使用motor连接MongoDB时,异步IO(Quart)抛出任务附加到不同的循环错误

使用motor连接MongoDB时,异步IO(Quart)抛出任务附加到不同的循环错误,mongodb,python-3.7,quart,motor-asyncio,Mongodb,Python 3.7,Quart,Motor Asyncio,我使用MongoDB和Motor.Asyncio创建了一个带有Quart的webapp。 当应用程序尝试查询数据库时,将抛出一个错误: Task <Task pending coro=<ASGIHTTPConnection.handle_request() running at /home/user/.local/lib/python3.7/site-packages/quart /asgi.py:59> cb=[_wait.<locals>._on_comple

我使用MongoDB和Motor.Asyncio创建了一个带有Quart的webapp。 当应用程序尝试查询数据库时,将抛出一个错误:

Task <Task pending coro=<ASGIHTTPConnection.handle_request() 
running at /home/user/.local/lib/python3.7/site-packages/quart
/asgi.py:59> cb=[_wait.<locals>._on_completion() at /usr/lib/python3.7
/asyncio/tasks.py:440]> got Future <Future pending cb=[run_on_executor.
<locals>._call_check_cancel() at /home/user/.local/lib/python3.7/site-
packages/motor/frameworks/asyncio/__init__.py:80]> attached to a 
different loop
在这之后,我加上:

    app = Quart(__name__)
我试着在马达导入块之前移动它,但它没有改变任何东西

如问题/答案所述: 我补充说:

这并没有解决问题

这是第一次调用motor时发生的块,错误发生在:

    try:
        session_info = await db.sessions.find_one(
            {
                'session_id': uuid.UUID(session_id)
            },
            {
                'username':True,
                '_id':False
            }
        )
    except Exception as e:
        print('error retrieving session info:', e)
我可以忽略错误并继续,但下一次调用时,同样的错误也会发生


我知道Quart在默认事件_循环上工作,不需要为马达创建特殊的循环。在上一个版本中,它可以工作,而不需要计算它。所以我完全不知所措。

我从这个问题出发,找到了解决办法:

这里提供的答案建议将mongoDB的初始化移到main()内部。在这种特定情况下,由于这是一个夸脱应用程序,因此本身没有主应用程序。但直觉依然存在

我在模块级定义了一个初始化函数,然后在调用db之前,我检查它是否已经初始化,如果没有,我调用初始化函数

    import motor.motor_asyncio
    from pymongo import ReturnDocument
    from bson.objectid import ObjectId
    from bson.son import SON

    client = None
    db = None
    fs = None

    async def connect_to_mongo():
        global client, db, fs
        client = motor.motor_asyncio.AsyncIOMotorClient()
        db = client.myDataBase
        fs = motor.motor_asyncio.AsyncIOMotorGridFSBucket(db)
然后在调用数据库之前:

    if db is None:
        await connect_to_mongo()

这解决了我的问题。为什么我的代码在升级之前工作?我不知道。

我知道它的回复很晚,但如果它也能帮助别人

您可以使用Quart Motor(pip安装Quart Motor)并在应用程序中的任何位置使用它

来自夸脱电机进口电机
app=夸脱(\uuuuuu名称\uuuuuuuuu)
mongo=Motor(应用程序,uri='…')
@应用程序路径(“/”)
异步def用户信息(用户名):
user=wait mongo.db.users.find_one_或_404({“username”:user_name})
返回呈现模板(“user.html”,user=user)
注:我是Quart Motor的开发者

    import motor.motor_asyncio
    from pymongo import ReturnDocument
    from bson.objectid import ObjectId
    from bson.son import SON

    client = None
    db = None
    fs = None

    async def connect_to_mongo():
        global client, db, fs
        client = motor.motor_asyncio.AsyncIOMotorClient()
        db = client.myDataBase
        fs = motor.motor_asyncio.AsyncIOMotorGridFSBucket(db)
    if db is None:
        await connect_to_mongo()
from quart_motor import Motor

app = Quart(__name__)
mongo = Motor(app,  uri='...')

@app.route('/<user_name:str>')
async def user_info(user_name):
    user = await mongo.db.users.find_one_or_404({"username": user_name})
    return render_template("user.html", user=user)