Skip to content

Latest commit

 

History

History
35 lines (22 loc) · 1.23 KB

asyncio-and-asynchronous-programming.md

File metadata and controls

35 lines (22 loc) · 1.23 KB

AsyncIO & Asynchronous Programming

Introduction

Traditional, Synchronous Programming

Normally, functions will run synchronously. For example, say that you had three different functions within your code, traditionally, your functions will wait until a return true or termination and then execute the next function in the code.

func1()
func2()
func3()

Asynchronous Programming

Please note, this is NOT to be confused with multithreading. This is completely different.

Hypothetically, we have three functions and we are waiting on func1() to finish, however, we do NOT want to waste CPU time, so we decide to execute func2(). We are NOT running func1() and func2() simultaneously, but rather putting func1() to sleep or in a waiting state and use our CPU time on func2() or func3() while we wait to be very efficient.

In order to achieve this, we will need to import the asyncio library.

import asyncio

You want to define a function as asynchronous, rather than the whole program.

To achieve this, you will use the async keyword:

async def main():

The next step is to sleep this