SlideShare a Scribd company logo
1 of 48
Download to read offline
ASYNCHRONOUS
PROGRAMMING IN PYTHON
P R E S E N T, PA S T A N D F U T U R E O F
www.prontotools.io
Y O T H I N M U A N G S O M M U K
Y O T H I N
M U A N G S O M M U K
A B O U T M E
Pythonista
Best in class productivity tools that enable smart and
efficient marketing execution for small business
B e t t e r M a r k e t i n g V i s i b i l i t y
A S Y N C
A programming technique where execution order is not
known ahead of time
W H Y W E N E E D T O K N O W A S Y N C
P R O G R A M M I N G
B E C A U S E I T M A K E Y O U R C O D E
G O FA S T
H O W FA S T
B O B B Y F I S C H E R P L AY I N G 5 0 O P P O N E N T S S I M U LTA N E O U S LY, 1 9 6 4
S Y N C H R O N O U S C H E S S
E X H I B I T I O N
- 50 opponents
- Fischer move in 5 seconds
- Opponents move in 55 seconds
- Game average 30 move pairs
Assumptions:
Each game runs for 30 minutes
50 sequential games would take
50 x 30 min = 1500 min = 25 hours
B O B B Y F I S C H E R P L AY I N G 5 0 O P P O N E N T S S I M U LTA N E O U S LY, 1 9 6 4
A S Y N C H R O N O U S C H E S S
E X H I B I T I O N
- Fischer moves on first game
- While opponent thinks, he moves

on second game, the third, fourth…
- A move on all 50 game takes him

50 x 5 sec = 250 sec = 4 min
- After he complete the round, the 

first game is ready for his next move
- 50 games are completed in

