Skip to main content

Timeout timer use signal or thread module, support nested loop

Project description

PyPI version

Installation

python setup.py install

Timeout Timer

Add a timeout function to a function or statement and raise a exception if time limit runs out, can work as a context or decorator, support loop nesting and should use diff exception class, if use signal timer, outside timer will fired after the inside signal timer finish the work(raise exception or normal finish).

Support signal timer and thread timer, signal timer can only work on main thread, if not on main thread use thread timer, thread timer may cost longer time than time out seconds settled if the user’s function is busy in a system call (time.sleep(), socket.accept()…), exception will fired after system call done.

Usage

support nested loop

from timeout_timer import timeout, TimeoutInterrupt

class TimeoutInterruptNested(TimeoutInterrupt):
    pass

def test_timeout_nested_loop_both_timeout(timer):
    cnt = 0
    try:
        with timeout(5, timer=timer):
            try:
                with timeout(2, timer=timer, exception=TimeoutInterruptNested):
                    sleep(2)
            except TimeoutInterruptNested:
                cnt += 1
            time.sleep(10)
    except TimeoutInterrupt:
        cnt += 1
    assert cnt == 2

use as decorator

@timeout(2):
def f():
    time.sleep(3)
    time.sleep(2)

License

Code released under the MIT license

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timeout-timer-0.2.0.tar.gz (5.6 kB view hashes)

Uploaded Source

Built Distribution

timeout_timer-0.2.0-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page