Python support

See also

Python configuration options

Application dictionary

You can use the application dictionary mechanism to avoid setting up your application in your configuration.

  1. import uwsgi
  2. import django.core.handlers.wsgi
  3.  
  4. application = django.core.handlers.wsgi.WSGIHandler()
  5.  
  6. def myapp(environ, start_response):
  7. start_response('200 OK', [('Content-Type', 'text/plain')])
  8. yield 'Hello World\n'
  9.  
  10. uwsgi.applications = {
  11. '': application,
  12. '/django': 'application',
  13. '/myapp': myapp
  14. }

Passing this Python module name (that is, it should be importable and without the .py extension) to uWSGI’s module / wsgi option, uWSGI will search the uwsgi.applications dictionary for the URL prefix/callable mappings.

The value of every item can be a callable, or its name as a string.

Virtualenv support

virtualenv is a mechanism that lets you isolate one (or more) Python applications’ libraries (and interpreters, when not using uWSGI) from each other.Virtualenvs should be used by any respectable modern Python application.

Quickstart

  • Create your virtualenv:
  1. $ virtualenv myenv
  2. New python executable in myenv/bin/python
  3. Installing setuptools...............done.
  4. Installing pip.........done.
  • Install all the modules you need (using Flask as an example):
  1. $ ./myenv/bin/pip install flask
  2. $ # Many modern Python projects ship with a `requirements.txt` file that you can use with pip like this:
  3. $ ./myenv/bin/pip install -r requirements.txt
  • Copy your WSGI module into this new environment (under lib/python2.x if you do not want to modify your PYTHONPATH).

Note

It’s common for many deployments that your application will live outside the virtualenv. How to configure this is not quite documented yet, but it’s probably very easy.

Run the uwsgi server using the home/virtualenv option (-H for short):

  1. $ uwsgi -H myenv -s 127.0.0.1:3031 -M -w envapp

Python 3

The WSGI specification was updated for Python 3 as PEP3333.

One major change is that applications are required to respond only with bytes instances, not (Unicode) strings, back to the WSGI stack.

You should encode strings or use bytes literals:

  1. def application(environ, start_response):
  2. start_response('200 OK', [('Content-Type', 'text/plain')])
  3. yield 'Hello '.encode('utf-8')
  4. yield b'World\n'

Paste support

If you are a user or developer of Paste-compatible frameworks, such as Pyramid, Pylons and Turbogears or applications using them, you can use the uWSGI —paste option to conveniently deploy your application.

For example, if you have a virtualenv in /opt/tg2env containing a Turbogears app called addressbook configured in /opt/tg2env/addressbook/development.ini:

  1. uwsgi --paste config:/opt/tg2env/addressbook/development.ini --socket :3031 -H /opt/tg2env

That’s it! No additional configuration or Python modules to write.

Warning

If you setup multiple process/workers (master mode) you will receive an error:

  1. AssertionError: The EvalException middleware is not usable in a multi-process environment

in which case you’ll have to set the debug option in your paste configuration file to False – or revert to single process environment.

Pecan support

If you are a user or developer of the Pecan WSGI framework, you can use the uWSGI —pecan option to conveniently deploy your application.

For example, if you have a virtualenv in /opt/pecanenv containing a Pecan app called addressbook configured in /opt/pecanenv/addressbook/development.py:

  1. uwsgi --pecan /opt/pecanenv/addressbook/development.py --socket :3031 -H /opt/pecanenv

Warning

If you setup multiple process/workers (master mode) you will receive an error:

  1. AssertionError: The DebugMiddleware middleware is not usable in a multi-process environment

in which case you’ll have to set the debug option in your pecan configuration file to False – or revert to single process environment.

Using the django-uwsgi Django app

First of all you need to get the django_uwsgi app from https://github.com/unbit/django-uwsgi (once it was in the django directory of the distribution).

Install it with pip install django-uwsgi and add it into your INSTALLED_APPS.

  1. INSTALLED_APPS = (
  2. # ...
  3. 'django.contrib.admin',
  4. 'django_uwsgi',
  5. # ...
  6. )

Then modify your urls.py accordingly. For example:

  1. # ...
  2. url(r'^admin/uwsgi/', include('django_uwsgi.urls')),
  3. url(r'^admin/', include(admin.site.urls)),
  4. # ...

Be sure to place the URL pattern for djangouwsgi _before the one for the admin site, or it will never match.

/admin/uwsgi/ will then serve uWSGI statistics and has a button for graceful reloading of the server (when running under a Master). Note that memory usage is reported only when the memory-report option is enabled.

Read the documentation for django-uwsgi at rtfd.org