4 min x 30 = 120 min = 2 hour
C O R O U T I N E
– W I K I P E D I A
C o r o u t i n e s a r e c o m p u t e r p r o g r a m
components that generalize subroutines for
nonpreemptive multitasking, by allowing
multiple entry points for suspending and
resuming execution at certain locations.
coroutines are functions whose execution
you can pause
E V E N T L O O P
– W I K I P E D I A
Event loop is a programming construct that
waits for a dispatches events or messages
in a program.
A B R I E F H I S T O RY
S I M P L E G E N E R AT O R
P Y T H O N 2 . 2 ( P E P 2 5 5 ) - 1 8 M AY 2 0 0 1
Generator was born with
“yield” expression make us
able to generate iterator
without holding memory
upfront, all you need is
memory for the current value
def eager_range(up_to):
sequence = []
index = 0
while index < up_to:
sequence.append(index)
Index += 1
return sequence
S I M P L E G E N E R AT O R
P Y T H O N 2 . 2 ( P E P 2 5 5 ) - 1 8 M AY 2 0 0 1
Generator was born with
“yield” expression make us
able to generate iterator
without holding memory
upfront, all you need is
memory for the current value
def lazy_range(up_to):
index = 0
while index < up_to:
yield index
Index += 1
C O R O U T I N E S V I A
E N H A N C E D G E N E R AT O R S
P Y T H O N 2 . 5 ( P E P 3 4 2 ) - 1 0 M AY 2 0 0 5
“send()” method on
generator, that allowed us
not only pause the
generator, but also send
back the value to generator
def jumping_range(up_to):
index = 0
while index < up_to:
jump = yield index
if jump is None:
jump = 1
index += jump
if __name__ == '__main__':
iterator = jumping_range(5)
print(next(iterator)) # 0
print(iterator.send(2)) # 2
print(next(iterator)) # 3
S Y N TA X F O R D E L E G AT I N G T O
A S U B G E N E R AT O R
P Y T H O N 3 . 3 ( P E P 3 8 0 ) - 1 3 F E B 2 0 0 9
“yield from” expression for yield every
value from iterator (generator) also able to
chain generator together which made us
able to bubble up and down the call stack
without code having to do anything special
def lazy_range(up_to):
index = 0
def gratuitous_refactor():
nonlocal index
while index < up_to:
yield index
index += 1
yield from gratuitous_refactor()
S Y N TA X F O R D E L E G AT I N G T O
A S U B G E N E R AT O R
P Y T H O N 3 . 3 ( P E P 3 8 0 ) - 1 3 F E B 2 0 0 9
“yield from” expression for yield every
value from iterator (generator) also able to
chain generator together which made us
able to bubble up and down the call stack
without code having to do anything special
def bottom():
return (yield 42)
def middle():
return (yield from bottom())
def top():
return (yield from middle())
gen = top()
value = next(gen)
print(value) # 42
P Y T H O N 3 . 4 ( P E P 3 1 5 6 ) - 1 2 D E C 2 0 1 2
Pluggable event loop made non blocking
IO happen, but not actual asynchronous
programming. It’s concurrent programming
A S Y N C I O
import asyncio
loop = asyncio.get_event_loop()
@asyncio.coroutine
def hello():
print('Hello')
yield from asyncio.sleep(3)
print('World!')
if __name__ == '__main__':
loop.run_until_complete(hello())
A S Y N C A WA I T
P Y T H O N 3 . 5 ( P E P 4 9 2 ) - 9 A P R 2 0 1 5
Explicit asynchronous code there is
no need to do “asyncio.coroutine”
decorator with generator that return
“yield from” anymore. Using
“async def” syntax to define that
function is a coroutine and use
“await” or “return” for delegate
the results like “yield from”
import asyncio
loop = asyncio.get_event_loop()
async def hello():
print('Hello')
await asyncio.sleep(3)
print('World!')
if __name__ == '__main__':
loop.run_until_complete(hello())
import asyncio
async def compute(x, y):
print(“Compute %s + %s …” % (x, y))
await asyncio.sleep(1.0)
return x + y
async def print_sum(x, y):
result = await compute(x, y)
print(“%s + %s = %s” % (x, y, result))
loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()
A S Y N C A WA I T
P Y T H O N 3 . 5 ( P E P 4 9 2 ) - 9 A P R 2 0 1 5
Explicit asynchronous code there is
no need to do “asyncio.coroutine”
decorator with generator that return
“yield from” anymore. Using
“async def” syntax to define that
function is a coroutine and use
“await” or “return” for delegate
the results like “yield from”
ref: https://docs.python.org/3/library/asyncio-task.html
A S Y N C G E N E R AT O R
P Y T H O N 3 . 6 ( P E P 5 2 5 ) - 2 8 J U L 2 0 1 6
Remove restriction to use await and yield
in the same function body
import asyncio
loop = asyncio.get_event_loop()
async def ticker(delay, to):
for i in range(to)
yield i
await asyncio.sleep(delay)
if __name__ == '__main__':
loop.run_until_complete(ticker(5, 10))
A S Y N C
C O M P R E H E N S I O N S
P Y T H O N 3 . 6 ( P E P 5 3 0 ) - 3 S E P 2 0 1 6
Introduce async for and await expression
in all kinds of comprehensions
result = [ i async for i in aiter() if i % 2 ]
result = [ await fun() for fun in funcs if await condition() ]
E V E N T L O O P
P L U G G A B L E
U V L O O P
import asyncio
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
H T T P B E N C H M A R K S
▄▄▄▄▄
▀▀▀██████▄▄▄ _______________
▄▄▄▄▄ █████████▄ / 
▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Gotta go fast! |
▀▀█████▄▄ ▀██████▄██ | _________________/
▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
▀▀▀▄ ▀▀███ ▀ ▄▄
▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌
██▀▄▄▄██▀▄███▀ ▀▀████ ▄██
▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀
▌ ▐▀████▐███▒▒▒▒▒▐██▌
▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
▀▀█████████▀
▄▄██▀██████▀█
▄██▀ ▀▀▀ █
▄█ ▐▌
▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
▌ ▐ ▀▀▄▄▄▀
▀▀▄▄▀
S A N I C
from sanic import Sanic
from sanic.response import son
app = Sanic()
@app.route(“/“)
async def test(request):
return json({“Hello”: “world”})
if __name__ == “__main__”:
app.run(host=“0.0.0.0”, port=8000)
J A P R O N T O
H T T P S : / / M E D I U M . F R E E C O D E C A M P. C O M / M I L L I O N - R E Q U E S T S - P E R - S E C O N D - W I T H - P Y T H O N - 9 5 C 1 3 7 A F 3 1 9
J A P R O N T O
J A P R O N T O
import asyncio
from japronto import Application
app = Application()
async def asynchronous(request):
for i in range(1, 4):
await asyncio.sleep(1)
print(i, ‘seconds elapsed’)
return request.Response(text=‘3 sec elapsed’)
r = app.router
r.add_route(‘/async’, asynchronous)
app.run()
T O K I O
A LT E R N AT I V E
C U R I O
C U R I O
- Using same async / await syntactic
- Different approach from any other I / O Libraries
- Simplicity
O T H E R L I B R A R I E S
- aiohttp
- asyncpg
- aiosmtp
- aio.testing
- aioelasticsearch
- etc.
Questions?
R E F E R E N C E
- https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/
- https://hackernoon.com/asynchronous-python-45df84b82434
- https://magic.io/blog/uvloop-blazing-fast-python-networking/
- https://github.com/channelcat/sanic
- https://medium.freecodecamp.com/million-requests-per-second-with-python-95c137af319
- https://github.com/PyO3/tokio
- Miguel Grinberg Asynchronous Python for the Complete Beginner PyCon 2017
- Yury Selivanov asyncawait and asyncio in Python 3 6 and beyond PyCon 2017
We are hiring!
https://www.prontomarketing.com/company/careers/

