OLD | NEW |
| (Empty) |
1 # -*- test-case-name: twisted.test.test_timeoutqueue -*- | |
2 # Copyright (c) 2001-2007 Twisted Matrix Laboratories. | |
3 # See LICENSE for details. | |
4 | |
5 """ | |
6 A Queue subclass that supports timeouts. | |
7 """ | |
8 | |
9 # System Imports | |
10 import Queue, time, warnings | |
11 | |
12 | |
13 _time = time.time | |
14 _sleep = time.sleep | |
15 | |
16 | |
17 class TimedOut(Exception): | |
18 pass | |
19 | |
20 | |
21 class TimeoutQueue(Queue.Queue): | |
22 """ | |
23 A thread-safe queue that supports timeouts. | |
24 """ | |
25 | |
26 def __init__(self, max=0): | |
27 warnings.warn("timeoutqueue is deprecated since Twisted 8.0", | |
28 category=DeprecationWarning, stacklevel=2) | |
29 Queue.Queue.__init__(self, max) | |
30 | |
31 def wait(self, timeout): | |
32 """ | |
33 Wait until the queue isn't empty. Raises TimedOut if still empty. | |
34 """ | |
35 endtime = _time() + timeout | |
36 delay = 0.0005 # 500 us -> initial delay of 1 ms | |
37 while 1: | |
38 gotit = not self.empty() | |
39 if gotit: | |
40 break | |
41 remaining = endtime - _time() | |
42 if remaining <= 0: | |
43 raise TimedOut, "timed out." | |
44 delay = min(delay * 2, remaining, .05) | |
45 _sleep(delay) | |
46 | |
47 | |
48 __all__ = ["TimeoutQueue", "TimedOut"] | |
49 | |
OLD | NEW |