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

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.

Become a Member to join the conversation.