More Related Content

What's hot

computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7
ecomputernotes
 

What's hot (18)

Of class3
Of class3Of class3
Of class3
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
C exam
C examC exam
C exam
 
Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Program of speed and direction control of dc motor
Program of speed and direction control of dc motorProgram of speed and direction control of dc motor
Program of speed and direction control of dc motor
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfix
 
The Ring programming language version 1.2 book - Part 34 of 84
The Ring programming language version 1.2 book - Part 34 of 84The Ring programming language version 1.2 book - Part 34 of 84
The Ring programming language version 1.2 book - Part 34 of 84
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
Sary
SarySary
Sary
 
งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์
 
The Ring programming language version 1.5.1 book - Part 56 of 180
The Ring programming language version 1.5.1 book - Part 56 of 180The Ring programming language version 1.5.1 book - Part 56 of 180
The Ring programming language version 1.5.1 book - Part 56 of 180
 
Scala Under the Hood / ScalaSwarm
Scala Under the Hood / ScalaSwarmScala Under the Hood / ScalaSwarm
Scala Under the Hood / ScalaSwarm
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Openframworks x Mobile
Openframworks x MobileOpenframworks x Mobile
Openframworks x Mobile
 
Matlab project
Matlab projectMatlab project
Matlab project
 
Tu1
Tu1Tu1
Tu1
 

Similar to Codemania101: The Present, Past and Future of Asynchronous Programming in Python

Similar to Codemania101: The Present, Past and Future of Asynchronous Programming in Python (20)

Compiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 2 | Declarative Syntax DefinitionCompiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 2 | Declarative Syntax Definition
 
Os lab final
Os lab finalOs lab final
Os lab final
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
Programming simple games with a raspberry pi and
Programming simple games with a raspberry pi andProgramming simple games with a raspberry pi and
Programming simple games with a raspberry pi and
 
Python meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/OPython meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/O
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in Scala
 
4. functions
4. functions4. functions
4. functions
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
Hargun
HargunHargun
Hargun
 
BUILDING APPS WITH ASYNCIO
BUILDING APPS WITH ASYNCIOBUILDING APPS WITH ASYNCIO
BUILDING APPS WITH ASYNCIO
 
Implementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & CImplementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & C
 
C-LOOP-Session-2.pptx
C-LOOP-Session-2.pptxC-LOOP-Session-2.pptx
C-LOOP-Session-2.pptx
 
cats.effect.IO - Scala Vienna Meetup February 2019
cats.effect.IO - Scala Vienna Meetup February 2019cats.effect.IO - Scala Vienna Meetup February 2019
cats.effect.IO - Scala Vienna Meetup February 2019
 
Presentation 6 (1).pptx
Presentation 6 (1).pptxPresentation 6 (1).pptx
Presentation 6 (1).pptx
 
C
CC
C
 
functions
functionsfunctions
functions
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
ZIO Queue
ZIO QueueZIO Queue
ZIO Queue
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

