#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
async def foo():
await time.sleep(1)
foo()
I couldn't make this dead simple example to run:
RuntimeWarning: coroutine 'foo' was never awaited foo()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
async def foo():
await time.sleep(1)
foo()
I couldn't make this dead simple example to run:
RuntimeWarning: coroutine 'foo' was never awaited foo()
Running coroutines requires an event loop. Use the
asyncio()
library to create one:or
Also see the Tasks and Coroutines chapter of the
asyncio
documentation. If you already have a loop running, you'd want to run additional coroutines concurrently by creating a task (asyncio.create_task(...)
in Python 3.7+,asyncio.ensure_future(...)
in older versions).Note however that
time.sleep()
is not an awaitable object. It returnsNone
so you get an exception after 1 second:In this case you should use the
asyncio.sleep()
coroutine instead:which is cooperates with the loop to enable other tasks to run. For blocking code from third-party libraries that do not have asyncio equivalents, you could run that code in an executor pool. See Running Blocking Code in the asyncio development guide.
If you already have a loop running (with some other tasks), you can add new tasks with:
otherwise you might get
error.