Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

asyncio.sleep() and Writing Your First Coroutine

Give Feedback

In this lesson, you’ll learn how to convert a normal synchronous function into an asynchronous coroutine.

from random import randint
import time
import asyncio


async def randn():
   await asyncio.sleep(3)
    return randint(1, 10)

Comments & Discussion

Vaibhav Chauhan on April 21, 2019

so what’s the event loop in this example ?

UBBA on May 16, 2019

No event loop in this lesson. Next lesson.

Pygator on Sept. 14, 2019

Don’t understand how a coroutine is a consumer? It produced a random number here. I see how generators are produces, because they “generate” sequences one value at a time.

fofanovis on April 9, 2020

Dont understand how a coroutine is a consumer? It produced a random number here. I see how generators are produces, because they generate sequences one value at a time.

If you watch more, you’ll see that they consume output from async funcs using await. In that sense, they are consumers.

Become a Member to join the conversation.