funboost 44.8__py3-none-any.whl → 44.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of funboost might be problematic. Click here for more details.
- funboost/__init__.py +1 -1
- funboost/constant.py +14 -4
- funboost/consumers/base_consumer.py +48 -20
- funboost/core/booster.py +15 -0
- funboost/core/current_task.py +87 -68
- funboost/core/func_params_model.py +8 -0
- funboost/function_result_web/__pycache__/functions.cpython-39.pyc +0 -0
- funboost/publishers/base_publisher.py +21 -2
- funboost/set_frame_config.py +2 -1
- funboost/utils/class_utils.py +101 -51
- funboost/utils/dependency_packages_in_pythonpath/__pycache__/__init__.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/__pycache__/add_to_pythonpath.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__init__.py +59 -59
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/__init__.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/client.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/compat.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/connection.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/exceptions.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/lock.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/utils.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/client.py +4804 -4804
- funboost/utils/dependency_packages_in_pythonpath/aioredis/compat.py +8 -8
- funboost/utils/dependency_packages_in_pythonpath/aioredis/connection.py +1668 -1668
- funboost/utils/dependency_packages_in_pythonpath/aioredis/exceptions.py +96 -96
- funboost/utils/dependency_packages_in_pythonpath/aioredis/lock.py +306 -306
- funboost/utils/dependency_packages_in_pythonpath/aioredis/log.py +15 -15
- funboost/utils/dependency_packages_in_pythonpath/aioredis/sentinel.py +329 -329
- funboost/utils/dependency_packages_in_pythonpath/aioredis/utils.py +61 -61
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/StoppableThread.py +134 -133
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__init__.py +16 -16
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/StoppableThread.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/__init__.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/dafunc.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/exceptions.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/py3_raise.cpython-39.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/dafunc.py +244 -244
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/dafunc2222.py +244 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/exceptions.py +98 -98
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/py2_raise.py +7 -7
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/py3_raise.py +7 -7
- funboost/utils/times/__init__.py +85 -85
- funboost/utils/times/version.py +1 -1
- {funboost-44.8.dist-info → funboost-44.9.dist-info}/METADATA +1 -2
- {funboost-44.8.dist-info → funboost-44.9.dist-info}/RECORD +48 -76
- {funboost-44.8.dist-info → funboost-44.9.dist-info}/entry_points.txt +0 -1
- funboost/function_result_web/__pycache__/app.cpython-37.pyc +0 -0
- funboost/function_result_web/__pycache__/functions.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/__pycache__/__init__.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/__pycache__/__init__.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/__pycache__/add_to_pythonpath.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/__pycache__/add_to_pythonpath.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/__init__.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/__init__.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/client.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/client.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/compat.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/compat.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/connection.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/connection.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/exceptions.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/exceptions.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/lock.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/lock.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/utils.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/aioredis/__pycache__/utils.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/StoppableThread.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/StoppableThread.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/__init__.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/__init__.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/dafunc.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/dafunc.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/exceptions.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/exceptions.cpython-37.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/py3_raise.cpython-311.pyc +0 -0
- funboost/utils/dependency_packages_in_pythonpath/func_timeout/__pycache__/py3_raise.cpython-37.pyc +0 -0
- {funboost-44.8.dist-info → funboost-44.9.dist-info}/LICENSE +0 -0
- {funboost-44.8.dist-info → funboost-44.9.dist-info}/WHEEL +0 -0
- {funboost-44.8.dist-info → funboost-44.9.dist-info}/top_level.txt +0 -0
|
@@ -1,244 +1,244 @@
|
|
|
1
|
-
|
|
2
|
-
# vim: set ts=4 sw=4 expandtab :
|
|
3
|
-
|
|
4
|
-
'''
|
|
5
|
-
Copyright (c) 2016, 2017 Tim Savannah All Rights Reserved.
|
|
6
|
-
|
|
7
|
-
Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as
|
|
8
|
-
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE
|
|
9
|
-
'''
|
|
10
|
-
|
|
11
|
-
import copy
|
|
12
|
-
import functools
|
|
13
|
-
import inspect
|
|
14
|
-
import threading
|
|
15
|
-
import time
|
|
16
|
-
import types
|
|
17
|
-
import sys
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
from .exceptions import FunctionTimedOut
|
|
21
|
-
from .StoppableThread import StoppableThread
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
from .py3_raise import raise_exception
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
from functools import wraps
|
|
28
|
-
|
|
29
|
-
__all__ = ('func_timeout', 'func_set_timeout')
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def func_timeout(timeout, func, args=(), kwargs=None):
|
|
35
|
-
'''
|
|
36
|
-
func_timeout - Runs the given function for up to #timeout# seconds.
|
|
37
|
-
|
|
38
|
-
Raises any exceptions #func# would raise, returns what #func# would return (unless timeout is exceeded), in which case it raises FunctionTimedOut
|
|
39
|
-
|
|
40
|
-
@param timeout <float> - Maximum number of seconds to run #func# before terminating
|
|
41
|
-
|
|
42
|
-
@param func <function> - The function to call
|
|
43
|
-
|
|
44
|
-
@param args <tuple> - Any ordered arguments to pass to the function
|
|
45
|
-
|
|
46
|
-
@param kwargs <dict/None> - Keyword arguments to pass to the function.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
@raises - FunctionTimedOut if #timeout# is exceeded, otherwise anything #func# could raise will be raised
|
|
50
|
-
|
|
51
|
-
If the timeout is exceeded, FunctionTimedOut will be raised within the context of the called function every two seconds until it terminates,
|
|
52
|
-
but will not block the calling thread (a new thread will be created to perform the join). If possible, you should try/except FunctionTimedOut
|
|
53
|
-
to return cleanly, but in most cases it will 'just work'.
|
|
54
|
-
|
|
55
|
-
@return - The return value that #func# gives
|
|
56
|
-
'''
|
|
57
|
-
|
|
58
|
-
if not kwargs:
|
|
59
|
-
kwargs = {}
|
|
60
|
-
if not args:
|
|
61
|
-
args = ()
|
|
62
|
-
|
|
63
|
-
ret = []
|
|
64
|
-
exception = []
|
|
65
|
-
isStopped = False
|
|
66
|
-
|
|
67
|
-
from funboost.core.current_task import thread_current_task
|
|
68
|
-
|
|
69
|
-
def funcwrap(args2, kwargs2,
|
|
70
|
-
fct = thread_current_task
|
|
71
|
-
fct.
|
|
72
|
-
try:
|
|
73
|
-
ret.append( func(*args2, **kwargs2) )
|
|
74
|
-
except FunctionTimedOut:
|
|
75
|
-
# Don't print traceback to stderr if we time out
|
|
76
|
-
pass
|
|
77
|
-
except BaseException as e:
|
|
78
|
-
exc_info = sys.exc_info()
|
|
79
|
-
if isStopped is False:
|
|
80
|
-
# Assemble the alternate traceback, excluding this function
|
|
81
|
-
# from the trace (by going to next frame)
|
|
82
|
-
# Pytohn3 reads native from __traceback__,
|
|
83
|
-
# python2 has a different form for "raise"
|
|
84
|
-
e.__traceback__ = exc_info[2].tb_next
|
|
85
|
-
exception.append( e )
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
# fct = funboost_current_task()
|
|
89
|
-
thread = StoppableThread(target=funcwrap, args=(args, kwargs,
|
|
90
|
-
thread.daemon = True
|
|
91
|
-
|
|
92
|
-
thread.start()
|
|
93
|
-
thread.join(timeout)
|
|
94
|
-
|
|
95
|
-
stopException = None
|
|
96
|
-
if thread.is_alive():
|
|
97
|
-
isStopped = True
|
|
98
|
-
|
|
99
|
-
class FunctionTimedOutTempType(FunctionTimedOut):
|
|
100
|
-
def __init__(self):
|
|
101
|
-
return FunctionTimedOut.__init__(self, '', timeout, func, args, kwargs)
|
|
102
|
-
|
|
103
|
-
FunctionTimedOutTemp = type('FunctionTimedOut' + str( hash( "%d_%d_%d_%d" %(id(timeout), id(func), id(args), id(kwargs))) ), FunctionTimedOutTempType.__bases__, dict(FunctionTimedOutTempType.__dict__))
|
|
104
|
-
|
|
105
|
-
stopException = FunctionTimedOutTemp
|
|
106
|
-
# raise FunctionTimedOut('', timeout, func, args, kwargs)
|
|
107
|
-
thread._stopThread(stopException)
|
|
108
|
-
thread.join(min(.1, timeout / 50.0))
|
|
109
|
-
raise FunctionTimedOut('', timeout, func, args, kwargs)
|
|
110
|
-
else:
|
|
111
|
-
# We can still cleanup the thread here..
|
|
112
|
-
# Still give a timeout... just... cuz..
|
|
113
|
-
thread.join(.5)
|
|
114
|
-
|
|
115
|
-
if exception:
|
|
116
|
-
raise_exception(exception)
|
|
117
|
-
|
|
118
|
-
if ret:
|
|
119
|
-
return ret[0]
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
def func_set_timeout(timeout, allowOverride=False):
|
|
123
|
-
'''
|
|
124
|
-
func_set_timeout - Decorator to run a function with a given/calculated timeout (max execution time).
|
|
125
|
-
Optionally (if #allowOverride is True), adds a paramater, "forceTimeout", to the
|
|
126
|
-
function which, if provided, will override the default timeout for that invocation.
|
|
127
|
-
|
|
128
|
-
If #timeout is provided as a lambda/function, it will be called
|
|
129
|
-
prior to each invocation of the decorated function to calculate the timeout to be used
|
|
130
|
-
for that call, based on the arguments passed to the decorated function.
|
|
131
|
-
|
|
132
|
-
For example, you may have a "processData" function whose execution time
|
|
133
|
-
depends on the number of "data" elements, so you may want a million elements to have a
|
|
134
|
-
much higher timeout than seven elements.)
|
|
135
|
-
|
|
136
|
-
If #allowOverride is True AND a kwarg of "forceTimeout" is passed to the wrapped function, that timeout
|
|
137
|
-
will be used for that single call.
|
|
138
|
-
|
|
139
|
-
@param timeout <float OR lambda/function> -
|
|
140
|
-
|
|
141
|
-
**If float:**
|
|
142
|
-
Default number of seconds max to allow function to execute
|
|
143
|
-
before throwing FunctionTimedOut
|
|
144
|
-
|
|
145
|
-
**If lambda/function:
|
|
146
|
-
|
|
147
|
-
If a function/lambda is provided, it will be called for every
|
|
148
|
-
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed)
|
|
149
|
-
to determine the timeout to use based on the arguments to the decorated function.
|
|
150
|
-
|
|
151
|
-
The arguments as passed into the decorated function will be passed to this function.
|
|
152
|
-
They either must match exactly to what the decorated function has, OR
|
|
153
|
-
if you prefer to get the *args (list of ordered args) and **kwargs ( key : value keyword args form),
|
|
154
|
-
define your calculate function like:
|
|
155
|
-
|
|
156
|
-
def calculateTimeout(*args, **kwargs):
|
|
157
|
-
...
|
|
158
|
-
|
|
159
|
-
or lambda like:
|
|
160
|
-
|
|
161
|
-
calculateTimeout = lambda *args, **kwargs : ...
|
|
162
|
-
|
|
163
|
-
otherwise the args to your calculate function should match exactly the decorated function.
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
@param allowOverride <bool> Default False, if True adds a keyword argument to the decorated function,
|
|
167
|
-
"forceTimeout" which, if provided, will override the #timeout. If #timeout was provided as a lambda / function, it
|
|
168
|
-
will not be called.
|
|
169
|
-
|
|
170
|
-
@throws FunctionTimedOut If time alloted passes without function returning naturally
|
|
171
|
-
|
|
172
|
-
@see func_timeout
|
|
173
|
-
'''
|
|
174
|
-
# Try to be as efficent as possible... don't compare the args more than once
|
|
175
|
-
|
|
176
|
-
# Helps closure issue on some versions of python
|
|
177
|
-
defaultTimeout = copy.copy(timeout)
|
|
178
|
-
|
|
179
|
-
isTimeoutAFunction = bool( issubclass(timeout.__class__, (types.FunctionType, types.MethodType, types.LambdaType, types.BuiltinFunctionType, types.BuiltinMethodType) ) )
|
|
180
|
-
|
|
181
|
-
if not isTimeoutAFunction:
|
|
182
|
-
if not issubclass(timeout.__class__, (float, int)):
|
|
183
|
-
try:
|
|
184
|
-
timeout = float(timeout)
|
|
185
|
-
except:
|
|
186
|
-
raise ValueError('timeout argument must be a float/int for number of seconds, or a function/lambda which gets passed the function arguments and returns a calculated timeout (as float or int). Passed type: < %s > is not of any of these, and cannot be converted to a float.' %( timeout.__class__.__name__, ))
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
if not allowOverride and not isTimeoutAFunction:
|
|
190
|
-
# Only defaultTimeout provided. Simple function wrapper
|
|
191
|
-
def _function_decorator(func):
|
|
192
|
-
|
|
193
|
-
return wraps(func)(lambda *args, **kwargs : func_timeout(defaultTimeout, func, args=args, kwargs=kwargs))
|
|
194
|
-
|
|
195
|
-
# def _function_wrapper(*args, **kwargs):
|
|
196
|
-
# return func_timeout(defaultTimeout, func, args=args, kwargs=kwargs)
|
|
197
|
-
# return _function_wrapper
|
|
198
|
-
return _function_decorator
|
|
199
|
-
|
|
200
|
-
if not isTimeoutAFunction:
|
|
201
|
-
# allowOverride is True and timeout is not a function. Simple conditional on every call
|
|
202
|
-
def _function_decorator(func):
|
|
203
|
-
def _function_wrapper(*args, **kwargs):
|
|
204
|
-
if 'forceTimeout' in kwargs:
|
|
205
|
-
useTimeout = kwargs.pop('forceTimeout')
|
|
206
|
-
else:
|
|
207
|
-
useTimeout = defaultTimeout
|
|
208
|
-
|
|
209
|
-
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
|
210
|
-
|
|
211
|
-
return wraps(func)(_function_wrapper)
|
|
212
|
-
return _function_decorator
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
# At this point, timeout IS known to be a function.
|
|
216
|
-
timeoutFunction = timeout
|
|
217
|
-
|
|
218
|
-
if allowOverride:
|
|
219
|
-
# Could use a lambda here... but want traceback to highlight the calculate function,
|
|
220
|
-
# and not the invoked function
|
|
221
|
-
def _function_decorator(func):
|
|
222
|
-
def _function_wrapper(*args, **kwargs):
|
|
223
|
-
if 'forceTimeout' in kwargs:
|
|
224
|
-
useTimeout = kwargs.pop('forceTimeout')
|
|
225
|
-
else:
|
|
226
|
-
useTimeout = timeoutFunction(*args, **kwargs)
|
|
227
|
-
|
|
228
|
-
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
|
229
|
-
|
|
230
|
-
return wraps(func)(_function_wrapper)
|
|
231
|
-
return _function_decorator
|
|
232
|
-
|
|
233
|
-
# Cannot override, and calculate timeout function
|
|
234
|
-
def _function_decorator(func):
|
|
235
|
-
def _function_wrapper(*args, **kwargs):
|
|
236
|
-
useTimeout = timeoutFunction(*args, **kwargs)
|
|
237
|
-
|
|
238
|
-
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
|
239
|
-
|
|
240
|
-
return wraps(func)(_function_wrapper)
|
|
241
|
-
return _function_decorator
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
# vim: set ts=4 sw=4 expandtab :
|
|
1
|
+
|
|
2
|
+
# vim: set ts=4 sw=4 expandtab :
|
|
3
|
+
|
|
4
|
+
'''
|
|
5
|
+
Copyright (c) 2016, 2017 Tim Savannah All Rights Reserved.
|
|
6
|
+
|
|
7
|
+
Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as
|
|
8
|
+
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE
|
|
9
|
+
'''
|
|
10
|
+
|
|
11
|
+
import copy
|
|
12
|
+
import functools
|
|
13
|
+
import inspect
|
|
14
|
+
import threading
|
|
15
|
+
import time
|
|
16
|
+
import types
|
|
17
|
+
import sys
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
from .exceptions import FunctionTimedOut
|
|
21
|
+
from .StoppableThread import StoppableThread
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
from .py3_raise import raise_exception
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
from functools import wraps
|
|
28
|
+
|
|
29
|
+
__all__ = ('func_timeout', 'func_set_timeout')
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def func_timeout(timeout, func, args=(), kwargs=None):
|
|
35
|
+
'''
|
|
36
|
+
func_timeout - Runs the given function for up to #timeout# seconds.
|
|
37
|
+
|
|
38
|
+
Raises any exceptions #func# would raise, returns what #func# would return (unless timeout is exceeded), in which case it raises FunctionTimedOut
|
|
39
|
+
|
|
40
|
+
@param timeout <float> - Maximum number of seconds to run #func# before terminating
|
|
41
|
+
|
|
42
|
+
@param func <function> - The function to call
|
|
43
|
+
|
|
44
|
+
@param args <tuple> - Any ordered arguments to pass to the function
|
|
45
|
+
|
|
46
|
+
@param kwargs <dict/None> - Keyword arguments to pass to the function.
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@raises - FunctionTimedOut if #timeout# is exceeded, otherwise anything #func# could raise will be raised
|
|
50
|
+
|
|
51
|
+
If the timeout is exceeded, FunctionTimedOut will be raised within the context of the called function every two seconds until it terminates,
|
|
52
|
+
but will not block the calling thread (a new thread will be created to perform the join). If possible, you should try/except FunctionTimedOut
|
|
53
|
+
to return cleanly, but in most cases it will 'just work'.
|
|
54
|
+
|
|
55
|
+
@return - The return value that #func# gives
|
|
56
|
+
'''
|
|
57
|
+
|
|
58
|
+
if not kwargs:
|
|
59
|
+
kwargs = {}
|
|
60
|
+
if not args:
|
|
61
|
+
args = ()
|
|
62
|
+
|
|
63
|
+
ret = []
|
|
64
|
+
exception = []
|
|
65
|
+
isStopped = False
|
|
66
|
+
|
|
67
|
+
from funboost.core.current_task import thread_current_task
|
|
68
|
+
|
|
69
|
+
def funcwrap(args2, kwargs2,):
|
|
70
|
+
# fct = thread_current_task
|
|
71
|
+
# fct.set_fct_context(fct_context) # 把funboost的消费线程上下文需要传递到超时线程上下文里面来.
|
|
72
|
+
try:
|
|
73
|
+
ret.append( func(*args2, **kwargs2) )
|
|
74
|
+
except FunctionTimedOut:
|
|
75
|
+
# Don't print traceback to stderr if we time out
|
|
76
|
+
pass
|
|
77
|
+
except BaseException as e:
|
|
78
|
+
exc_info = sys.exc_info()
|
|
79
|
+
if isStopped is False:
|
|
80
|
+
# Assemble the alternate traceback, excluding this function
|
|
81
|
+
# from the trace (by going to next frame)
|
|
82
|
+
# Pytohn3 reads native from __traceback__,
|
|
83
|
+
# python2 has a different form for "raise"
|
|
84
|
+
e.__traceback__ = exc_info[2].tb_next
|
|
85
|
+
exception.append( e )
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# fct = funboost_current_task()
|
|
89
|
+
thread = StoppableThread(target=funcwrap, args=(args, kwargs,))
|
|
90
|
+
thread.daemon = True
|
|
91
|
+
|
|
92
|
+
thread.start()
|
|
93
|
+
thread.join(timeout)
|
|
94
|
+
|
|
95
|
+
stopException = None
|
|
96
|
+
if thread.is_alive():
|
|
97
|
+
isStopped = True
|
|
98
|
+
|
|
99
|
+
class FunctionTimedOutTempType(FunctionTimedOut):
|
|
100
|
+
def __init__(self):
|
|
101
|
+
return FunctionTimedOut.__init__(self, '', timeout, func, args, kwargs)
|
|
102
|
+
|
|
103
|
+
FunctionTimedOutTemp = type('FunctionTimedOut' + str( hash( "%d_%d_%d_%d" %(id(timeout), id(func), id(args), id(kwargs))) ), FunctionTimedOutTempType.__bases__, dict(FunctionTimedOutTempType.__dict__))
|
|
104
|
+
|
|
105
|
+
stopException = FunctionTimedOutTemp
|
|
106
|
+
# raise FunctionTimedOut('', timeout, func, args, kwargs)
|
|
107
|
+
thread._stopThread(stopException)
|
|
108
|
+
thread.join(min(.1, timeout / 50.0))
|
|
109
|
+
raise FunctionTimedOut('', timeout, func, args, kwargs)
|
|
110
|
+
else:
|
|
111
|
+
# We can still cleanup the thread here..
|
|
112
|
+
# Still give a timeout... just... cuz..
|
|
113
|
+
thread.join(.5)
|
|
114
|
+
|
|
115
|
+
if exception:
|
|
116
|
+
raise_exception(exception)
|
|
117
|
+
|
|
118
|
+
if ret:
|
|
119
|
+
return ret[0]
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def func_set_timeout(timeout, allowOverride=False):
|
|
123
|
+
'''
|
|
124
|
+
func_set_timeout - Decorator to run a function with a given/calculated timeout (max execution time).
|
|
125
|
+
Optionally (if #allowOverride is True), adds a paramater, "forceTimeout", to the
|
|
126
|
+
function which, if provided, will override the default timeout for that invocation.
|
|
127
|
+
|
|
128
|
+
If #timeout is provided as a lambda/function, it will be called
|
|
129
|
+
prior to each invocation of the decorated function to calculate the timeout to be used
|
|
130
|
+
for that call, based on the arguments passed to the decorated function.
|
|
131
|
+
|
|
132
|
+
For example, you may have a "processData" function whose execution time
|
|
133
|
+
depends on the number of "data" elements, so you may want a million elements to have a
|
|
134
|
+
much higher timeout than seven elements.)
|
|
135
|
+
|
|
136
|
+
If #allowOverride is True AND a kwarg of "forceTimeout" is passed to the wrapped function, that timeout
|
|
137
|
+
will be used for that single call.
|
|
138
|
+
|
|
139
|
+
@param timeout <float OR lambda/function> -
|
|
140
|
+
|
|
141
|
+
**If float:**
|
|
142
|
+
Default number of seconds max to allow function to execute
|
|
143
|
+
before throwing FunctionTimedOut
|
|
144
|
+
|
|
145
|
+
**If lambda/function:
|
|
146
|
+
|
|
147
|
+
If a function/lambda is provided, it will be called for every
|
|
148
|
+
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed)
|
|
149
|
+
to determine the timeout to use based on the arguments to the decorated function.
|
|
150
|
+
|
|
151
|
+
The arguments as passed into the decorated function will be passed to this function.
|
|
152
|
+
They either must match exactly to what the decorated function has, OR
|
|
153
|
+
if you prefer to get the *args (list of ordered args) and **kwargs ( key : value keyword args form),
|
|
154
|
+
define your calculate function like:
|
|
155
|
+
|
|
156
|
+
def calculateTimeout(*args, **kwargs):
|
|
157
|
+
...
|
|
158
|
+
|
|
159
|
+
or lambda like:
|
|
160
|
+
|
|
161
|
+
calculateTimeout = lambda *args, **kwargs : ...
|
|
162
|
+
|
|
163
|
+
otherwise the args to your calculate function should match exactly the decorated function.
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@param allowOverride <bool> Default False, if True adds a keyword argument to the decorated function,
|
|
167
|
+
"forceTimeout" which, if provided, will override the #timeout. If #timeout was provided as a lambda / function, it
|
|
168
|
+
will not be called.
|
|
169
|
+
|
|
170
|
+
@throws FunctionTimedOut If time alloted passes without function returning naturally
|
|
171
|
+
|
|
172
|
+
@see func_timeout
|
|
173
|
+
'''
|
|
174
|
+
# Try to be as efficent as possible... don't compare the args more than once
|
|
175
|
+
|
|
176
|
+
# Helps closure issue on some versions of python
|
|
177
|
+
defaultTimeout = copy.copy(timeout)
|
|
178
|
+
|
|
179
|
+
isTimeoutAFunction = bool( issubclass(timeout.__class__, (types.FunctionType, types.MethodType, types.LambdaType, types.BuiltinFunctionType, types.BuiltinMethodType) ) )
|
|
180
|
+
|
|
181
|
+
if not isTimeoutAFunction:
|
|
182
|
+
if not issubclass(timeout.__class__, (float, int)):
|
|
183
|
+
try:
|
|
184
|
+
timeout = float(timeout)
|
|
185
|
+
except:
|
|
186
|
+
raise ValueError('timeout argument must be a float/int for number of seconds, or a function/lambda which gets passed the function arguments and returns a calculated timeout (as float or int). Passed type: < %s > is not of any of these, and cannot be converted to a float.' %( timeout.__class__.__name__, ))
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
if not allowOverride and not isTimeoutAFunction:
|
|
190
|
+
# Only defaultTimeout provided. Simple function wrapper
|
|
191
|
+
def _function_decorator(func):
|
|
192
|
+
|
|
193
|
+
return wraps(func)(lambda *args, **kwargs : func_timeout(defaultTimeout, func, args=args, kwargs=kwargs))
|
|
194
|
+
|
|
195
|
+
# def _function_wrapper(*args, **kwargs):
|
|
196
|
+
# return func_timeout(defaultTimeout, func, args=args, kwargs=kwargs)
|
|
197
|
+
# return _function_wrapper
|
|
198
|
+
return _function_decorator
|
|
199
|
+
|
|
200
|
+
if not isTimeoutAFunction:
|
|
201
|
+
# allowOverride is True and timeout is not a function. Simple conditional on every call
|
|
202
|
+
def _function_decorator(func):
|
|
203
|
+
def _function_wrapper(*args, **kwargs):
|
|
204
|
+
if 'forceTimeout' in kwargs:
|
|
205
|
+
useTimeout = kwargs.pop('forceTimeout')
|
|
206
|
+
else:
|
|
207
|
+
useTimeout = defaultTimeout
|
|
208
|
+
|
|
209
|
+
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
|
210
|
+
|
|
211
|
+
return wraps(func)(_function_wrapper)
|
|
212
|
+
return _function_decorator
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# At this point, timeout IS known to be a function.
|
|
216
|
+
timeoutFunction = timeout
|
|
217
|
+
|
|
218
|
+
if allowOverride:
|
|
219
|
+
# Could use a lambda here... but want traceback to highlight the calculate function,
|
|
220
|
+
# and not the invoked function
|
|
221
|
+
def _function_decorator(func):
|
|
222
|
+
def _function_wrapper(*args, **kwargs):
|
|
223
|
+
if 'forceTimeout' in kwargs:
|
|
224
|
+
useTimeout = kwargs.pop('forceTimeout')
|
|
225
|
+
else:
|
|
226
|
+
useTimeout = timeoutFunction(*args, **kwargs)
|
|
227
|
+
|
|
228
|
+
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
|
229
|
+
|
|
230
|
+
return wraps(func)(_function_wrapper)
|
|
231
|
+
return _function_decorator
|
|
232
|
+
|
|
233
|
+
# Cannot override, and calculate timeout function
|
|
234
|
+
def _function_decorator(func):
|
|
235
|
+
def _function_wrapper(*args, **kwargs):
|
|
236
|
+
useTimeout = timeoutFunction(*args, **kwargs)
|
|
237
|
+
|
|
238
|
+
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
|
239
|
+
|
|
240
|
+
return wraps(func)(_function_wrapper)
|
|
241
|
+
return _function_decorator
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
# vim: set ts=4 sw=4 expandtab :
|