why i still develop synchronous web in the asyncio era · pdf filewhy i still develop...

79
Why I still develop synchronous web in the asyncIO era April 7th, 2017 Giovanni Barillari - pycon otto - Firenze, Italy

Upload: trinhkiet

Post on 26-Mar-2018

228 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Why I still develop synchronous

web in the asyncIO era

April 7th, 2017

Giovanni Barillari - pycon otto - Firenze, Italy

Page 2: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Who am I?

I’m Gio!pronounced as Joe

trust me, I’m a physicist :)

code principally in Python, Ruby, Java, Javascript

work with web applications since 2012

CTO @ Sellf since 2016

Page 3: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

I love Open Source

contributor of web2py framework since 2012

maintainer of pydal library since 2015

author of weppy framework since 2014

Page 4: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Disclaimer

This is quite a subjective talk

I consider the need of a database in web development

Page 5: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

asynchronous IO

approach used to achieve concurrency by allowingprocessing to continue while responses from IO operations

are still being waited upon

Page 6: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

the event loop

asyncio programming is heavily centered on the notion ofan event loop, which in its most classic form uses callbackfunctions that receive a call once their corresponding IO

request has data available

Page 7: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 8: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 9: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

the asyncio era

Page 10: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

started with javascript

Page 11: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Javascript was designed to be a client side scriptinglanguage for browsers.

Browsers, like any other GUI app, are essentially eventmachines. All they do is respond to user-initiated events.

Page 12: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

then it comes node

and Javascript became a server side language.

Reason for its success: it fully embraces the event-drivenprogramming paradigm that client-side programmers are

already well-versed in and comfortable with.

Page 13: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 14: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

the non-blocking IO approach appropriate for the classiccase of lots of usually asleep or arbitrarily slow connections

became the de facto style in which all web-orientedsoftware should be written.

Page 15: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

we all hate threads

asynchronous programming being used to criticisemultithreaded programming since:

threads are expensive to create and maintain in anapplicationthreaded programming is difficult and non-deterministic

Page 16: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

the python world

continued confusion over what the GIL does and does notdo provided a fertile land for the async model to take root

strongly

Page 17: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 18: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Since Python 3.3 we moved from the implicit async IOparadigm offered by eventlet and gevent to the futures

and coroutines concepts of the asyncio module.

Page 19: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 20: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

are you sure?

Page 21: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

the throughput mith

Page 22: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

since you avoid the wait for I/O context switches,

asynchronous programming styles are innately

superior for concurrent performance in nearly all

cases.

Page 23: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Are you sure your application is I/O bound?

Page 24: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

I/O Bound refers to a condition in which the time it

takes to complete a computation is determined

principally by the period spent waiting for

input/output operations to be completed.

Page 25: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Are you sure context-switching is the bottleneck in yourreal world application?

Page 26: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Python is slow

Page 27: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

I mean REALLY slow

Page 28: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Web applications deal with databases

communication with the database takes up a

majority of the time spent in a database-centric

application

This is a common wisdom in compiled languages, butPython is very slow, compared to such systems.

Page 29: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

image made by zzzeek

Page 30: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

asyncio is slow

Insert into postgres a few million rows:

Python 2.7.8 threads (22k r/sec, 22k r/sec) Python 3.4.1 threads (10k r/sec, 21k r/sec) Python 2.7.8 gevent (18k r/sec, 19k r/sec) Python 3.4.1 asyncio (8k r/sec, 10k r/sec)

benchmarks made by zzzeek

Page 31: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

uvloop

benchmarks made by magicstack

Page 32: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

but these are just TCP connections

Page 33: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

dude wait, the magicstack guys also published asyncpg!

Page 34: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

where the core parsing is written in Cython

and sadly is not DBAPI v2 compatible (PEP249)

Page 35: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

web development is something more than network

Page 36: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

what are we benchmarking?

Page 37: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

the benchmarks’ fairy dust

Page 38: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

benchmarks from Falcon website

benchmarks made by TechEmpower

Page 39: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

we should stop looking just at numbers

Page 40: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 41: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 42: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

let’s make a pointless benchmark

