SlideShare ist ein Scribd-Unternehmen logo
1 von 20
uWSGI
Swiss army knife for your Python web apps
TOMISLAV RAŠETA
@traseta
me
 scribbling and doodling on *IX operating systems for 10+ years
 working as sysadmin/DevOps with a hint of networking
 tools of the trade
 nginx apache tomcat (ugh!)
 python php java (ugh!)
 postgresql mysql
 memcached redis mongo
 not a developer, sorry :)
before we begin
 know your environment
 server specs (cores, memory, bandwidth)
 virtualenv or global
 know your application
 database pooling agents and latencies
 python version requirements (!)
 Linux kernel tunables
 limits.conf (file descriptors)
 ipv4.tcp_tw_reuse (not recycle)
 kernel.shmmax
 ipv4.ip_local_port_range
what it is
 full stack for building hosting services
 pluggable architecture
 mostly used as application server for Python apps using WSGI
 “WSGI is the Web Server Gateway Interface. It is a specification that
describes how web server communicates with web applications, and how
web applications can be chained together to process one request.”
 rules
 versatility
 performance
 low-resource usage
 reliability
 developed by @unbit (Italy)
 awesome docs
installation
 PyPI
 most recent versions, maintained by devs
 pip install uwsgi
 use virtualenv when possible
 watch out for dependencies - check what you actually need, the
compile errors/warnings are very descriptive
 distro packed versions are *always* outdated
 “from source” - please avoid :)
configuration
 choice of xml, ini, command-line arguments etc.
 read the docs carefully
 don’t reinvent the wheel
 "Please, turn on your brain and try to adapt shown configs to your
needs, or invent new ones."
configuration example (xml), start from ve
<uwsgi>
<chdir>/home/user/ve/app</chdir>
<wsgi-file>project.wsgi</wsgi-file>
<uid>user</uid>
<gid>user</gid>
<uwsgi-socket>/home/user/uwsgi/uwsgi.sock</uwsgi-socket>
<chmod-socket>600</chmod-socket>
<pidfile>/home/user/uwsgi/uwsgi.pid</pidfile>
<stats>/home/user/uwsgi/uwsgistats.sock</stats>
<buffer-size>8192</buffer-size>
<post-buffering>4096</post-buffering>
<memory-report/>
<daemonize>/home/user/uwsgi/daemon.log</daemonize>
<reload-mercy>20</reload-mercy>
<max-requests>500</max-requests>
<max-worker-lifetime>86400</max-worker-lifetime>
<socket-timeout>5</socket-timeout>
<disable-logging/>
<reload-on-rss>256</reload-on-rss>
<touch-reload>/home/user/ve/app/project.wsgi</touch-reload>
<enable-threads/>
<threads>1</threads>
<processes>100</processes>
<single-interpreter/>
<vacuum/>
</uwsgi>
start it up
 activate ve
 uwsgi --xmlconfig /home/user/uwsgi/uwsgi.xml
 configure and start nginx
location / {
include uwsgi_params;
uwsgi_pass unix:/home/wmaster/uwsgi/uwsgi.sock;
}
 ta-daa!
 tip: socket based pass doesn’t consume server TCP port usage = less
