AsyncIO in Python (Guide and Example).pdf

PreetAujla6 165 views 8 slides Nov 05, 2023
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

What is AsyncIO?
AsyncIO is a library in Python that provides a framework for writing concurrent code using the async/await syntax. It is built on the concept of coroutines, which are an extension of Python generators. The AsyncIO library enables Python to handle asynchronous I/O operations like rea...


Slide Content

AsyncIOinPython:ACompleteWalkthrough
Introduction
WhatisAsyncIO?
AsyncIOisalibraryinPythonthatprovidesaframeworkforwritingconcurrentcodeusingthe
async/awaitsyntax.Itisbuiltontheconceptofcoroutines,whichareanextensionofPython
generators.TheAsyncIOlibraryenablesPythontohandleasynchronousI/Ooperationslike
readingandwritingtofiles,networkcalls,andotherI/O-intensiveoperationswithouttheneed
formulti-threadingormulti-processing.Itallowsfortheexecutionofcodewhilewaitingfor
theseoperationstocomplete,whichcanleadtomoreefficientuseofresourcesandbetter
performance,especiallyinI/O-boundandhigh-levelstructurednetworkcode.
TheImportanceofAsynchronousProgramminginPython
AsynchronousprogrammingiscriticalinPythonfordevelopingapplicationsthatcanperform
multipleI/O-boundtasksconcurrentlywithoutblockingtheexecutionoftheprogram.Thisis
especiallyimportantinmodernwebdevelopment,wherehandlingalargenumberof
simultaneousconnectionsinaperformantwayisacommonrequirement.Traditional
synchronouscoderunssequentially,whichmeansthattheapplicationcanbesignificantly
sloweddownwaitingforI/Ooperationstocomplete.AsyncIOprovidesawaytowritecodethat
isnon-blockingandcanhandlemanytasksconcurrently,makingitpossibletobuildscalable
andresponsiveapplications.
Real-worldApplicationsofAsyncIO
AsyncIOisusedinavarietyofreal-worldapplicationswherenon-blockingI/Ocanimprove
performance:

●WebServersandFrameworks:ManymodernPythonwebframeworks,likeFastAPIand
Sanic,useAsyncIOtohandlewebrequestsasynchronously.Thisallowsthemto
managethousandsofconnectionsatthesametime,makingthemhighlyscalable.
●WebScrapingandCrawling:AsyncIOcanbeusedtospeedupwebscrapingand
crawlingtasksbysendingmultiplerequestsconcurrentlyandwaitingforresponsesina
non-blockingmanner.
●MicroservicesandCloudServices:Inmicroservicearchitectures,AsyncIOallows
servicestocommunicatewitheachotherasynchronously,whichcanimprovethroughput
andreducelatency.
●DataProcessingPipelines:Fordata-intensiveapplications,AsyncIOcanmanagedata
streamsandprocessdatawithoutwaitingforeachoperationtocompletebeforestarting
thenextone.
●NetworkedApplications:Applicationsthatrequirecommunicationovernetwork
protocols,suchasHTTP,WebSocket,andMQTT,canbenefitfromAsyncIO’sabilityto
handleasynchronousnetworkcallsefficiently.
●IoTDevices:InternetofThings(IoT)devicesoftenneedtohandlemultiplesensorinputs
andoutputssimultaneously,whichcanbeefficientlymanagedusingAsyncIO.
ByleveragingAsyncIO,developerscanwritecodethatisbothefficientandreadable,maintaining
asingle-threaded,single-processcodebasewhileperformingtasksthattraditionallywould
requirecomplexconcurrencymanagementtechniques.
Prerequisites
TofullygraspandeffectivelyutilizeAsyncIOinPython,itisessentialtocomepreparedwith
certainfoundationalskillsandknowledge.Belowisadetailedexplanationoftheprerequisites:
BasicUnderstandingofPythonProgramming:

