2

Extracted from python 3.6.8 documentation.

coroutine asyncio.sleep(delay, result=None, *, loop=None)

Create a coroutine that completes after a given time (in seconds). If result is provided, it is produced to the caller when the coroutine completes.

Question 1: What does the 2nd sentence mean, i.e. "If result is provided, ....."? I don't understand how to use the result argument. Can an example be provided to illustrate it's use?

Question 2: When should the loop argument be used? Can an example be given also to illustrate it's use?

1 Answer 1

3

I don't understand how to use the result argument.

result is simply the value that will be returned by asyncio.sleep once the specified time elapses. This is useful if you replace something that returns actual data with sleep(), e.g. for testing purposes, you can immediately specify a return value. For example:

data = await read_from_database()
...

if mocking:
    read_from_database = functools.partial(
        asyncio.sleep, 0.1, result='no data')
else:
    async def read_from_database():
        ... real implementation ...

When should the loop argument be used?

The loop argument is, as of Python 3.7 deprecated and scheduled for removal. It was useful in Python 3.5 and earlier, when the return value of asyncio.get_event_loop() wasn't guaranteed to be the currently running event loop, but an event loop associated with the thread. Since one can run multiple event loops during the lifetime of a thread, correct code had to propagate an explicit loop everywhere. If you were running in a non-default event loop, you had to specify the loop to asyncio.sleep and most other asyncio functions and constructors. This style is often encountered in old tutorials and is nowadays actively discouraged.

Sign up to request clarification or add additional context in comments.

4 Comments

Noted that loop will be depreciated. As for result, am I correct that it's value can be any string or number that I desire to be returned at the end of 0.1sec in your example? That is, in your example, read_from_database = 'no data' if mocking is True after 0.1sec?
@SunBear That is correct, you can pass any object and it will be returned by sleep() unchanged, after the delay elapses. I'm not sure what you meant by read_from_database = 'no data', but (after the await, and if mocking is true) it will hold that data == 'no data'.
I initially thought read_from_database = 'no data' because of read_from_database = functools.partial(asyncio.sleep, 0.1, result='no data'). Looking at it carefully, I realised read_from_data has actually become a pointer to functools.partial(<function sleep at 0x7fbe4a604d90>, 0.1, 'no data') and since data = await read_from_database(), i.e. the execution of the pointer results in a <generator object sleep at 0x7fbe49deb888>, therefore data == 'no data' is True. Thanks for clarifying.
@SunBear Right, even with normal functions, partial simply returns another function, not actual data passed to the function. Here the idea is to make read_from_database a synonym for sleep. The same could have been achieved with async def read_from_database(): return await asyncio.sleep(.1, result='no data'), but then one could simply have return 'no data' after the await so it wouldn't demonstrate result. The result feature is not very useful when async/await is available to compose async code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.