Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

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.

https://img.shields.io/pypi/v/wsgi-harakiri.svg https://img.shields.io/travis/adamchainz/wsgi-harakiri.svg

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.

Metadata

Release files for wsgi-harakiri 1.0.0

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

Source distribution (sdist)

Source distribution for wsgi-harakiri 1.0.0
File Size Uploaded
wsgi-harakiri-1.0.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wsgi-harakiri 1.0.0
File Interpreter ABI Platform
wsgi_harakiri-1.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 11.2 kB

Release files / wsgi-harakiri-1.0.0.tar.gz

Download URL wsgi-harakiri-1.0.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
84583627bf1d0afdeae062855f78970c04b09aec80b0c51cc127ef06e4e43ada
BLAKE2b-256 checksum
How to use checksums
80342da6fa30cf6a516367b5a06d9f4d165c972e3fb9e335955e0de1e9ab1abc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / wsgi_harakiri-1.0.0-py2.py3-none-any.whl

Download URL wsgi_harakiri-1.0.0-py2.py3-none-any.whl
Size 5.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b28fcdf2696bcf4d0877174c802e732072d4a0dda4702eebc921fc15da54da37
BLAKE2b-256 checksum
How to use checksums
2044eb5d49be9ec0610ef8762e9d37689c8308ceac8231b01799e01aad2c81b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

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