1.PythonSyntaxandSemantics:YoushouldbecomfortablewithPython’ssyntax,
includingdefiningfunctions,usingloops,andwritingconditionals.Agoodgraspof
Python’suniquefeatureslikelistcomprehensionsandlambdafunctionswillalsobe
beneficial.
2.WritingFunctionsandClasses:FunctionsarethebuildingblocksofPythoncode,and
understandinghowtowriteandusethemiscrucial.Classesandobjectsarealsocentral
toPython,andknowledgeofobject-orientedprogramming(OOP)principleswillhelpyou
understandmoreadvancedAsyncIOconcepts.
3.ControlFlowMastery:Beingadeptatcontrollingtheflowofyourprogramisessential.
Thisincludesusingif,elif,andelsestatementsforbranching,forandwhileloopsfor
iteration,andtry,except,andfinallyblocksforhandlingexceptions.
4.FamiliaritywithPython’sStandardLibrary:Built-inFunctionsandDataTypes:Knowing
Python’sbuilt-infunctionsandcomplexdatatypeslikedictionaries,sets,tuples,andlists
isnecessary.Theseareoftenusedtomanageandorganizedatawithinasynchronous
operations.
5.StandardLibraries:Aworkingknowledgeofstandardlibrarieslikeosforinteractingwith
theoperatingsystem,sysforaccessingsystem-specificparametersandfunctions,and
collectionsforspecializedcontainerdatatypeswillserveasastrongfoundation.
6.FileI/OOperations:Understandinghowtoperformfileoperations—readingfromfiles,
writingtofiles,andunderstandingfilepointers—isimportant,asasynchronousfileI/Ois
acommonusecaseforAsyncIO.
7.ConceptualKnowledgeofSynchronousvs.AsynchronousExecution:Synchronous
(Blocking)Operations:Youshouldunderstandhowsynchronouscodeexecutes
operationsinsequence,oneaftertheother,oftenleadingtowaiting(or“blocking”)foran
operationtocompletebeforemovingontothenext.Thisisthetraditionalwayofwriting
scriptsandprogramsinPython.
8.Asynchronous(Non-blocking)Operations:Asynchronouscode,ontheotherhand,
allowsaprogramtostartanoperationandmoveontoanotheronebeforethefirsthas
finished.ThisisparticularlyusefulforI/O-boundtaskswheretheprogramwould

otherwisespendalotoftimewaiting,suchaswebrequests,databaseoperations,orfile
reads/writes.
9.BenefitsofAsynchronousExecution:Anunderstandingofhowasynchronousexecution
canleadtomoreefficientuseofresources,betterperformance,andamoreresponsive
programiskey.It’salsoimportanttoknowwhennottouseasynchronousprogramming,
asitcanaddcomplexityandisnotalwaystherightchoiceforCPU-boundtasks.
Withtheseprerequisites,learnersarewell-equippedtotackletheintricaciesofasynchronous
programmingwithAsyncIOinPython.It’srecommendedtobrushupontheseareasifyou’renot
alreadycomfortablewiththem,astheyformthebedrockuponwhichyourunderstandingof
AsyncIOwillbebuilt.
UnderstandingAsynchronousProgramming
IntroductiontoAsynchronousProgramming
Asynchronousprogrammingisadesignparadigmthatfacilitatestheconcurrentexecutionof
taskswithoutblockingtheflowoftheprogram.Itallowsaprogramtoperformworkwhile
waitingforsomethingtocomplete,whichisoftenanI/Ooperation.Intraditionalsynchronous
programming,tasksareperformedoneaftertheother,eachtaskwaitingforthepreviousoneto
finish.Asynchronousprogramming,however,enablestaskstoruninparallel,oratleastappear
todoso,bystartingataskandthenmovingontoanotherwithoutwaitingforthefirstto
complete.
DifferencesBetweenSynchronousandAsynchronousProgramming
●ExecutionFlow:Insynchronousprogramming,thecodeexecutionblocksorwaitsforan
operationtocompletebeforemovingontothenextlineofcode.Asynchronous
programming,ontheotherhand,allowstheexecutiontocontinuewithothertaskswhile
waitingforotheroperationstofinish.

