Asynchronous Web with Python

Przemek Lewandowski

@haxoza

What is "asynchronous"?

Asynchronous communication, transmission of data without the use of an external clock signal, where data can be transmitted intermittently rather than in a steady stream.

Wikipedia

In other words:

We don't want to wait and block our application awaiting the response.

You know JavaScript? ;)

What blocks our apps?

  • I/O
    • Networking
    • Disk operations
  • Computation
  • Shared objects
  • System calls

Possible solutions

(in Python)

Non-blocking solutions

  • Processes
  • Threads
  • Low-level select-like
  • Greenlets / Coroutines / Green Threads

Processes

are

expensive

Threads?

CPython threads are managed by OS

Threads are still expensive and not worth due to GIL

select?

So C-like solution!

Green threads

to the rescue!

Why user-space threads?

  • Light weight and fast
  • One code path at a time
  • No condition races
  • Similar API to threads
  • High-level API
    • Event loop based
    • select-like for blocking I/O

But...

wrong code can block the process

Why for web servers?

  • Fast
  • Memory efficient
  • Number of connections
    is virtually unlimited

Python implementations

  • Asyncio
  • Gevent
  • Tornado
  • Twisted
  • Eventlet
  • Greenlet
  • ...

asyncio is

low-level API

Gevent is general purpose framework
(focused on networking
and synchronous API)

Tornado is a web framework

Twisted is
event-driven networking engine

Your choice should depend on a problem

Case study

Web application that shows tweets in realtime

Gevent vs Tornado

Tweepy library

for Twitter Streaming API

Tweepy uses

  • uses blocking i/o
  • threads

Gevent

  • Greenlets
  • Threads monkey patching
  • Synchronous API
  • One process

Tornado

  • Event loop
  • Bad idea to run with threads
  • Asynchronous API
  • Two processes

Let's dive into code

https://bitbucket.org/sunscrapers/twfeed

 

https://github.com/haxoza/tweefee

Thanks!

Questions?

@haxoza

Resources

  • http://www.artima.com/weblogs/viewpost.jsp?thread=214235
  • http://stackoverflow.com/questions/200469/what-is-the-difference-between-a-process-and-a-thread
  • http://stackoverflow.com/questions/12758952/green-threads-and-thread-in-python
  • http://vaidik.in/blog/understanding-non-blocking-io-with-python-part-1.html
  • Gevent & Tornado docs

Asynchronous Web in Python

By Przemek Lewandowski

Asynchronous Web in Python

  • 2,238