analysis of Python WSGI By Obi Uchenna David Obi Uchenna David (othreecodes) Software Developer at Tuteria Ingressive Campus Ambassador KnowIT CTO and Tutor Part time Poet
(pronounced “whiz-gee” with a hard “g” or “whiskey”) Its a specification for simple and universal interface between web servers and web applications or frameworks for the Python programming language. (PEP333)
(Contd.) • Portability • Its Simple • Many Python components for web application development are standardizing on it • Web Frameworks are not made to process thousands of request. NB: WSGI is not a server, a python module, a framework, an API or any kind of software. It is just an interface specification by which server and application communicate.
receives the request from the client, pass it to the application and then send the response returned by the application to the client. It does nothing else.
on listen 80; # the domain name it will serve for server_name 0.0.0.0 ;# www.thecovfefe.com.ng thecovfefe.com.ng; # substitute by your FQDN and machine's IP address charset utf-8; #Max upload size client_max_body_size 75M; # adjust to taste error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; # Django media location /media { alias /root/thecovfefe/thecovfefe/media/; # your Django project's media files } location /static { alias /root/thecovfefe/thecovfefe/static/; # your Django project's static files }# Finally, send all non-media requests to the Django server. location / { include proxy_params; proxy_pass http://unix:/root/thecovfefe/covfefe.sock; } } NGINX
within the WSGI server • Do not show if the best achievable config was used • Ain’t nobody deploying “Hello World” applications • All requests are not the same • Don’t Trust benchmarks !
Y WSGI server running on U processes that have V threads • Configuring your web server will be different for different applications • There is not a single wsgi configuration to give your web application because your web app is going to be unique • Monitoring - Performance monitoring helps Identify bottlenecks in your apps. Eg New Relic, Appdynamics, Data Dogs etc
the minimum amount of work to solve the problem • Defer the work you don't need to do immediately • Use cache when you can • Understand and avoid the N+1 query problem with relational databases
all single quotes in the code base to double quotes and wrote tests for it” CTO: • This very often happens because such developers don't really know what are the critical parts of their code or the ones that need to be optimized more • Write the first version of your code without worrying much about performance • use a profiler to instrument your code and see where the bottlenecks are • Optimize NB: This is not an Excuse to write shitty code
Make use of job queues and Asynchronous tasks. • A simple queue system can be easily done with any kind of data store (very often Redis or MongoDB are used) or a message broker like RabbitMQ or ActiveMQ.
so that it can be quickly accessed at a later time. • Byte Code Cache • Application Cache eg Redis • HTTP Cache: using HTTP Cache headers like `Etag` and `Cache-control` • Proxy Cache eg Nginx
all_active_user_books = {} users = User.object.filter(is_active=True): all_active_user_books = [for x in Books.objects.filter(owner=user) for user in users ]
WHERE user_id = 1; SELECT * FROM Books WHERE user_id = 2; SELECT * FROM Books WHERE user_id = 3; SELECT * FROM Books WHERE user_id = 4; SELECT * FROM Books WHERE user_id = 5; SELECT * FROM Books WHERE user_id = 6; SELECT * FROM Books WHERE user_id = 7; . . SELECT * FROM Books WHERE user_id = n; • Use SELECT for only the data you need • Use JOIN Syntax • Use UNION • Use LIMIT Or their equivalent in whatever ORM you use.