Codemania101: The Present, Past and Future of Asynchronous Programming in Python

  • 1. ASYNCHRONOUS PROGRAMMING IN PYTHON P R E S E N T, PA S T A N D F U T U R E O F www.prontotools.io Y O T H I N M U A N G S O M M U K
  • 2. Y O T H I N M U A N G S O M M U K A B O U T M E Pythonista
  • 3.
  • 4.
  • 5. Best in class productivity tools that enable smart and efficient marketing execution for small business
  • 6. B e t t e r M a r k e t i n g V i s i b i l i t y
  • 7.
  • 8. A S Y N C
  • 9. A programming technique where execution order is not known ahead of time
  • 10. W H Y W E N E E D T O K N O W A S Y N C P R O G R A M M I N G
  • 11. B E C A U S E I T M A K E Y O U R C O D E G O FA S T
  • 12. H O W FA S T
  • 13. B O B B Y F I S C H E R P L AY I N G 5 0 O P P O N E N T S S I M U LTA N E O U S LY, 1 9 6 4 S Y N C H R O N O U S C H E S S E X H I B I T I O N - 50 opponents - Fischer move in 5 seconds - Opponents move in 55 seconds - Game average 30 move pairs Assumptions: Each game runs for 30 minutes 50 sequential games would take 50 x 30 min = 1500 min = 25 hours
  • 14. B O B B Y F I S C H E R P L AY I N G 5 0 O P P O N E N T S S I M U LTA N E O U S LY, 1 9 6 4 A S Y N C H R O N O U S C H E S S E X H I B I T I O N - Fischer moves on first game - While opponent thinks, he moves
 on second game, the third, fourth… - A move on all 50 game takes him
 50 x 5 sec = 250 sec = 4 min - After he complete the round, the 
 first game is ready for his next move - 50 games are completed in
 4 min x 30 = 120 min = 2 hour
  • 15. C O R O U T I N E
  • 16. – W I K I P E D I A C o r o u t i n e s a r e c o m p u t e r p r o g r a m components that generalize subroutines for nonpreemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations.
  • 17. coroutines are functions whose execution you can pause
  • 18. E V E N T L O O P
  • 19. – W I K I P E D I A Event loop is a programming construct that waits for a dispatches events or messages in a program.
  • 20. A B R I E F H I S T O RY
  • 21. S I M P L E G E N E R AT O R P Y T H O N 2 . 2 ( P E P 2 5 5 ) - 1 8 M AY 2 0 0 1 Generator was born with “yield” expression make us able to generate iterator without holding memory upfront, all you need is memory for the current value def eager_range(up_to): sequence = [] index = 0 while index < up_to: sequence.append(index) Index += 1 return sequence
  • 22. S I M P L E G E N E R AT O R P Y T H O N 2 . 2 ( P E P 2 5 5 ) - 1 8 M AY 2 0 0 1 Generator was born with “yield” expression make us able to generate iterator without holding memory upfront, all you need is memory for the current value def lazy_range(up_to): index = 0 while index < up_to: yield index Index += 1
  • 23. C O R O U T I N E S V I A E N H A N C E D G E N E R AT O R S P Y T H O N 2 . 5 ( P E P 3 4 2 ) - 1 0 M AY 2 0 0 5 “send()” method on generator, that allowed us not only pause the generator, but also send back the value to generator def jumping_range(up_to): index = 0 while index < up_to: jump = yield index if jump is None: jump = 1 index += jump if __name__ == '__main__': iterator = jumping_range(5) print(next(iterator)) # 0 print(iterator.send(2)) # 2 print(next(iterator)) # 3
  • 24. S Y N TA X F O R D E L E G AT I N G T O A S U B G E N E R AT O R P Y T H O N 3 . 3 ( P E P 3 8 0 ) - 1 3 F E B 2 0 0 9 “yield from” expression for yield every value from iterator (generator) also able to chain generator together which made us able to bubble up and down the call stack without code having to do anything special def lazy_range(up_to): index = 0 def gratuitous_refactor(): nonlocal index while index < up_to: yield index index += 1 yield from gratuitous_refactor()
  • 25. S Y N TA X F O R D E L E G AT I N G T O A S U B G E N E R AT O R P Y T H O N 3 . 3 ( P E P 3 8 0 ) - 1 3 F E B 2 0 0 9 “yield from” expression for yield every value from iterator (generator) also able to chain generator together which made us able to bubble up and down the call stack without code having to do anything special def bottom(): return (yield 42) def middle(): return (yield from bottom()) def top(): return (yield from middle()) gen = top() value = next(gen) print(value) # 42
  • 26. P Y T H O N 3 . 4 ( P E P 3 1 5 6 ) - 1 2 D E C 2 0 1 2 Pluggable event loop made non blocking IO happen, but not actual asynchronous programming. It’s concurrent programming A S Y N C I O import asyncio loop = asyncio.get_event_loop() @asyncio.coroutine def hello(): print('Hello') yield from asyncio.sleep(3) print('World!') if __name__ == '__main__': loop.run_until_complete(hello())
  • 27. A S Y N C A WA I T P Y T H O N 3 . 5 ( P E P 4 9 2 ) - 9 A P R 2 0 1 5 Explicit asynchronous code there is no need to do “asyncio.coroutine” decorator with generator that return “yield from” anymore. Using “async def” syntax to define that function is a coroutine and use “await” or “return” for delegate the results like “yield from” import asyncio loop = asyncio.get_event_loop() async def hello(): print('Hello') await asyncio.sleep(3) print('World!') if __name__ == '__main__': loop.run_until_complete(hello())
  • 28. import asyncio async def compute(x, y): print(“Compute %s + %s …” % (x, y)) await asyncio.sleep(1.0) return x + y async def print_sum(x, y): result = await compute(x, y) print(“%s + %s = %s” % (x, y, result)) loop = asyncio.get_event_loop() loop.run_until_complete(print_sum(1, 2)) loop.close() A S Y N C A WA I T P Y T H O N 3 . 5 ( P E P 4 9 2 ) - 9 A P R 2 0 1 5 Explicit asynchronous code there is no need to do “asyncio.coroutine” decorator with generator that return “yield from” anymore. Using “async def” syntax to define that function is a coroutine and use “await” or “return” for delegate the results like “yield from”
  • 30. A S Y N C G E N E R AT O R P Y T H O N 3 . 6 ( P E P 5 2 5 ) - 2 8 J U L 2 0 1 6 Remove restriction to use await and yield in the same function body import asyncio loop = asyncio.get_event_loop() async def ticker(delay, to): for i in range(to) yield i await asyncio.sleep(delay) if __name__ == '__main__': loop.run_until_complete(ticker(5, 10))
  • 31. A S Y N C C O M P R E H E N S I O N S P Y T H O N 3 . 6 ( P E P 5 3 0 ) - 3 S E P 2 0 1 6 Introduce async for and await expression in all kinds of comprehensions result = [ i async for i in aiter() if i % 2 ] result = [ await fun() for fun in funcs if await condition() ]
  • 32. E V E N T L O O P P L U G G A B L E
  • 33. U V L O O P
  • 35. ▄▄▄▄▄ ▀▀▀██████▄▄▄ _______________ ▄▄▄▄▄ █████████▄ / ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Gotta go fast! | ▀▀█████▄▄ ▀██████▄██ | _________________/ ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/ ▀▀▀▄ ▀▀███ ▀ ▄▄ ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀ ▌ ▐▀████▐███▒▒▒▒▒▐██▌ ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀ ▀▀█████████▀ ▄▄██▀██████▀█ ▄██▀ ▀▀▀ █ ▄█ ▐▌ ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄ ▌ ▐ ▀▀▄▄▄▀ ▀▀▄▄▀ S A N I C from sanic import Sanic from sanic.response import son app = Sanic() @app.route(“/“) async def test(request): return json({“Hello”: “world”}) if __name__ == “__main__”: app.run(host=“0.0.0.0”, port=8000)
  • 36. J A P R O N T O
  • 37. H T T P S : / / M E D I U M . F R E E C O D E C A M P. C O M / M I L L I O N - R E Q U E S T S - P E R - S E C O N D - W I T H - P Y T H O N - 9 5 C 1 3 7 A F 3 1 9 J A P R O N T O
  • 38. J A P R O N T O import asyncio from japronto import Application app = Application() async def asynchronous(request): for i in range(1, 4): await asyncio.sleep(1) print(i, ‘seconds elapsed’) return request.Response(text=‘3 sec elapsed’) r = app.router r.add_route(‘/async’, asynchronous) app.run()
  • 39. T O K I O
  • 40.
  • 41. A LT E R N AT I V E
  • 42. C U R I O
  • 43. C U R I O - Using same async / await syntactic - Different approach from any other I / O Libraries - Simplicity
  • 44. O T H E R L I B R A R I E S
  • 45. - aiohttp - asyncpg - aiosmtp - aio.testing - aioelasticsearch - etc.
  • 47. R E F E R E N C E - https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/ - https://hackernoon.com/asynchronous-python-45df84b82434 - https://magic.io/blog/uvloop-blazing-fast-python-networking/ - https://github.com/channelcat/sanic - https://medium.freecodecamp.com/million-requests-per-second-with-python-95c137af319 - https://github.com/PyO3/tokio - Miguel Grinberg Asynchronous Python for the Complete Beginner PyCon 2017 - Yury Selivanov asyncawait and asyncio in Python 3 6 and beyond PyCon 2017