Slide 1

Slide 1 text

Async I/O Programming with Python 3.4

Slide 2

Slide 2 text

We develop web application

Slide 3

Slide 3 text

Web application is about communication

Slide 4

Slide 4 text

Chat everywhere • Real time communication is everywhere • Holding large amount of conenction • Push message to large amount of clients

Slide 5

Slide 5 text

C10k Problem • C10k (handle a large number of clients at the same time) https://en.wikipedia.org/wiki/C10k_problem • One thread per each connection is not practical • Use too much memory • Context switch overhead out weight payload • Processing I/O with event loop is more efficient

Slide 6

Slide 6 text

What is event loop? • Single threaded execution • Listen to I/O events (e.g. network socket) • Schedule callbacks to be called when certain event occurs • Event processed asynchronously

Slide 7

Slide 7 text

What is python asyncio • PEP 3153 - created Dec 2013 • Part of standard library since Python 3.4 • You can install from PyPi asyncio for Python 3.3 • Writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources

Slide 8

Slide 8 text

Comparing with gevent • Gevent mock all network io, which may yield to unnessacary callback. • Asyncio is more explicity • Developer are in more control on when to use syncio and asyncio in program flow

Slide 9

Slide 9 text

What is a coroutine • Coroutine is the way you write async code without writing callback function • Construct a coroutine is like construct a generator, no code execution on creation time • Coroutine is executed when event loop iterate over it • Coroutine is suspended when waiting for event

Slide 10

Slide 10 text

Demo https://github.com/oursky/pycon2015

Slide 11

Slide 11 text

Common errors • Blocking the event loop with synchronous operation • Choose async library

Slide 12

Slide 12 text

We are hiring টਓ • Python • golang • iOS • Android jobs@oursky.com jobs.oursky.com/tw

Slide 13

Slide 13 text

Q & A