Difference between revisions of "Short Notes on Python"
From PaskvilWiki
(→Importing Files) |
|||
Line 28: | Line 28: | ||
# use your commons.py module now... | # use your commons.py module now... | ||
commons.super_function()</nowiki> | commons.super_function()</nowiki> | ||
+ | |||
+ | == uWSGI, nginx, Flask == | ||
+ | |||
+ | * install uwsgi (incl. uwsgi python plugin), python flask, and nginx, | ||
+ | |||
+ | === Setting Up uWSGI === | ||
+ | |||
+ | * create ''main.py'' file that will hold the server logic, for instance: | ||
+ | <pre>from flask import Flask | ||
+ | |||
+ | app = Flask(__name__) | ||
+ | |||
+ | |||
+ | @app.route("/") | ||
+ | def hello(): | ||
+ | return "hello there!"</pre> | ||
+ | * create uwsgi config file, ''wsgi.ini'' ('''minimal''' version here; read uwsgi docs for head-spinning array of configurables): | ||
+ | <pre>[uwsgi] | ||
+ | module = wsgi:app | ||
+ | master = true | ||
+ | processes = 5 | ||
+ | socket = 127.0.0.1:8000 | ||
+ | protocol = http | ||
+ | plugin = python</pre> | ||
+ | * run uwsgi | ||
+ | uwsgi --ini wsgi.ini | ||
+ | * check that all works on http://localhost:8000/ | ||
+ | |||
+ | === Adding nginx Layer === | ||
+ | |||
+ | * remove the "protocol" directive from ''wsgi.ini'', and add "die-on-term": | ||
+ | <pre>[uwsgi] | ||
+ | module = wsgi:app | ||
+ | master = true | ||
+ | processes = 5 | ||
+ | socket = 127.0.0.1:8000 | ||
+ | plugin = python | ||
+ | die-on-term = true</pre> | ||
+ | * add a new vhost to nginx - ''/etc/nginx/sites-available/app.nginx'': | ||
+ | <pre>server { | ||
+ | listen 80; | ||
+ | server_name my.awesome.domain; | ||
+ | location / { | ||
+ | include uwsgi_params; | ||
+ | uwsgi_pass 127.0.0.1:8000; | ||
+ | } | ||
+ | }</pre> | ||
+ | ** communication through socket is also possible (see ''socket'', ''chmod-socket'', ''vacuum'' and other directives for uWSGI) | ||
+ | ** of course, create link in ''/etc/nginx/sites-enabled/'', and restart nginx, | ||
+ | * create ''systemd'' file for uWSGI, ''/etc/systemd/system/uwsgi-app.service'': | ||
+ | <pre>[Unit] | ||
+ | Description=Job that runs the uWSGI app | ||
+ | |||
+ | [Service] | ||
+ | Type=simple | ||
+ | WorkingDirectory=/home/project/flask-test/ | ||
+ | ExecStart=/usr/bin/uwsgi --ini wsgi.ini</pre> |
Revision as of 16:17, 8 July 2016
Contents
Timing, and memory, on Linux
Timing
On Linux, it's safer to use time.time()
import time t = time.time() # do some stuff print "stuff took %1.3f", time.time() - t, "seconds"
On Windows, AFAIK, it's safer to use time.clock()
Memory
For me, the following does a good job getting memory usage (in kB) on Linux:
import resource print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
Since resource is standard package, it should work on Windows too, but I don't know if it does, or what units are used if it works.
Importing Files
If you need to import a file '../mylib/commons.py', you can use the following snippet:
import sys, os fld = os.path.realpath(os.path.abspath(os.path.join('..', 'mylib'))) if fld not in sys.path: sys.path.insert(0, fld) import commons # use your commons.py module now... commons.super_function()
uWSGI, nginx, Flask
- install uwsgi (incl. uwsgi python plugin), python flask, and nginx,
Setting Up uWSGI
- create main.py file that will hold the server logic, for instance:
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "hello there!"
- create uwsgi config file, wsgi.ini (minimal version here; read uwsgi docs for head-spinning array of configurables):
[uwsgi] module = wsgi:app master = true processes = 5 socket = 127.0.0.1:8000 protocol = http plugin = python
- run uwsgi
uwsgi --ini wsgi.ini
- check that all works on http://localhost:8000/
Adding nginx Layer
- remove the "protocol" directive from wsgi.ini, and add "die-on-term":
[uwsgi] module = wsgi:app master = true processes = 5 socket = 127.0.0.1:8000 plugin = python die-on-term = true
- add a new vhost to nginx - /etc/nginx/sites-available/app.nginx:
server { listen 80; server_name my.awesome.domain; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; } }
- communication through socket is also possible (see socket, chmod-socket, vacuum and other directives for uWSGI)
- of course, create link in /etc/nginx/sites-enabled/, and restart nginx,
- create systemd file for uWSGI, /etc/systemd/system/uwsgi-app.service:
[Unit] Description=Job that runs the uWSGI app [Service] Type=simple WorkingDirectory=/home/project/flask-test/ ExecStart=/usr/bin/uwsgi --ini wsgi.ini