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
Tweet

More Decks by Oursky Limited

Other Decks in Programming

Transcript

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

    large amount of conenction • Push message to large amount of clients
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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