Slide 1

Slide 1 text

import asyncio Petr Viktorin [email protected] Pyvo, 2014-01-30

Slide 2

Slide 2 text

The problem Do something useful while waiting

Slide 3

Slide 3 text

The problem Do something useful while waiting (for IO)

Slide 4

Slide 4 text

The problems Do something useful while waiting (for IO) Do several things at the same time

Slide 5

Slide 5 text

Multiple processes import subprocess

Slide 6

Slide 6 text

Threads import threading

Slide 7

Slide 7 text

Greenlets Event loop Cooperative multitacking Monkeypatching pip install eventlet gevent

Slide 8

Slide 8 text

Callbacks Explicit cooperative multitasking pip install twisted

Slide 9

Slide 9 text

Help! Which do I use?

Slide 10

Slide 10 text

Compare: web frameworks

Slide 11

Slide 11 text

Compare: web frameworks Flask app

Slide 12

Slide 12 text

Compare: web frameworks Flask app Pyramid debug toolbar

Slide 13

Slide 13 text

Compare: web frameworks Flask app Pyramid debug toolbar Cherrypy logging

Slide 14

Slide 14 text

Compare: web frameworks Flask app Pyramid debug toolbar Cherrypy logging PEP 333 - Python Web Server Gateway Interface

Slide 15

Slide 15 text

Let's do the same for async!

Slide 16

Slide 16 text

Let's do the same for async! import asyncio

Slide 17

Slide 17 text

Let's do the same for async! import asyncio Python 3.4+

Slide 18

Slide 18 text

Let's do the same for async! import asyncio Python 3.4+ pip install tulip

Slide 19

Slide 19 text

Let's do the same for async! import asyncio Python 3.4+ pip install tulip Python 3.3+

Slide 20

Slide 20 text

asyncio Explicit cooperative multitasking

Slide 21

Slide 21 text

asyncio Explicit cooperative multitasking like Twisted

Slide 22

Slide 22 text

Global event loop

Slide 23

Slide 23 text

Global event loop Event loop policy

Slide 24

Slide 24 text

Callbacks

Slide 25

Slide 25 text

Callbacks call_soon(callback, args)

Slide 26

Slide 26 text

Callbacks call_soon(callback, args) call_later(delay, cb, *args)

Slide 27

Slide 27 text

Callbacks call_soon(callback, args) call_later(delay, cb, *args) What is time?

Slide 28

Slide 28 text

Callback style import asyncio def print_and_repeat(loop): print('Hello World') loop.call_later(2, print_and_repeat, loop) loop = asyncio.get_event_loop() loop.call_soon(print_and_repeat, loop) loop.run_forever()

Slide 29

Slide 29 text

Futures

Slide 30

Slide 30 text

Futures Placeholders for a result

Slide 31

Slide 31 text

Futures Placeholders for a result future.result()

Slide 32

Slide 32 text

Futures Placeholders for a result future.result() future.set_result()

Slide 33

Slide 33 text

Futures Placeholders for a result future.result() future.set_result() future.set_exception()

Slide 34

Slide 34 text

Futures Placeholders for a result future.result() future.set_result() future.set_exception() future.add_done_callback()

Slide 35

Slide 35 text

Futures Placeholders for a result future.result() future.set_result() future.set_exception() future.add_done_callback() concurrent.futures

Slide 36

Slide 36 text

Locks, Semaphores, Queues

Slide 37

Slide 37 text

Locks, Semaphores, Queues http://xkcd.com/927/

Slide 38

Slide 38 text

Couroutines & Tasks import asyncio @asyncio.coroutine def greet_every_two_seconds(): while True: print('Hello World') yield from asyncio.sleep(1) print('How are you?') yield from asyncio.sleep(1) loop = asyncio.get_event_loop() loop.run_until_complete(greet_every_two_seconds())

Slide 39

Slide 39 text

Transports & Protocols Transport: TCP, UDP, SSL Protocol: HTTP, FTP, IRC

Slide 40

Slide 40 text

Echo client import asyncio class EchoClient(asyncio.Protocol): message = 'This is the message. It will be echoed.' def connection_made(self, transport): transport.write(self.message.encode()) print('data sent: {}'.format(self.message)) def data_received(self, data): print('data received: {}'.format(data.decode())) def connection_lost(self, exc): print('server closed the connection') asyncio.get_event_loop().stop() loop = asyncio.get_event_loop() coro = loop.create_connection(EchoClient, '127.0.0.1', 8888) loop.run_until_complete(coro) loop.run_forever() loop.close()

Slide 41

Slide 41 text

Echo server import asyncio class EchoServer(asyncio.Protocol): def connection_made(self, transport): peername = transport.get_extra_info('peername') print('connection from {}'.format(peername)) self.transport = transport def data_received(self, data): print('data received: {}'.format(data.decode())) self.transport.write(data) # close the socket self.transport.close() loop = asyncio.get_event_loop() coro = loop.create_server(EchoServer, '127.0.0.1', 8888) server = loop.run_until_complete(coro) print('serving on {}'.format(server.sockets[0].getsockname())) try: loop.run_forever() except KeyboardInterrupt: print("exit") finally: server.close() loop.close()

Slide 42

Slide 42 text

? Petr Viktorin @encukou.cz @encukou [email protected] github.com/encukou Slides available under the Creative Commons Attribution-ShareAlike 3.0 http://creativecommons.org/licenses/by-sa/3.0/

Slide 43

Slide 43 text

Sources & links PEP 3156 - Asynchronous IO Support Rebooted: the "asyncio" Module PEP 3148 - futures - execute computations asynchronously PEP 3153 (Superseded) - Asynchronous IO support http://docs.python.org/3.4/library/asyncio.html Youtube: Guido van Rossum - Tulip: Async I/O for Python 3