●Performance:Synchronouscodecanbelessefficient,especiallywhendealingwithI/O
operations,astheCPUremainsidlewhilewaitingfortheI/Otasktocomplete.
AsynchronouscodecanimproveperformancebyutilizingtheCPUmoreeffectively
duringthesewaitingperiods.
●Complexity:Synchronouscodeisgenerallystraightforwardandeasytounderstand
becauseitexecutesinalinearfashion.Asynchronouscodecanbemorecomplexdueto
theconcurrentnatureoftaskexecution,whichcanmakeithardertofollowanddebug.
●UseCases:Synchronousprogrammingissuitableforsimplescriptsandprograms
whereconcurrencyisnotrequired.Asynchronousprogrammingisbeneficialinscenarios
wheretheprogramneedstohandlemultipleI/O-boundtaskssimultaneously,suchasin
webservers,APIs,orcomplexdataprocessingpipelines.
EventLoopsandConcurrency
●EventLoop:ThecoreconceptofasynchronousprogramminginPythonistheevent
loop.Theeventloopisresponsibleformanaginganddistributingtheexecutionof
differenttasks.Itkeepstrackofalltherunningtasksandexecutesacallbackwhena
taskiscompletedorwhenaneventoccurs.
●Concurrency:Concurrencyisthenotionofmultipletasksmakingprogresswithout
necessarilycompletinganysingletaskbeforemovingontothenextone.It’simportantto
notethatconcurrencyisnotthesameasparallelism,whichisaboutperformingmultiple
tasksatexactlythesametime.InPython,duetotheGlobalInterpreterLock(GIL),true
parallelismisnotachievablewiththreads,whichmakesasynchronousprogrammingan
attractivealternativeforconcurrency.
WhentoUseAsynchronousProgramming
Asynchronousprogrammingisparticularlyusefulinthefollowingscenarios:
●WebDevelopment:Handlingmultiplewebrequestssimultaneouslywithoutblockingthe
server.

●NetworkServices:Writingnetworkapplicationsthatrequirenon-blockingnetworkI/O.
●I/OBoundApplications:ProgramsthatspendalotoftimewaitingforI/Ooperations,
suchasfilereading/writing,databaseoperations,ornetworkcommunications.
●Real-timeDataProcessing:Applicationsthatneedtoprocessdatainreal-time,suchas
chatapplicationsorlivedatafeeds.
However,asynchronousprogrammingmightnotbethebestchoiceforCPU-boundtasksthat
requireheavycomputationandlittletonoI/O.Insuchcases,usingmulti-processingorother
parallelismtechniquesmightbemoreappropriate.It’salsoworthnotingthatasynchronous
codecanintroducecomplexityandshouldbeusedonlywhenthebenefitsoutweighthe
potentialforincreasedcomplexity.
GettingStartedwithAsyncIO
SettingUptheEnvironment
BeforeyoustartusingAsyncIO,youneedtoensureyourenvironmentissetupcorrectly.
AsyncIOisincludedinthePythonstandardlibraryfromPython3.4onwards,soyoushouldhave
Python3.4orhigherinstalledonyoursystem.YoucancheckyourPythonversionbyrunning
python–versionorpython3–versioninyourterminal.
Ifyou’reusinganolderversionofPython,you’llneedtoupdatetoanewerversiontouse
AsyncIO.It’salsorecommendedtouseavirtualenvironmenttomanageyourproject’s
dependenciesseparatelyfromyoursystem’sPythoninstallation.
TheAsyncIOEventLoop
TheeventloopisthecoreoftheAsyncIOlibrary.It’saninfiniteloopthatwaitsforand
dispatcheseventsormessagesinaprogram.Itrunsasynchronoustasksandcallbacks,
performsnetworkIOoperations,andrunssubprocesses.

Hereisasimpleexampleofhowtocreateandrunaneventloop:
importasyncio
#Thisisthecoroutinethatwillberunbytheeventloop
asyncdefmain():
print('Hello')
awaitasyncio.sleep(1)
print('World')
#Getthedefaulteventloop
loop=asyncio.get_event_loop()
#Runthecoroutine
loop.run_until_complete(main())
#Closetheloop
loop.close()
WritingYourFirstAsyncProgram
Let’swriteasimpleasyncprogramthatusesasyncdeftodefineanasynchronousfunction,or
“coroutine”,andawaittopauseitsexecutionuntiltheawaitedtaskiscomplete.
importasyncio
#Defineacoroutine
asyncdefgreet(delay,name):
awaitasyncio.sleep(delay)
print(f"Hello,{name}")
#Themainentrypointoftheprogram
asyncdefmain():
print("Startingthegreetingprogram...")
#Scheduletwogreetingstorunconcurrently
awaitasyncio.gather(
greet(2,'Alice'),
greet(1,'Bob')
)
print("Finishedgreeting!")

#Runthemaincoroutine
asyncio.run(main())
Inthisexample,asyncio.run(main())isahigh-levelAPItorunthetop-levelcoroutine,main,and
automaticallymanagetheeventloop.Insidemain,weuseasyncio.gathertorunthegreet
coroutineconcurrentlyfor‘Alice’and‘Bob’.
ReadmyfullarticleonAsyncIOinPython(GuideandExample)at:
https://codingparks.com/asyncio-in-python/