Page 43: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

i5-6600k - OSX 10.11.6 - docker 17.03.0 - python 3.6.0 - wrk -d 15 -c [8-128] -t 4

serialize {“message”: “Hello, World!”} in json

Page 44: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

let’s make a realistic benchmark

Page 45: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

i5-6600k - OSX 10.11.6 - docker 17.03.0 - python 3.6.0 - wrk -d 15 -c [8-128] -t 4

load 20 records from postgres and serialize in json

Page 46: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 47: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

when you do benchmarks, be sure on what you’re actuallybenchmarking

Page 48: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

the json serialization benchmark on sanic equalsbenchmarking:

MagicStack’s httptools library vs gunicorn/uwsgi HTTPparsingujson vs standard json library

Page 49: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

which are faster independently of asyncio

Page 50: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

the code simplicity

Page 51: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

threads are bad. asyncio code is more explicit and

you’ll have fewer bugs in your program.

Page 52: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

The principle is basically:

I want context switches syntactically explicit in my

code. If they aren’t, reasoning about it is

exponentially harder.

Page 53: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

In practice, you’ll end up with so many yield from orawait lines in your code that you end up with

well, I guess I could context switch just about

anywhere

Which is the problem you were trying to avoid in the firstplace.

Page 54: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

def get_account_data(): user = database.get_user() preferences = database.get_user_preferences(user) post_count = database.get_post_count(user) return locals()

Page 55: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

async def get_account_data(): user = await database.get_user() preferences = await database.get_user_preferences(user) post_count = await database.get_post_count(user) return locals()

Page 56: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 57: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

forget about thread locals

Page 58: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

from framework import request, response, session

def message(value): return {'message': value, 'page': request.page}

@app.route('/foo') def foo(): return message('foo')

@app.route('/bar') def bar(): return message('bar')

Page 59: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

def message(value, request): return {'message': value, 'page': request.page}

@app.route('/foo') async def foo(request, response, session): return message('foo', request)

@app.route('/bar') async def bar(request, response, session): return message('bar', request)

Page 60: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

your code is just less DRY

Page 61: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

are we re-inventing the wheel?

Page 62: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Remember tornado?

Page 63: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

aiohttp, muffin, Kyoukai, sanic..

can you use any of these in production to write a real app?

Page 64: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

are we just moving the dust?

Page 65: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Before asyncio these were your 2 best friends

nginx

uwsgi

Page 66: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

why?

nginx use an event loop to process requests

is pure C

Page 67: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

with asyncio you would use gunicorn or other wsgi/asgiservers

and I still ask myself

is better to put it behind nginx or not?

Page 68: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

does it mean we’re moving the HTTP stack to python code?

Page 69: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization
Page 70: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

you saying you don’t use asyncio?

Page 71: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Of course I use it.

Page 72: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

when I do HTTP requests

@app.register('/oauth/{provider}') def oauth(request): provider = request.match_info.get('provider') client, _ = yield from app.ps.oauth.login(provider, request) user, data = yield from client.user_info() url = app.cfg['MOON_HOST'] + '/v1/oauth/' + provider resp = yield from aiohttp.request( 'POST', url, data=json.dumps({'user': user, 'data': data})) redir_url = app.cfg['APP_HOST'] + '/?' rv = yield from resp.json() redir_url += urlencode(rv) raise aiohttp.web.HTTPFound(redir_url)

Page 73: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

summing up

Page 74: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

asyncio is awesome compared to the nodejs world

uvloop is just amazing

with python 3.6 asyncio seems pretty stable

pypy started support of async code in the latest rc

Page 75: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

concurrency doesn’t mean things go faster

there’s no need to asyncify everything

avoid Hipe Driven Development

Page 76: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

async or not, the performance of your application highlydepends on your application code

Page 77: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

The future is bright, but we’re not there yet

Page 78: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

I still see more cons rather than pros into turning webdevelopment async

Page 79: Why I still develop synchronous web in the asyncIO era · PDF fileWhy I still develop synchronous web in the asyncIO era April 7th, ... Ruby, Java, Javascript ... the json serialization

Thank you.

Let’s discuss.