asynchronous applications in ruby. roman gorel
DESCRIPTION
Kiev-rb#2TRANSCRIPT
![Page 1: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/1.jpg)
![Page 2: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/2.jpg)
Асинхронные приложения в руби
Роман Горель
![Page 3: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/3.jpg)
Вебсервер
Приложение
БД
Запрос
Ваше приложение
![Page 4: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/4.jpg)
Вебсервер
Приложение
БД
Запрос
Остальные запросы – в очередь
![Page 5: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/5.jpg)
Фронтэнд-сервер
Приложение
БД
Запрос
Приложение Приложение
Нужно больше инстансов!
![Page 6: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/6.jpg)
Фронтэнд-сервер
Приложение
БД
Приложение
Приложение
Фронтэнд-сервер
Приложение
БД
Приложение
Приложение
Балансер
Сервер 1
Сервер 2
Докупим еще серверов
![Page 7: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/7.jpg)
Синхронная модель
![Page 8: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/8.jpg)
Многопоточность / многопроцессовость
![Page 9: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/9.jpg)
Многопроцессовость
● Параллельное выполнение
● Легко управлять
● Дорогое переключение контекста
● Ресурсоемкость
● Параллельное выполнение
● Легко управлять
![Page 10: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/10.jpg)
Многопоточность
● Потокобезопасность
● GIL
● Параллельное выполнение
● Дешевле переключение контекста
● Меньшее потребление ресурсов
![Page 11: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/11.jpg)
Запрос Поток
Запрос Поток
Запрос Поток
Пул
10k
Очередь
Проблема C10k
![Page 12: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/12.jpg)
Асинхронная модель
![Page 13: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/13.jpg)
Блокировка в синхронной модели
![Page 14: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/14.jpg)
Асинхронные технологии
![Page 15: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/15.jpg)
Reactor pattern
![Page 16: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/16.jpg)
EventMachine
![Page 17: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/17.jpg)
Простой веб-сервер
![Page 18: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/18.jpg)
ab -c 5 -n 10 "http://127.0.0.1:3000/"
![Page 19: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/19.jpg)
EM deferred
![Page 20: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/20.jpg)
Sync vs Deferred
![Page 21: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/21.jpg)
Свои Deferred классы
![Page 22: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/22.jpg)
Спагетти из коллбеков
![Page 23: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/23.jpg)
Файберы приходят на помощь
![Page 24: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/24.jpg)
EM-Synchrony
![Page 25: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/25.jpg)
Немного магии EM-Synchrony
![Page 26: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/26.jpg)
Поддерживаемые клиенты
● Mysql2● Activerecord (via mysql2)● Mongo / Mongoid● Memcached● Redis
![Page 27: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/27.jpg)
Как же применить это к вебу?
![Page 28: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/28.jpg)
Sinatra::Async
![Page 29: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/29.jpg)
Маунтим к рельсам
● Gemfile
● Routes.rb
![Page 30: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/30.jpg)
![Page 31: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/31.jpg)
Проверим асинхронность
Rails controller, sleep(1)
AsyncTest
![Page 32: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/32.jpg)
Почитать
● Илья Григорик - http://www.igvita.com/● Мануал по Twisted
http://krondo.com/?page_id=1327 ● http://everburning.com/news/eventmachine-intro
ductions/
![Page 33: Asynchronous Applications in Ruby. Roman Gorel](https://reader034.vdocuments.net/reader034/viewer/2022051323/54b663324a79598f6d8b4898/html5/thumbnails/33.jpg)