WSGI Middleware that implements a customizable 'harakiri' like uWSGI
Project description
WSGI Harakiri
Abandoned
Don’t use this, it’s not the best way to achieve a request timeout. You’re probably better off using and improving any such feature built into your wsgi server, since it will terminate things more reliably.
WSGI Middleware that implements a customizable ‘harakiri’ like uWSGI.
Installation
Use pip:
pip install wsgi-harakiri
Tested on Python 2.7, 3.4, and 3.5.
Usage
Wrap your WSGI application with the middleware, for example for a Django application in your wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()
from wsgi_harakiri import HarakiriMiddleware
# By default adds a request timeout of 30 seconds
application = HarakiriMiddleware(application)
Your app must not be running a multi-threaded server (multi-process is ok), and it must be running on a POSIX system. The alarm system call is used, so this cannot be combined with other things that use it, e.g. the ‘harakiri’ functionality in uWSGI.
API
HarakiriMiddleware(application, timeout=30, on_harakiri=None, error_app=None)
Wraps a WSGI application with the harakiri functionality.
application may be any WSGI application.
timeout may be any integer number of seconds, and defaults to 30.
on_harakiri will be called when a harakiri occurs, from inside the alarm function - it is thus suitable for logging the stack trace that lead to the timeout. Its return value is ignored.
error_app is a second WSGI application that will be called to produce an error response when a timeout occurs. The default response is a rather plain ‘500 Internal server error’ with HTML ‘<h1>Page load timeout</h1>’.
Example usage with all arguments:
application = get_wsgi_application()
from wsgi_harakiri import HarakiriMiddleware
def harakiri_handler():
logger.error("Harakiri occured", extra={'stack': True})
def harakiri_page(environ, start_response):
start_response(
'500 Internal server error',
[('Content-Type', 'text/html')]
)
return [b'<h1>Sorry, this page timed out.</h1>']
application = HarakiriMiddleware(
application,
timeout=15,
on_harakiri=harakiri_handler,
error_app=harakiri_page,
)
Harakiri
This is the exception that gets raised when a timeout occurs. You should not catch it anywhere in your code, however you could use it to detect when it happens inside a particular code path. For example:
from wsgi_harakiri import Harakiri
def find_users(search_term):
conn = make_db_connection()
try:
return conn.query(search_term)
except Harakiri:
logger.error("A search timed out", extra={'search_term': search_term})
raise
History
Pending Release
New release notes here
1.0.0 (2016-03-28)
First release on PyPI, with HarakiriMiddleware.
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
Built Distribution
File details
Details for the file wsgi-harakiri-1.0.0.tar.gz
.
File metadata
- Download URL: wsgi-harakiri-1.0.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84583627bf1d0afdeae062855f78970c04b09aec80b0c51cc127ef06e4e43ada |
|
MD5 | 247f66a80a1367454a8f3723a012f221 |
|
BLAKE2b-256 | 80342da6fa30cf6a516367b5a06d9f4d165c972e3fb9e335955e0de1e9ab1abc |
File details
Details for the file wsgi_harakiri-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: wsgi_harakiri-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b28fcdf2696bcf4d0877174c802e732072d4a0dda4702eebc921fc15da54da37 |
|
MD5 | 2e19d8a3b5c648b3af8b6283e8212dc4 |
|
BLAKE2b-256 | 2044eb5d49be9ec0610ef8762e9d37689c8308ceac8231b01799e01aad2c81b7 |