How async and await ended up in Python EuroPython 2018, Edinburgh - - PowerPoint PPT Presentation

how async and await ended up in python
SMART_READER_LITE
LIVE PREVIEW

How async and await ended up in Python EuroPython 2018, Edinburgh - - PowerPoint PPT Presentation

How async and await ended up in Python EuroPython 2018, Edinburgh Hello (: https:/ /www.hacksoft.io Django React Twitter: @pgergov_ Python is synchronous Request Response Threads Asynchronous asyncio hello_asyncio.py import asyncio


slide-1
SLIDE 1

How async and await ended up in Python

EuroPython 2018, Edinburgh

slide-2
SLIDE 2

Hello (:

https:/ /www.hacksoft.io

Django React

Twitter: @pgergov_

slide-3
SLIDE 3
slide-4
SLIDE 4

Python is synchronous

slide-5
SLIDE 5

Request Response

slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8

Threads

slide-9
SLIDE 9

Asynchronous

slide-10
SLIDE 10

asyncio

slide-11
SLIDE 11

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

Python 3.6.3

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

slide-13
SLIDE 13

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

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
slide-16
SLIDE 16
slide-17
SLIDE 17

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

Order of execution

slide-19
SLIDE 19

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

hello_world.py

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

slide-21
SLIDE 21

yield

slide-22
SLIDE 22

Iterable __iter__ for x in iterable:

slide-23
SLIDE 23

Iterator __next__ __iter__

slide-24
SLIDE 24

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

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


slide-26
SLIDE 26

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

slide-27
SLIDE 27

Generator function

slide-28
SLIDE 28

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

generator_function.py

slide-29
SLIDE 29

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

.send

slide-31
SLIDE 31

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

generator_send.py

slide-32
SLIDE 32

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

yield from

for x in iterator: yield x

yield from iterator

Python 3.3

slide-34
SLIDE 34

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

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

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

definition of coroutine in Python

Python 3.3

slide-38
SLIDE 38

@asyncio.coroutine

Python 3.4

slide-39
SLIDE 39

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

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

@types.coroutine

Python 3.5

slide-42
SLIDE 42

async Python 3.5 async def

slide-43
SLIDE 43

await Python 3.5

slide-44
SLIDE 44

__await__

slide-45
SLIDE 45

Conclusion

slide-46
SLIDE 46

What’ s next?

slide-47
SLIDE 47

The superpowers of async and await

slide-48
SLIDE 48

Thank you

Here’ s a kiss for you!

https:/ /github.com/pgergov/ europython-2018