Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python asyncio

Python asyncio

Async I/O Programming with Python 3.4


Oursky Limited

June 07, 2015


  1. Async I/O Programming with Python 3.4

  2. We develop web application

  3. Web application is about communication

  4. Chat everywhere • Real time communication is everywhere • Holding

    large amount of conenction • Push message to large amount of clients
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. Demo https://github.com/oursky/pycon2015

  11. Common errors • Blocking the event loop with synchronous operation

    • Choose async library
  12. We are hiring টਓ • Python • golang • iOS

    • Android jobs@oursky.com jobs.oursky.com/tw
  13. Q & A