Hi,

I'm writing documentation to explain common traps with asynchronous
programming (with asyncio). While working on an example, I realized
that asyncio doesn't emit warnings when a task is scheduled before
never executed.

It's possible to detect when a coroutine is called but not scheduled,
detect when an exception is not consumed, but I don't see how to
detect when tasks are still scheduled at exit.

Well, there is Task.all_tasks(loop), but it doesn't give the traceback
where the task was created, and it doesn't include other scheduled
callbacks.

Example:
---
import asyncio
asyncio.tasks._DEBUG = True

@asyncio.coroutine
def important_task():
    yield from asyncio.sleep(1.0)
    print("import task")

def stop():
    loop.stop()

loop = asyncio.get_event_loop()
loop.call_later(0.1, stop)
asyncio.async(important_task())
loop.run_forever()
print("loop is stopped")
loop.close()
---

"import task" is not displayed because the task never completes.
Imagine that this task flushs important data on disk before exit.

BaseEventLoop.stop() documentation says:
"Every callback scheduled before stop() is called will run. Callback
scheduled after stop() is called won’t."

BaseEventLoop.close() should maybe warn when there are still scheduled
tasks? Maybe only in debug mode? I don't want to document
BaseEventLoop._scheduled.

By the way, the event loop destructor should maybe emit a
ResourceWarning if the event loop is not closed.

Victor

Reply via email to