Subprocess WSGI middleware and Pyramid tween.
Project description
This package was built to support rendering Python generated JSON into HTML using a nodejs. Transform subprocesses are reused, avoiding process startup overhead and allowing the JIT to kick in. For our code this gives a 10x speedup for subsequent responses.
The protocol is simple and generic, HTTP formatted responses (headers and body) are piped into and out of the transform subprocess. Transforms may modify both the response headers and body.
Transforms modifying the response body must ensure the Content-Length header is updated to match.
Python 2 and subprocess32
The subprocess module in Python 2.7 can leak file descriptors. Backported fixes from Python 3.x are available in subprocess32, and will be used if installed.
Pipe buffering
For small responses, the unix command cat works as an identity transform. Once a response exceeds the pipe buffer limit (typically 16K or 64K), a deadlock occurs with both processes waiting for the other to read. To avoid this, subprocesses should read in the entirity of each response before writing to stdout and flush stdout at the response end.
Working around this limitation would require writing and reading from different threads.
Alternatives
Apache mod_ext_filter
With mod_ext_filter, the response body is simply piped through an external program. A new process is started for each response, so it suffers from the same limitations as CGI where application setup costs are paid for each request.
FastCGI filter
FastCGI defines a filter role for transforming responses using long-lived processes. Unfortunately the filter role is not supported by Apache mod_fcgid and the FastCGI protocol itself is unnecessarily complicated to implement.
Transforming HTTP reverse proxy
Another alternative would be to implement the transform as part of an HTTP proxy. This adds significant deployment complexity with multiple hops required to support SSL.
PyV8
PyV8 allows running JavaScript in process. It can be tricky to build whereas nodejs packages are easily available.
uWSGI Transformations
uWSGI are working on an rpc plugin for their transformation system. The rpc protocol itself has a 64k request size limit.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file subprocess_middleware-0.3.zip
.
File metadata
- Download URL: subprocess_middleware-0.3.zip
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 182c3ecd1d5657dbfb2e1d22629835305b26851d8baaf759f10cd1b2e4b4697e |
|
MD5 | f784acee2a9faba045ea721a7b2d1a0d |
|
BLAKE2b-256 | 5630ab3a1f62f706c1081f51b1660167cd877acf7a36c6cf4ba621596f1b34e8 |