geopython/PyWPS — pull request #24 by ricardogsilva (2014-08-28 17:00:45+00:00)
Title: improved async execution when running in mod_wsgi and under a virtualenv

This pull request enables the async execution of processes when running PyWPS under Apache's mod_wsgi and when using a virtualenv.

It addresses the following problems:

##### No logging for the async process
When ran under mod_wsgi, there was no logging for the executed process. This was due to the fact that mod_wsgi does not expect client code to call sys.stdout (or print, or logging.debug). It only redirects sys.stderr to the apache error log file.

This solution involves replacing sys.stdout with sys.stderr inside the spawned process, as described in https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Writing_To_Standard_Output

##### No way to load pywps settings if they are in a custom path
When the Execute instance spawned after having been called by subprocess.Popen, it didn't have any way of knowing where to read its configuration and would default to the standard places.

This solution solves this issue by pickling the configuration object (along with the already being pickled Wps instance) into the state file. The spawned process then unpickles the file and restores the same configurations that were being used by the parent process (the one who called subprocess.Popen)

##### No way to use the virtualenv inside the spawned process
When the Execute instance spawned a new process, the virtualenv settings were lost, and the new process would run under the normal, system Python.

This solution solves this issue by adding a new **optional** flag to the pywps settings which specifies the path to the virtualenv being used. The flag is called `virtualenvpath` and belongs in the `server` section.

```python
# pywps settings file

[server]
virtualenvpath =
```

When the new process is spawned, and after loading the pickled settings, it tries to read the value of `virtualenvpath` and activates the virtualenv following the instructions described in https://code.google.com/p/modwsgi/wiki/VirtualEnvironments#Application_Environments