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

How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

May 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

How async and await ended up in Python

EuroPython 2018, Edinburgh

Page 2: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Hello (:

https://www.hacksoft.io

Django

React

Twitter: @pgergov_

Page 3: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python
Page 4: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Python is synchronous

Page 5: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Request

Response

Page 6: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python
Page 7: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python
Page 8: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Threads

Page 9: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Asynchronous

Page 10: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

asyncio

Page 11: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

hello_asyncio.pyimport 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()

Page 12: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Python 3.6.3

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

Page 13: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

hello_asyncio.pyimport 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()

Page 14: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 15: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python
Page 16: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python
Page 17: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 18: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Order of execution

Page 19: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

throw_exception.py

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

Page 20: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

hello_world.py

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

Page 21: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

yield

Page 22: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Iterable

__iter__

for x in iterable:

Page 23: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Iterator

__next__

__iter__

Page 24: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 25: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 26: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

iterator = MyIterator()

for numb in iterator: print(numb)

1 2 3

Page 27: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Generator function

Page 28: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

generator_function.py

Page 29: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 30: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

.send

Page 31: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

generator_send.py

Page 32: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 33: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

yield from

for x in iterator: yield x

yield from iterator

Python 3.3

Page 34: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 35: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 36: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 37: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

definition of coroutine in Python

Python 3.3

Page 38: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

@asyncio.coroutine

Python 3.4

Page 39: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

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

Page 40: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Python 3.4import 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()

Page 41: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

@types.coroutine

Python 3.5

Page 42: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

async

Python 3.5

async def

Page 43: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

await

Python 3.5

Page 44: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

__await__

Page 45: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Conclusion

Page 46: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

What’s next?

Page 47: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

The superpowers of async and await

Page 48: How async and await ended up in Python...How async and await ended up in python Order of execution - raise and return Iterable and Iterator Generator functions - yield and .send Python

Thank you

Here’s a kiss for you!

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