Skip to content

Commit d03aac3

Browse files
committed
init
0 parents commit d03aac3

File tree

8 files changed

+563
-0
lines changed

8 files changed

+563
-0
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# middle file
2+
*.py[cod]
3+
*.swp
4+
*~
5+
venv
6+
.env
7+
8+
# C extensions
9+
*.so
10+
11+
# Packages
12+
*.egg
13+
*.egg-info
14+
dist
15+
build
16+
eggs
17+
parts
18+
bin
19+
var
20+
sdist
21+
develop-eggs
22+
.installed.cfg
23+
lib
24+
lib64

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021- DozySun
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Installation
2+
============
3+
::
4+
5+
python setup.py install
6+
7+
Timeout Timer
8+
============
9+
Add a timeout function to a function or statement and raise a exception if time limit runs out ,
10+
it support loop nesting when timer worked as loop nesting, if use signal timer, it will fire after the inside
11+
signal timer finish the work.
12+
13+
Signal timer can only work on main thread, if not use thread timer, thread timer may cost longer time than
14+
time out seconds if the timer's sub thread(user's function) is busy in a system call (time.sleep(),
15+
socket.accept()...), exception will fired after system call done.
16+
17+
Usage
18+
============
19+
support nested loop
20+
::
21+
try:
22+
with TimeoutTimer(2, timer="signal") as f:
23+
f(time.sleep, 0.5)
24+
with TimeoutTimer(1, timer="signal") as f2:
25+
f2(time.sleep, 2)
26+
except TimeoutInterrupt:
27+
print("timeout triggered")
28+
29+
30+
or use signal timer can simplify
31+
::
32+
try:
33+
with TimeoutTimer(2) :
34+
time.sleep(3)
35+
except TimeoutInterrupt:
36+
print("timeout triggered")
37+
38+
or use as decorator
39+
::
40+
@TimeoutTimer(2):
41+
def f():
42+
time.sleep(3)
43+
time.sleep(2)
44+
45+
License
46+
-------
47+
48+
Code released under the `MIT license <http://en.wikipedia.org/wiki/MIT_License>`_

setup.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Setuptools entry point."""
2+
import os
3+
4+
try:
5+
from setuptools import setup
6+
except ImportError:
7+
from distutils.core import setup
8+
9+
CLASSIFIERS = """
10+
Development Status :: 1 - Beta
11+
Intended Audience :: Developers
12+
License :: OSI Approved :: MIT License
13+
Programming Language :: C
14+
Programming Language :: Python :: 3
15+
Programming Language :: Python :: 3.6
16+
Programming Language :: Python :: 3.7
17+
Programming Language :: Python :: 3.8
18+
Programming Language :: Python :: 3.9
19+
Programming Language :: Python :: 3 :: Only
20+
"""
21+
22+
23+
def long_description():
24+
with open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'README.rst'))) as f:
25+
return f.read()
26+
27+
28+
setup(
29+
name='timeout-timer',
30+
version='0.1.0',
31+
description='Timeout timer use signal or thread module, support nested loop',
32+
long_description=long_description(),
33+
author='dozysun',
34+
author_email='dozysun@gmail.com',
35+
url='https://github.com/dozysun/timeout-timer',
36+
packages=['timeout_timer'],
37+
install_requires=[],
38+
classifiers=CLASSIFIERS)

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# coding: utf-8

tests/timeout_timer_test.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# coding: utf-8
2+
from timeout_timer.timeout_timer import TimeoutTimer, TimeoutInterrupt
3+
import time
4+
5+
import pytest
6+
7+
8+
class TimeoutInterruptNested(TimeoutInterrupt):
9+
pass
10+
11+
12+
@pytest.fixture(params=["signal", "thread"])
13+
def timer(request):
14+
return request.param
15+
16+
17+
def sleep(s):
18+
time.sleep(s)
19+
time.sleep(s)
20+
21+
22+
def test_timeout_no_seconds(timer):
23+
is_timeout = None
24+
try:
25+
with TimeoutTimer(0, timer=timer) as f:
26+
f(sleep, 1)
27+
except TimeoutInterrupt:
28+
is_timeout = True
29+
assert not is_timeout
30+
31+
32+
def test_timeout_seconds(timer):
33+
is_timeout = None
34+
try:
35+
with TimeoutTimer(1, timer=timer) as f:
36+
f(sleep, 2)
37+
except TimeoutInterrupt:
38+
is_timeout = True
39+
assert is_timeout
40+
41+
42+
def test_timeout_nested_loop_inside_timeout(timer):
43+
try:
44+
def s():
45+
try:
46+
with TimeoutTimer(2, timer=timer, exception=TimeoutInterruptNested) as f2:
47+
f2(sleep, 3)
48+
except TimeoutInterruptNested:
49+
return True
50+
51+
with TimeoutTimer(10, timer=timer) as f:
52+
is_timeout = f(s)
53+
54+
except TimeoutInterrupt:
55+
is_timeout = "out alert"
56+
assert is_timeout is True
57+
58+
59+
def test_timeout_nested_loop_outsite_timeout(timer):
60+
try:
61+
def s():
62+
try:
63+
with TimeoutTimer(10, timer=timer, exception=TimeoutInterruptNested) as f2:
64+
f2(sleep, 3)
65+
except TimeoutInterruptNested:
66+
return True
67+
68+
with TimeoutTimer(2, timer=timer) as f:
69+
is_timeout = f(s)
70+
71+
except TimeoutInterrupt:
72+
is_timeout = False
73+
assert is_timeout is False
74+
75+
76+
def test_timeout_nested_loop_both_timeout(timer):
77+
cnt = []
78+
try:
79+
def s():
80+
try:
81+
with TimeoutTimer(2, timer=timer, exception=TimeoutInterruptNested) as f2:
82+
f2(sleep, 2)
83+
except TimeoutInterruptNested:
84+
cnt.append(1)
85+
time.sleep(10)
86+
cnt.append(0)
87+
88+
with TimeoutTimer(5, timer=timer) as f:
89+
f(s)
90+
except TimeoutInterrupt:
91+
cnt.append(1)
92+
assert sum(cnt) == 2

timeout_timer/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# coding: utf-8
2+
__version__ = "0.1.0"

0 commit comments

Comments
 (0)