Slide 1

Slide 1 text

How async and await ended up in Python EuroPython 2018, Edinburgh

Slide 2

Slide 2 text

Hello (: https:/ /www.hacksoft.io Django React Twitter: @pgergov_

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Python is synchronous

Slide 5

Slide 5 text

Request Response

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Threads

Slide 9

Slide 9 text

Asynchronous

Slide 10

Slide 10 text

asyncio

Slide 11

Slide 11 text

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()

Slide 12

Slide 12 text

Python 3.6.3 Hell Hello World, with delay 1 World, with delay 2

Slide 13

Slide 13 text

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()

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Order of execution

Slide 19

Slide 19 text

throw_exception.py def throw_exception(): print('Will raise an Exception') raise Exception('Raised inside `throw_exception`') print('This message won\'t be printed')

Slide 20

Slide 20 text

hello_world.py def hello_world(): print('Hello world!') return 42 print('This message will never be printed')

Slide 21

Slide 21 text

yield

Slide 22

Slide 22 text

Iterable __iter__ for x in iterable:

Slide 23

Slide 23 text

Iterator __next__ __iter__

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

iterator = MyIterator() next(iterator) # returns 1 next(iterator) # returns 2 next(iterator) # returns 3 next(iterator) # raises StopIteration


Slide 26

Slide 26 text

iterator = MyIterator() for numb in iterator: print(numb) 1 2 3

Slide 27

Slide 27 text

Generator function

Slide 28

Slide 28 text

def generator_function(): print('Going to yield first value') yield 1 print('Yielding second value') yield 2 generator_function.py

Slide 29

Slide 29 text

gen = generator_function() next(gen) ‘Going to yield first value’ 1 next(gen) ‘Yielding second value’ 2 next(gen) # raises StopIteration


Slide 30

Slide 30 text

.send

Slide 31

Slide 31 text

def generator_send(): print('Going to yield a value') received = yield 42 print(f'Received {received}') generator_send.py

Slide 32

Slide 32 text

gen = generator_function() gen.send(None) ‘Going to yield value’ 42 gen.send(‘Hello generator’) ‘Received Hello generator’ StopIteration is raised

Slide 33

Slide 33 text

yield from for x in iterator: yield x yield from iterator Python 3.3

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

definition of coroutine in Python Python 3.3

Slide 38

Slide 38 text

@asyncio.coroutine Python 3.4

Slide 39

Slide 39 text

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()

Slide 40

Slide 40 text

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()

Slide 41

Slide 41 text

@types.coroutine Python 3.5

Slide 42

Slide 42 text

async Python 3.5 async def

Slide 43

Slide 43 text

await Python 3.5

Slide 44

Slide 44 text

__await__

Slide 45

Slide 45 text

Conclusion

Slide 46

Slide 46 text

What’s next?

Slide 47

Slide 47 text

The superpowers of async and await

Slide 48

Slide 48 text

Thank you Here’s a kiss for you! https:/ /github.com/pgergov/europython-2018