Sobes.tech
Middle

What will be res = asyncio.sleep(1) without await, is it possible to call it like that?

sobes.tech AI

Answer from AI

Calling res = asyncio.sleep(1) without await does not start the pause, but simply returns a coroutine object (an object of type coroutine). This coroutine will not be executed until you explicitly call await or run it in an event loop.

That is, without await, you will not get a delay, only an object that needs to be run later. Such a call is permissible if you plan to pass the coroutine further or run it later, but simply calling asyncio.sleep(1) without await is a logical error, as the delay will not occur.

Example:

import asyncio

async def main():
    res = asyncio.sleep(1)  # res is a coroutine, the pause has not started
    print(res)  # <coroutine object sleep at 0x...>
    await res  # now the pause will execute

asyncio.run(main())