17
async def start(channel):
    while True:
        m = await client.send_message(channel, "Generating... ")
        generator.makeFile()
        with open('tmp.png', 'rb') as f:
            await client.send_file(channel, f) 
        await client.delete_message(m)
        await asyncio.sleep(2)

I have a discord bot that runs a task every 2 seconds. I tried using an infinite loop for this, but the script crashes with a Task was destroyed but it is still pending! I have read about asyncio's coroutines, but none of the examples that I found use await in them. Is it possible avoid this error, by running a coroutine with await, for example?

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
user8245289
  • 173
  • 1
  • 1
  • 7
  • `await` is not a problem here. More the `while True` is also common way for periodic calls (https://stackoverflow.com/questions/37512182/how-can-i-periodically-execute-a-function-with-asyncio). Show how you execute that function, are you tried to stop the task in the code? – kwarunek Jul 08 '17 at 10:15

1 Answers1

12

Task was destroyed but it is still pending! is warning that you receive when you call loop.close() when some of tasks in your script aren't finished. Usually you should avoid this situation because unfinished task may not release some resources. You need either to await task done or cancel it before event loop closed.

Since you have infinite loop you probably would need to cancel task, example:

import asyncio
from contextlib import suppress


async def start():
    # your infinite loop here, for example:
    while True:
        print('echo')
        await asyncio.sleep(1)


async def main():
    task = asyncio.Task(start())

    # let script some thime to work:
    await asyncio.sleep(3)

    # cancel task to avoid warning:
    task.cancel()
    with suppress(asyncio.CancelledError):
        await task  # await for task cancellation


loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
    loop.run_until_complete(main())
finally:
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()

See also this answer for more information about tasks.

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
  • If i'm awaiting two async tasks(task1 and task2) inside main func in the same order, task1 is having a while true loop and calling one more async task(task3) inside that while loop. Will the execution be ever passed to task2 in this case? – y_159 Feb 04 '22 at 08:05
  • @y_159 sorry, it's hard for me to follow. Any chance you could write this in code? – Mikhail Gerasimov Feb 04 '22 at 10:40
  • @MikhailGerasimov thanks, I tested via a script and async is working as it is supposed to even if tasks contain while True loops. – y_159 Feb 04 '22 at 21:01