---
title: "Using WSGI"
date: "2015-02-13"
category: "python"
slug: "using-wsgi"
---

## Overview

Python applications can be launched using [[using-multiple-versions-passenger]] for improved throughput and lifecycle management. CGI scripts wrapped by pyenv have poor performance due to the shell subprocesses needed to resolve the correct interpreter. Adapting a CGI application to WSGI eliminates this overhead by running a persistent process, resulting in faster page loads and more efficient resource usage.

**Note:** This requires a [[determining-platform-version]] hosting platform.

## Simple WSGI Script

**Prerequisite:** Follow the guide in [[using-multiple-versions-passenger]] to create a suitable directory structure.

Create a Passenger-compatible WSGI script named `passenger_wsgi.py` in your application root (alongside, not inside, the `public/` folder). Passenger requires a function named `application()` as the entry point. Without this function and file, Passenger cannot load your application.

The following example is compatible with Python 3:

\# Python 3-compatible version
import sys
ctr=0

def application(environ, start\_response):
 global ctr
 start\_response('200 OK', [('Content-Type', 'text/plain')])
 v = sys.version\_info
 ctr+=1
 str = 'hello world from %d.%d.%d!\\n' % (v.major, v.minor, v.micro)
 return [bytes(str, 'UTF-8')]

Your directory structure should look like:

.
├── passenger\_wsgi.py
├── public/
├── .python-version
└── tmp/

`.python-version` is created by defining a Python version for the directory, e.g. `pyenv local 3.3.5`. Connect the `public/` [[where-is-site-content-served-from]] to a subdomain in the [[logging-into-the-control-panel]] under **Web** > **Subdomains**.

## Troubleshooting

If the application does not launch, check the Passenger log at `/var/log/httpd/passenger.log`. See [[accessing-page-views-and-error-messages]] for details.
