WebCamp 2016: Python. Михаил Бегерский: Использование asyncio-стека...

Post on 13-Apr-2017

169 views 3 download

Transcript of WebCamp 2016: Python. Михаил Бегерский: Использование asyncio-стека...

1

About

Python developer @ DataRobot

2

Benchmark [1]

3

Benchmark

4

SQLAlchemy

6

Faster postgre client?

$ pip install asyncpg

7

Mongo? [2]

8

aiohttp_jinja2

9

aiohttp_debugtoolbar

10

aiohttp_debugtoolbar

$ pip install aiohttp_debugtoolbar

11

aiohttp_debugtoolbar

12

aiohttp_debugtoolbar

13

aiohttp_debugtoolbar

14

aiohttp_session

16

aiohttp_security

18

aiohttp_security

19

Slowloris

$ nmap --script http-slowloris-check 127.0.0.1 -p 8080

20

Web-sockets [3]

21

Middleware

22

Middleware

23

aiohttp client [4]

24

aiohttp_admin

25

aiohttp_admin

26

Other tools [5]

● aiohttp_runserver

● aiohttp_cors

● aiohttp_traversal

● aiothrottle

● aiohttp_mako

● and more ...

27

Project structure

28

Debug

$ export PYTHONASYNCIODEBUG=1

● explicit loop

● releasing resources

● awaititng coroutines

$ python script.py -Wdefault

29

Debug

$ Task was destroyed but it is pending!task: <Task pending coro=<fetch_bad() running at demo.py:14> wait_for=<Future pending cb=[Task._wakeup(), BaseSelectorEventLoop._sock_connect_done(10)()]>>

30

Debug

$ ConnectionResetError: [Errno 54] Connect call failed ('127.0.0.1', 8080)

$ OSError: [Errno 24] Too many open files

31

Debug

32

Testing

33

Testing

34

Testing [6]

35

Go production!

$ gunicorn app_module:app --bind localhost:8080 --worker-class aiohttp.worker.GunicornWebWorker

36

Graceful shutdown

37

Production architecture 38

Thanks for your attention

https://github.com/kpidata/webcamp16

bmwant@gmail.com

@bmwant

bmwlog.pp.ua

40