[Python] Timeout context manager should be used inside a task discord.py

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    My understanding of this error is that it is complaining that it is not in an async def function. However, mine is and I even used loop = asyncio.new_event_loop() followed by loop.run_until_complete(onInfoAsync(info,conn)). Here is the error:

    Timeout context manager should be used inside a task
    File "C:\Users\ianfi\Documents\pythonStuff\PyQt5\ServerCalling\main.py", line 42, in onInfoAsync
    dm = await member.create_dm()
    File "C:\Users\ianfi\Documents\pythonStuff\PyQt5\ServerCalling\main.py", line 57, in onInfo
    loop.run_until_complete(onInfoAsync(info,conn))


    as you can see in the error, it tracebacks to loop.run_until_complete(onInfoAsync(info,conn)) which puts it in a task. I don't know what I'm doing wrong, and if you need more information, dont hesitate to ask, I'm new to stack overflow.

    Here's some other stuff I tried: I found online that doing asyncio.get_event_loop() instead of making a new one might fix it, but it just errors saying that there is no loop to get. I have also tried using asyncio.set_event_loop(loop) but that did not change the results.

    Thanks for any help!

    Edit: I have tried some more things and made a kind of middleman function.

    async def between(info:str, conn):
    task = asyncio.create_task(onInfoAsync(info,conn))
    await task


    although the error was complaining about it not being in a task (which it very obviously is now), it still errors. Here is the traceback with this function in place:

    Timeout context manager should be used inside a task
    File "C:\Users\ianfi\Documents\pythonStuff\PyQt5\ServerCalling\main.py", line 51, in onInfoAsync
    dm = await member.create_dm()
    File "C:\Users\ianfi\Documents\pythonStuff\PyQt5\ServerCalling\main.py", line 36, in between
    await task
    File "C:\Users\ianfi\Documents\pythonStuff\PyQt5\ServerCalling\main.py", line 75, in onInfo
    loop.run_until_complete(between(info,conn))

    Continue reading...

Compartilhe esta Página