Skip to main content

Subprocess Middleware

https://travis-ci.org/lrowe/subprocess_middleware.svg?branch=master

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.

Release files for subprocess_middleware 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for subprocess_middleware 0.3
File Size Uploaded
subprocess_middleware-0.3.zip 14.5 kB Details

Release files / subprocess_middleware-0.3.zip

Download URL subprocess_middleware-0.3.zip
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
182c3ecd1d5657dbfb2e1d22629835305b26851d8baaf759f10cd1b2e4b4697e
BLAKE2b-256 checksum
How to use checksums
5630ab3a1f62f706c1081f51b1660167cd877acf7a36c6cf4ba621596f1b34e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3 This release

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page