Skip to content

Commit 386d814

Browse files
committed
optimize thread timeout module, maintaining consistent like the signal module, create a sub thread as timer, and call main thread's stop to interrupt,
1 parent d35db47 commit 386d814

File tree

5 files changed

+146
-160
lines changed

5 files changed

+146
-160
lines changed

README.rst

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,39 @@ Installation
1313

1414
Timeout Timer
1515
--------------
16-
Add a timeout function to a function or statement and raise a exception if time limit runs out ,
17-
it support loop nesting, if use signal timer, outside timer will fired after the inside
18-
signal timer finish the work(raise exception or normal finish).
16+
Add a timeout function to a function or statement and raise a exception if time limit runs out, can work as
17+
a context or decorator, support loop nesting and should use diff exception class, if use signal timer,
18+
outside timer will fired after the inside signal timer finish the work(raise exception or normal finish).
1919

20-
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
21-
longer time than time out seconds settled if the timer's sub thread(user's function) is busy in a
22-
system call (time.sleep(), socket.accept()...), exception will fired after system call done.
20+
Support signal timer and thread timer, signal timer can only work on main thread, if not on main thread use
21+
thread timer, thread timer may cost longer time than time out seconds settled if the user's function is busy
22+
in a system call (time.sleep(), socket.accept()...), exception will fired after system call done.
2323

2424
Usage
2525
--------------
2626
support nested loop
2727
::
2828

2929
from timeout_timer import timeout, TimeoutInterrupt
30-
try:
31-
with timeout(2, timer="signal") as f:
32-
f(time.sleep, 0.5)
33-
with timeout(1, timer="signal") as f2:
34-
f2(time.sleep, 2)
35-
except TimeoutInterrupt:
36-
print("timeout triggered")
3730

38-
39-
or use signal timer can simplify
40-
::
41-
42-
try:
43-
with timeout(2) :
44-
time.sleep(3)
45-
except TimeoutInterrupt:
46-
print("timeout triggered")
47-
48-
or use as decorator
31+
class TimeoutInterruptNested(TimeoutInterrupt):
32+
pass
33+
34+
def test_timeout_nested_loop_both_timeout(timer):
35+
cnt = 0
36+
try:
37+
with timeout(5, timer=timer):
38+
try:
39+
with timeout(2, timer=timer, exception=TimeoutInterruptNested):
40+
sleep(2)
41+
except TimeoutInterruptNested:
42+
cnt += 1
43+
time.sleep(10)
44+
except TimeoutInterrupt:
45+
cnt += 1
46+
assert cnt == 2
47+
48+
use as decorator
4949
::
5050

5151
@timeout(2):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def long_description():
2323

2424
setup(
2525
name='timeout-timer',
26-
version='0.1.1',
26+
version='0.2.0',
2727
description='Timeout timer use signal or thread module, support nested loop',
2828
long_description=long_description(),
2929
author='dozysun',

tests/timeout_timer_test.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def sleep(s):
2222
def test_timeout_no_seconds(timer):
2323
is_timeout = None
2424
try:
25-
with timeout(0, timer=timer) as f:
26-
f(sleep, 1)
25+
with timeout(0, timer=timer):
26+
sleep(1)
2727
except TimeoutInterrupt:
2828
is_timeout = True
2929
assert not is_timeout
@@ -32,61 +32,51 @@ def test_timeout_no_seconds(timer):
3232
def test_timeout_seconds(timer):
3333
is_timeout = None
3434
try:
35-
with timeout(1, timer=timer) as f:
36-
f(sleep, 2)
35+
with timeout(1, timer=timer):
36+
sleep(2)
3737
except TimeoutInterrupt:
3838
is_timeout = True
3939
assert is_timeout
4040

4141

4242
def test_timeout_nested_loop_inside_timeout(timer):
43+
is_timeout = None
4344
try:
44-
def s():
45+
with timeout(10, timer=timer):
4546
try:
46-
with timeout(2, timer=timer, exception=TimeoutInterruptNested) as f2:
47-
f2(sleep, 3)
47+
with timeout(2, timer=timer, exception=TimeoutInterruptNested):
48+
sleep(3)
4849
except TimeoutInterruptNested:
49-
return True
50-
51-
with timeout(10, timer=timer) as f:
52-
is_timeout = f(s)
53-
50+
is_timeout = True
5451
except TimeoutInterrupt:
55-
is_timeout = "out alert"
52+
is_timeout = False
5653
assert is_timeout is True
5754

5855

59-
def test_timeout_nested_loop_outsite_timeout(timer):
56+
def test_timeout_nested_loop_outside_timeout(timer):
6057
try:
61-
def s():
58+
with timeout(2, timer=timer):
6259
try:
63-
with timeout(10, timer=timer, exception=TimeoutInterruptNested) as f2:
64-
f2(sleep, 3)
60+
with timeout(10, timer=timer, exception=TimeoutInterruptNested):
61+
sleep(3)
6562
except TimeoutInterruptNested:
66-
return True
67-
68-
with timeout(2, timer=timer) as f:
69-
is_timeout = f(s)
63+
is_timeout = True
7064

7165
except TimeoutInterrupt:
7266
is_timeout = False
7367
assert is_timeout is False
7468

7569

7670
def test_timeout_nested_loop_both_timeout(timer):
77-
cnt = []
71+
cnt = 0
7872
try:
79-
def s():
73+
with timeout(5, timer=timer):
8074
try:
81-
with timeout(2, timer=timer, exception=TimeoutInterruptNested) as f2:
82-
f2(sleep, 2)
75+
with timeout(2, timer=timer, exception=TimeoutInterruptNested):
76+
sleep(2)
8377
except TimeoutInterruptNested:
84-
cnt.append(1)
78+
cnt += 1
8579
time.sleep(10)
86-
cnt.append(0)
87-
88-
with timeout(5, timer=timer) as f:
89-
f(s)
9080
except TimeoutInterrupt:
91-
cnt.append(1)
92-
assert sum(cnt) == 2
81+
cnt += 1
82+
assert cnt == 2

timeout_timer/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding: utf-8
2-
from .timeout_timer import timeout, TimeoutInterrupt, TimeoutTimer, StoppableThread
2+
from .timeout_timer import timeout, TimeoutInterrupt, StoppableThread
33

44
__all__ = ['timeout', 'TimeoutInterrupt']
5-
__version__ = "0.1.0"
5+
__version__ = "0.2.0"

0 commit comments

Comments
 (0)