Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
How async and await ended up in Python
Search
Pavlin Gergov
July 27, 2018
0
320
How async and await ended up in Python
EuroPython 2018, Edinbugh
Pavlin Gergov
July 27, 2018
Tweet
Share
Featured
See All Featured
The Limits of Empathy - UXLibs8
cassininazir
1
220
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
Mobile First: as difficult as doing things right
swwweet
225
10k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
120
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
190
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.2k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
200
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
So, you think you're a good person
axbom
PRO
2
1.9k
Transcript
How async and await ended up in Python EuroPython 2018,
Edinburgh
Hello (: https:/ /www.hacksoft.io Django React Twitter: @pgergov_
None
Python is synchronous
Request Response
None
None
Threads
Asynchronous
asyncio
hello_asyncio.py import asyncio async def hello_world_coroutine(delay): print('Hello') await asyncio.sleep(delay) print(f'World,
with delay: {delay}') loop = asyncio.get_event_loop() loop.create_task(hello_world_coroutine(1)) loop.create_task(hello_world_coroutine(2)) loop.run_forever()
Python 3.6.3 Hell Hello World, with delay 1 World, with
delay 2
hello_asyncio.py import asyncio async def hello_world_coroutine(delay): print('Hello') await asyncio.sleep(delay) print(f'World,
with delay: {delay}') loop = asyncio.get_event_loop() loop.create_task(hello_world_coroutine(1)) loop.create_task(hello_world_coroutine(2)) loop.run_forever()
Coroutines are computer-program components that generalize subroutines for non-preemptive multitasking,
by allowing multiple entry points for suspending and resuming execution at certain locations Wikipedia
None
None
How async and await ended up in python Order of
execution - raise and return Iterable and Iterator Generator functions - yield and .send Python 3.3 yield from Definition for coroutine in Python Python 3.4, @asyncio.coroutine Python 3.5, @types.coroutine async and await
Order of execution
throw_exception.py def throw_exception(): print('Will raise an Exception') raise Exception('Raised inside
`throw_exception`') print('This message won\'t be printed')
hello_world.py def hello_world(): print('Hello world!') return 42 print('This message will
never be printed')
yield
Iterable __iter__ for x in iterable:
Iterator __next__ __iter__
class MyIterator: def __init__(self): self.counter = 1 def __iter__(self): return
self def __next__(self): counter = self.counter if counter > 3: raise StopIteration self.counter += 1 return counter iterator.py
iterator = MyIterator() next(iterator) # returns 1 next(iterator) # returns
2 next(iterator) # returns 3 next(iterator) # raises StopIteration
iterator = MyIterator() for numb in iterator: print(numb) 1 2
3
Generator function
def generator_function(): print('Going to yield first value') yield 1 print('Yielding
second value') yield 2 generator_function.py
gen = generator_function() next(gen) ‘Going to yield first value’ 1
next(gen) ‘Yielding second value’ 2 next(gen) # raises StopIteration
.send
def generator_send(): print('Going to yield a value') received = yield
42 print(f'Received {received}') generator_send.py
gen = generator_function() gen.send(None) ‘Going to yield value’ 42 gen.send(‘Hello
generator’) ‘Received Hello generator’ StopIteration is raised
yield from for x in iterator: yield x yield from
iterator Python 3.3
def first_generator(): yield 1 print('In the middle of first generator')
yield 2 def second_generator(): gen = first_generator() yield from gen print('In the middle of second generator') yield 3 yield_from.py
gen = second_generator() next(gen) 1 next(gen) In the middle of
first generator 2 next(gen) In the middle of second generator 3 next(gen) # raises StopIteration
Coroutines are computer-program components that generalize subroutines for non-preemptive multitasking,
by allowing multiple entry points for suspending and resuming execution at certain locations Wikipedia
definition of coroutine in Python Python 3.3
@asyncio.coroutine Python 3.4
Python 3.6.3 import asyncio async def hello_world_coroutine(delay): print('Hello') await asyncio.sleep(delay)
print(f'World, with delay: {delay}') loop = asyncio.get_event_loop() loop.create_task(hello_world_coroutine(1)) loop.create_task(hello_world_coroutine(2)) loop.run_forever()
Python 3.4 import asyncio @asyncio.coroutine def hello_world_coroutine(delay): print('Hello') yield from
asyncio.sleep(delay) print(f'World, with delay: {delay}') loop = asyncio.get_event_loop() loop.create_task(hello_world_coroutine(1)) loop.create_task(hello_world_coroutine(2)) loop.run_forever()
@types.coroutine Python 3.5
async Python 3.5 async def
await Python 3.5
__await__
Conclusion
What’s next?
The superpowers of async and await
Thank you Here’s a kiss for you! https:/ /github.com/pgergov/europython-2018