SYSCPU time on your server; you can use TCP but check your kernel
tunables before! (tcp_tw_reuse, ip_local_port_range
 tip2: you can also use mod_uwsgi and apache
cool conf stuff (I)
 harakiri (timeout)
 every request is timestamped
 If a master process finds a worker with a timestamp running more than
specified timeout it will kill it
 logs: “F*CK !!! i must kill myself (pid: 9984 app_id: 0)”
 reload-on-rss
 gracefully reload a worker after the memory consumption of the worker
goes above this threshold
 you’re alive even if you have a nasty memory leak
 max-requests - gracefully reload a process after n requests
 max-worker-lifetime – gracefully reload a process after n seconds
 logs: “...The work of process PID is done. Seeya!"
cool conf stuff (II)
 reload-mercy
 wait for n seconds for worker to die during reload/shutdown
 touch-reload
 reload your app by touching a single file (your main project file)
 attach-daemon
 attach-daemon = memcached -p 11911 -u user
 when uWSGI is stopped or reloaded, memcahed is destroyed
 smart-attach-daemon
 smart-attach-daemon = /tmp/memcached.pid memcached -p 11911 -d -P
/tmp/memcached.pid -u user
 memcached survives
production settings
 processes
 “There is no magic rule for setting the number of processes or threads to use.
It is very much application and system dependent. Simple math like
processes = 2 * cpucores will not be enough. You need to experiment with
various setups and be prepared to constantly monitor your apps.”
 threads
 “If you need threads, remember to enable them with enable-threads.”
 start with: processes = n, threads = 1
 multi-threading should be thoroughly tested! (debugging is very hard)
 rule of thumb for avoiding OOM when using threads=1:
 available memory = process n * reload-on-rss value
 suspect everything, test everything, log everything
performance testing
 get URLs which your applications deliver
 run siege / apachebench while keeping your eye on
monitoring/logging
 hardware is cheap
 buy more memory
 get a faster CPU
 scaling up is always easier than optimizing your app
production deployment
 keep a single project file
 touch it when updating
 art of graceful reloading
 https://github.com/unbit/uwsgi-
docs/blob/master/articles/TheArtOfGracefulReloading.rst
 “Some applications or frameworks (like Django) may load the vast
majority of their code only at the first request.” – causes timeouts if
you have high requests/second rate
 keep multiple application servers with reverse-proxy (or LB in front)
 update one server at the time, warm it up with siege/ab
monitor all the things!
 New Relic
 uwsgitop
 Sentry
 Cacti
 dstat, iostat, htop etc.
uwsgitop
 <stats>/home/user/uwsgi/uwsgistats.sock</stats>
 pip install uwsgitop
 uwsgitop /home/user/uwsgi/uwsgistats.sock
New Relic
 pip install newrelic
 uwsgi start script
newrelic-admin generate-config <LICENSE> newrelic.ini
NEW_RELIC_CONFIG_FILE=/home/user/uwsgi/newrelic.ini
export NEW_RELIC_CONFIG_FILE
newrelic-admin run-program uwsgi --xmlconfig /home/user/uwsgi/uwsgi.xml
 tip: newrelic is sometimes buggy (memory leak)
 tip!
things not mentioned
 emperor
 router
 metrics (snmp, statsd, alarms etc.)
 signal framework
 caching framework
 autostarting (supervisor, upstart)
 …
other WSGI app servers
 mod_wsgi
 Python version hell
 Green Unicorn (Gunicorn)
 Passenger (requires Ruby even if you don’t use it)
 etc.
conclusion
 very active project with a fast release cycle
 bunch of configuration options
 modular
 awesome docs
 mailing list support
 FTW!
thanks
 Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Provisioning with Puppet
Provisioning with PuppetProvisioning with Puppet
Provisioning with PuppetJoe Ray
 
Build and deployment
Build and deploymentBuild and deployment
Build and deploymentWO Community
 
Journey to Microservice architecture via Amazon Lambda
Journey to Microservice architecture via Amazon LambdaJourney to Microservice architecture via Amazon Lambda
Journey to Microservice architecture via Amazon LambdaAxilis
 
OSDC.no 2015 introduction to node.js workshop
OSDC.no 2015 introduction to node.js workshopOSDC.no 2015 introduction to node.js workshop
OSDC.no 2015 introduction to node.js workshopleffen
 
Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...
Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...
Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...Puppet
 
Docker puppetcamp london 2013
Docker puppetcamp london 2013Docker puppetcamp london 2013
Docker puppetcamp london 2013Tomas Doran
 
@arzumy Dev Setup #klxrb
@arzumy Dev Setup #klxrb@arzumy Dev Setup #klxrb
@arzumy Dev Setup #klxrbArzumy MD
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansibleMukul Malhotra
 
How did puppet change our system's life?
How did puppet change our system's life?How did puppet change our system's life?
How did puppet change our system's life?Hung Phung Dac
 
DevOps Series: Extending vagrant with Puppet for configuration management
DevOps Series: Extending vagrant with Puppet for configuration managementDevOps Series: Extending vagrant with Puppet for configuration management
DevOps Series: Extending vagrant with Puppet for configuration managementFelipe
 
Deploying PHP Applications with Ansible
Deploying PHP Applications with AnsibleDeploying PHP Applications with Ansible
Deploying PHP Applications with AnsibleOrestes Carracedo
 
Experiences from Running Masterless Puppet - PuppetConf 2014
Experiences from Running Masterless Puppet - PuppetConf 2014Experiences from Running Masterless Puppet - PuppetConf 2014
Experiences from Running Masterless Puppet - PuppetConf 2014Puppet
 
Python virtualenv & pip in 90 minutes
Python virtualenv & pip in 90 minutesPython virtualenv & pip in 90 minutes
Python virtualenv & pip in 90 minutesLarry Cai
 
Ansible Oxford - Cows & Containers
Ansible Oxford - Cows & ContainersAnsible Oxford - Cows & Containers
Ansible Oxford - Cows & Containersjonatanblue
 
Deploying Rails applications with Moonshine
Deploying Rails applications with MoonshineDeploying Rails applications with Moonshine
Deploying Rails applications with MoonshineRobot Mode
 
Docker to the Rescue of an Ops Team
Docker to the Rescue of an Ops TeamDocker to the Rescue of an Ops Team
Docker to the Rescue of an Ops TeamRachid Zarouali
 
Tool it Up! - Session #1 - Xhprof
Tool it Up! - Session #1 - XhprofTool it Up! - Session #1 - Xhprof
Tool it Up! - Session #1 - Xhproftoolitup
 

Was ist angesagt? (20)

Provisioning with Puppet
Provisioning with PuppetProvisioning with Puppet
Provisioning with Puppet
 
Build and deployment
Build and deploymentBuild and deployment
Build and deployment
 
Journey to Microservice architecture via Amazon Lambda
Journey to Microservice architecture via Amazon LambdaJourney to Microservice architecture via Amazon Lambda
Journey to Microservice architecture via Amazon Lambda
 
Ansible intro
Ansible introAnsible intro
Ansible intro
 
OSDC.no 2015 introduction to node.js workshop
OSDC.no 2015 introduction to node.js workshopOSDC.no 2015 introduction to node.js workshop
OSDC.no 2015 introduction to node.js workshop
 
Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...
Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...
Building and Testing from Scratch a Puppet Environment with Docker - PuppetCo...
 
Docker puppetcamp london 2013
Docker puppetcamp london 2013Docker puppetcamp london 2013
Docker puppetcamp london 2013
 
@arzumy Dev Setup #klxrb
@arzumy Dev Setup #klxrb@arzumy Dev Setup #klxrb
@arzumy Dev Setup #klxrb
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
170112
170112170112
170112
 
How did puppet change our system's life?
How did puppet change our system's life?How did puppet change our system's life?
How did puppet change our system's life?
 
DevOps Series: Extending vagrant with Puppet for configuration management
DevOps Series: Extending vagrant with Puppet for configuration managementDevOps Series: Extending vagrant with Puppet for configuration management
DevOps Series: Extending vagrant with Puppet for configuration management
 
Deploying PHP Applications with Ansible
Deploying PHP Applications with AnsibleDeploying PHP Applications with Ansible
Deploying PHP Applications with Ansible
 
Experiences from Running Masterless Puppet - PuppetConf 2014
Experiences from Running Masterless Puppet - PuppetConf 2014Experiences from Running Masterless Puppet - PuppetConf 2014
Experiences from Running Masterless Puppet - PuppetConf 2014
 
Python virtualenv & pip in 90 minutes
Python virtualenv & pip in 90 minutesPython virtualenv & pip in 90 minutes
Python virtualenv & pip in 90 minutes
 
Ansible Oxford - Cows & Containers
Ansible Oxford - Cows & ContainersAnsible Oxford - Cows & Containers
Ansible Oxford - Cows & Containers
 
Deploying Rails applications with Moonshine
Deploying Rails applications with MoonshineDeploying Rails applications with Moonshine
Deploying Rails applications with Moonshine
 
Vagrant to-aws-flow
Vagrant to-aws-flowVagrant to-aws-flow
Vagrant to-aws-flow
 
Docker to the Rescue of an Ops Team
Docker to the Rescue of an Ops TeamDocker to the Rescue of an Ops Team
Docker to the Rescue of an Ops Team
 
Tool it Up! - Session #1 - Xhprof
Tool it Up! - Session #1 - XhprofTool it Up! - Session #1 - Xhprof
Tool it Up! - Session #1 - Xhprof
 

Andere mochten auch

Djangoday lt 20120420
Djangoday lt 20120420Djangoday lt 20120420
Djangoday lt 20120420WEBdeBS
 
Deploying flask with nginx & uWSGI
Deploying flask with nginx & uWSGIDeploying flask with nginx & uWSGI
Deploying flask with nginx & uWSGI정주 김
 
Symantec 2011 CIP Survey Global Results
Symantec 2011 CIP Survey Global ResultsSymantec 2011 CIP Survey Global Results
Symantec 2011 CIP Survey Global ResultsSymantec
 
Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0joescars
 
Performance Tuning and Optimization
Performance Tuning and OptimizationPerformance Tuning and Optimization
Performance Tuning and OptimizationMongoDB
 
Unbit djangoday 20120419
Unbit djangoday 20120419Unbit djangoday 20120419
Unbit djangoday 20120419WEBdeBS
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQLKonstantin Gredeskoul
 
Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA EDB
 
An Introduction to OAuth 2
An Introduction to OAuth 2An Introduction to OAuth 2
An Introduction to OAuth 2Aaron Parecki
 
Scalable Django Architecture
Scalable Django ArchitectureScalable Django Architecture
Scalable Django ArchitectureRami Sayar
 
Access Control Presentation
Access Control PresentationAccess Control Presentation
Access Control PresentationWajahat Rajab
 
containerd and CRI
containerd and CRIcontainerd and CRI
containerd and CRIDocker, Inc.
 

Andere mochten auch (15)

Djangoday lt 20120420
Djangoday lt 20120420Djangoday lt 20120420
Djangoday lt 20120420
 
Deploying flask with nginx & uWSGI
Deploying flask with nginx & uWSGIDeploying flask with nginx & uWSGI
Deploying flask with nginx & uWSGI
 
Symantec 2011 CIP Survey Global Results
Symantec 2011 CIP Survey Global ResultsSymantec 2011 CIP Survey Global Results
Symantec 2011 CIP Survey Global Results
 
Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0
 
Performance Tuning and Optimization
Performance Tuning and OptimizationPerformance Tuning and Optimization
Performance Tuning and Optimization
 
nginx入門
nginx入門nginx入門
nginx入門
 
Unbit djangoday 20120419
Unbit djangoday 20120419Unbit djangoday 20120419
Unbit djangoday 20120419
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL
 
Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA
 
An Introduction to OAuth 2
An Introduction to OAuth 2An Introduction to OAuth 2
An Introduction to OAuth 2
 
Scalable Django Architecture
Scalable Django ArchitectureScalable Django Architecture
Scalable Django Architecture
 
ASP.NET Core 1.0 Overview
ASP.NET Core 1.0 OverviewASP.NET Core 1.0 Overview
ASP.NET Core 1.0 Overview
 
Access Control Presentation
Access Control PresentationAccess Control Presentation
Access Control Presentation
 
containerd and CRI
containerd and CRIcontainerd and CRI
containerd and CRI
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 

Ähnlich wie uWSGI - Swiss army knife for your Python web apps

Containerization is more than the new Virtualization: enabling separation of ...
Containerization is more than the new Virtualization: enabling separation of ...Containerization is more than the new Virtualization: enabling separation of ...
Containerization is more than the new Virtualization: enabling separation of ...Jérôme Petazzoni
 
Linux Server Deep Dives (DrupalCon Amsterdam)
Linux Server Deep Dives (DrupalCon Amsterdam)Linux Server Deep Dives (DrupalCon Amsterdam)
Linux Server Deep Dives (DrupalCon Amsterdam)Amin Astaneh
 
Testing kubernetes and_open_shift_at_scale_20170209
Testing kubernetes and_open_shift_at_scale_20170209Testing kubernetes and_open_shift_at_scale_20170209
Testing kubernetes and_open_shift_at_scale_20170209mffiedler
 
Sanger OpenStack presentation March 2017
Sanger OpenStack presentation March 2017Sanger OpenStack presentation March 2017
Sanger OpenStack presentation March 2017Dave Holland
 
Salting new ground one man ops from scratch
Salting new ground   one man ops from scratchSalting new ground   one man ops from scratch
Salting new ground one man ops from scratchJay Harrison
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevFelix Geisendörfer
 
Go Faster with Ansible (AWS meetup)
Go Faster with Ansible (AWS meetup)Go Faster with Ansible (AWS meetup)
Go Faster with Ansible (AWS meetup)Richard Donkin
 
NGINX Installation and Tuning
NGINX Installation and TuningNGINX Installation and Tuning
NGINX Installation and TuningNGINX, Inc.
 
LibOS as a regression test framework for Linux networking #netdev1.1
LibOS as a regression test framework for Linux networking #netdev1.1LibOS as a regression test framework for Linux networking #netdev1.1
LibOS as a regression test framework for Linux networking #netdev1.1Hajime Tazaki
 
Minimal OpenStack LinuxCon NA 2015
Minimal OpenStack LinuxCon NA 2015Minimal OpenStack LinuxCon NA 2015
Minimal OpenStack LinuxCon NA 2015Sean Dague
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsSu Zin Kyaw
 
Scaling an ELK stack at bol.com
Scaling an ELK stack at bol.comScaling an ELK stack at bol.com
Scaling an ELK stack at bol.comRenzo Tomà
 
Network Stack in Userspace (NUSE)
Network Stack in Userspace (NUSE)Network Stack in Userspace (NUSE)
Network Stack in Userspace (NUSE)Hajime Tazaki
 
(WEB401) Optimizing Your Web Server on AWS | AWS re:Invent 2014
(WEB401) Optimizing Your Web Server on AWS | AWS re:Invent 2014(WEB401) Optimizing Your Web Server on AWS | AWS re:Invent 2014
(WEB401) Optimizing Your Web Server on AWS | AWS re:Invent 2014Amazon Web Services
 
Containerization Is More than the New Virtualization
Containerization Is More than the New VirtualizationContainerization Is More than the New Virtualization
Containerization Is More than the New VirtualizationC4Media
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
Extending Piwik At R7.com
Extending Piwik At R7.comExtending Piwik At R7.com
Extending Piwik At R7.comLeo Lorieri
 

Ähnlich wie uWSGI - Swiss army knife for your Python web apps (20)

Containerization is more than the new Virtualization: enabling separation of ...
Containerization is more than the new Virtualization: enabling separation of ...Containerization is more than the new Virtualization: enabling separation of ...
Containerization is more than the new Virtualization: enabling separation of ...
 
Linux Server Deep Dives (DrupalCon Amsterdam)
Linux Server Deep Dives (DrupalCon Amsterdam)Linux Server Deep Dives (DrupalCon Amsterdam)
Linux Server Deep Dives (DrupalCon Amsterdam)
 
Testing kubernetes and_open_shift_at_scale_20170209
Testing kubernetes and_open_shift_at_scale_20170209Testing kubernetes and_open_shift_at_scale_20170209
Testing kubernetes and_open_shift_at_scale_20170209
 
Sanger OpenStack presentation March 2017
Sanger OpenStack presentation March 2017Sanger OpenStack presentation March 2017
Sanger OpenStack presentation March 2017
 
Salting new ground one man ops from scratch
Salting new ground   one man ops from scratchSalting new ground   one man ops from scratch
Salting new ground one man ops from scratch
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
Go Faster with Ansible (AWS meetup)
Go Faster with Ansible (AWS meetup)Go Faster with Ansible (AWS meetup)
Go Faster with Ansible (AWS meetup)
 
NGINX Installation and Tuning
NGINX Installation and TuningNGINX Installation and Tuning
NGINX Installation and Tuning
 
LibOS as a regression test framework for Linux networking #netdev1.1
LibOS as a regression test framework for Linux networking #netdev1.1LibOS as a regression test framework for Linux networking #netdev1.1
LibOS as a regression test framework for Linux networking #netdev1.1
 
Minimal OpenStack LinuxCon NA 2015
Minimal OpenStack LinuxCon NA 2015Minimal OpenStack LinuxCon NA 2015
Minimal OpenStack LinuxCon NA 2015
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Scaling an ELK stack at bol.com
Scaling an ELK stack at bol.comScaling an ELK stack at bol.com
Scaling an ELK stack at bol.com
 
NodeJS
NodeJSNodeJS
NodeJS
 
Network Stack in Userspace (NUSE)
Network Stack in Userspace (NUSE)Network Stack in Userspace (NUSE)
Network Stack in Userspace (NUSE)
 
Running php on nginx
Running php on nginxRunning php on nginx
Running php on nginx
 
(WEB401) Optimizing Your Web Server on AWS | AWS re:Invent 2014
(WEB401) Optimizing Your Web Server on AWS | AWS re:Invent 2014(WEB401) Optimizing Your Web Server on AWS | AWS re:Invent 2014
(WEB401) Optimizing Your Web Server on AWS | AWS re:Invent 2014
 
Containerization Is More than the New Virtualization
Containerization Is More than the New VirtualizationContainerization Is More than the New Virtualization
Containerization Is More than the New Virtualization
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Multicore
MulticoreMulticore
Multicore
 
Extending Piwik At R7.com
Extending Piwik At R7.comExtending Piwik At R7.com
Extending Piwik At R7.com
 

Kürzlich hochgeladen

Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxmibuzondetrabajo
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxMario
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxNIMMANAGANTI RAMAKRISHNA
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书rnrncn29
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119APNIC
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa494f574xmv
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxAndrieCagasanAkio
 

Kürzlich hochgeladen (11)

Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptx
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptx
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptx
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptx
 

uWSGI - Swiss army knife for your Python web apps

  • 1. uWSGI Swiss army knife for your Python web apps TOMISLAV RAŠETA @traseta
  • 2. me  scribbling and doodling on *IX operating systems for 10+ years  working as sysadmin/DevOps with a hint of networking  tools of the trade  nginx apache tomcat (ugh!)  python php java (ugh!)  postgresql mysql  memcached redis mongo  not a developer, sorry :)
  • 3. before we begin  know your environment  server specs (cores, memory, bandwidth)  virtualenv or global  know your application  database pooling agents and latencies  python version requirements (!)  Linux kernel tunables  limits.conf (file descriptors)  ipv4.tcp_tw_reuse (not recycle)  kernel.shmmax  ipv4.ip_local_port_range
  • 4. what it is  full stack for building hosting services  pluggable architecture  mostly used as application server for Python apps using WSGI  “WSGI is the Web Server Gateway Interface. It is a specification that describes how web server communicates with web applications, and how web applications can be chained together to process one request.”  rules  versatility  performance  low-resource usage  reliability  developed by @unbit (Italy)  awesome docs
  • 5. installation  PyPI  most recent versions, maintained by devs  pip install uwsgi  use virtualenv when possible  watch out for dependencies - check what you actually need, the compile errors/warnings are very descriptive  distro packed versions are *always* outdated  “from source” - please avoid :)
  • 6. configuration  choice of xml, ini, command-line arguments etc.  read the docs carefully  don’t reinvent the wheel  "Please, turn on your brain and try to adapt shown configs to your needs, or invent new ones."
  • 7. configuration example (xml), start from ve <uwsgi> <chdir>/home/user/ve/app</chdir> <wsgi-file>project.wsgi</wsgi-file> <uid>user</uid> <gid>user</gid> <uwsgi-socket>/home/user/uwsgi/uwsgi.sock</uwsgi-socket> <chmod-socket>600</chmod-socket> <pidfile>/home/user/uwsgi/uwsgi.pid</pidfile> <stats>/home/user/uwsgi/uwsgistats.sock</stats> <buffer-size>8192</buffer-size> <post-buffering>4096</post-buffering> <memory-report/> <daemonize>/home/user/uwsgi/daemon.log</daemonize> <reload-mercy>20</reload-mercy> <max-requests>500</max-requests> <max-worker-lifetime>86400</max-worker-lifetime> <socket-timeout>5</socket-timeout> <disable-logging/> <reload-on-rss>256</reload-on-rss> <touch-reload>/home/user/ve/app/project.wsgi</touch-reload> <enable-threads/> <threads>1</threads> <processes>100</processes> <single-interpreter/> <vacuum/> </uwsgi>
  • 8. start it up  activate ve  uwsgi --xmlconfig /home/user/uwsgi/uwsgi.xml  configure and start nginx location / { include uwsgi_params; uwsgi_pass unix:/home/wmaster/uwsgi/uwsgi.sock; }  ta-daa!  tip: socket based pass doesn’t consume server TCP port usage = less SYSCPU time on your server; you can use TCP but check your kernel tunables before! (tcp_tw_reuse, ip_local_port_range  tip2: you can also use mod_uwsgi and apache
  • 9. cool conf stuff (I)  harakiri (timeout)  every request is timestamped  If a master process finds a worker with a timestamp running more than specified timeout it will kill it  logs: “F*CK !!! i must kill myself (pid: 9984 app_id: 0)”  reload-on-rss  gracefully reload a worker after the memory consumption of the worker goes above this threshold  you’re alive even if you have a nasty memory leak  max-requests - gracefully reload a process after n requests  max-worker-lifetime – gracefully reload a process after n seconds  logs: “...The work of process PID is done. Seeya!"
  • 10. cool conf stuff (II)  reload-mercy  wait for n seconds for worker to die during reload/shutdown  touch-reload  reload your app by touching a single file (your main project file)  attach-daemon  attach-daemon = memcached -p 11911 -u user  when uWSGI is stopped or reloaded, memcahed is destroyed  smart-attach-daemon  smart-attach-daemon = /tmp/memcached.pid memcached -p 11911 -d -P /tmp/memcached.pid -u user  memcached survives
  • 11. production settings  processes  “There is no magic rule for setting the number of processes or threads to use. It is very much application and system dependent. Simple math like processes = 2 * cpucores will not be enough. You need to experiment with various setups and be prepared to constantly monitor your apps.”  threads  “If you need threads, remember to enable them with enable-threads.”  start with: processes = n, threads = 1  multi-threading should be thoroughly tested! (debugging is very hard)  rule of thumb for avoiding OOM when using threads=1:  available memory = process n * reload-on-rss value  suspect everything, test everything, log everything
  • 12. performance testing  get URLs which your applications deliver  run siege / apachebench while keeping your eye on monitoring/logging  hardware is cheap  buy more memory  get a faster CPU  scaling up is always easier than optimizing your app
  • 13. production deployment  keep a single project file  touch it when updating  art of graceful reloading  https://github.com/unbit/uwsgi- docs/blob/master/articles/TheArtOfGracefulReloading.rst  “Some applications or frameworks (like Django) may load the vast majority of their code only at the first request.” – causes timeouts if you have high requests/second rate  keep multiple application servers with reverse-proxy (or LB in front)  update one server at the time, warm it up with siege/ab
  • 14. monitor all the things!  New Relic  uwsgitop  Sentry  Cacti  dstat, iostat, htop etc.
  • 15. uwsgitop  <stats>/home/user/uwsgi/uwsgistats.sock</stats>  pip install uwsgitop  uwsgitop /home/user/uwsgi/uwsgistats.sock
  • 16. New Relic  pip install newrelic  uwsgi start script newrelic-admin generate-config <LICENSE> newrelic.ini NEW_RELIC_CONFIG_FILE=/home/user/uwsgi/newrelic.ini export NEW_RELIC_CONFIG_FILE newrelic-admin run-program uwsgi --xmlconfig /home/user/uwsgi/uwsgi.xml  tip: newrelic is sometimes buggy (memory leak)  tip!
  • 17. things not mentioned  emperor  router  metrics (snmp, statsd, alarms etc.)  signal framework  caching framework  autostarting (supervisor, upstart)  …
  • 18. other WSGI app servers  mod_wsgi  Python version hell  Green Unicorn (Gunicorn)  Passenger (requires Ruby even if you don’t use it)  etc.
  • 19. conclusion  very active project with a fast release cycle  bunch of configuration options  modular  awesome docs  mailing list support  FTW!