1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Functions that can run in or out of coro.
24
25 This module provides emulation for some functions to run whether or not the
26 coro event loop is running.
27 """
28
29 __version__ = '$Revision: #3 $'
30
31 import coro
32 import signal
33 import time
34
37
40
42 """Call a function with a timeout.
43
44 This version supports running even if the coro event loop isn't running by
45 using SIGALRM.
46
47 See `coro._coro.sched.with_timeout` for more detail.
48
49 :Parameters:
50 - `timeout`: The number of seconds to wait before raising the timeout.
51 May be a floating point number.
52 - `function`: The function to call.
53
54 :Return:
55 Returns the return value of the function.
56
57 :Exceptions:
58 - `coro.TimeoutError`: The timeout expired.
59 """
60 if coro.coro_is_running():
61 return coro.with_timeout(timeout, function, *args, **kwargs)
62 else:
63
64 old_sigalrm_handler = signal.signal(signal.SIGALRM, _shutdown_sigalrm_handler)
65 try:
66 try:
67 signal.alarm(timeout)
68 return function(*args, **kwargs)
69 except _shutdown_sigalrm_exc:
70 raise coro.TimeoutError
71 finally:
72 signal.alarm(0)
73 signal.signal(signal.SIGALRM, old_sigalrm_handler)
74
76 """Sleep for a period of time.
77
78 :Parameters:
79 - `delta`: The number of seconds to sleep.
80 """
81 time.sleep(delta)
82