Compare commits
49 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42473ad7ff | ||
|
|
eeda3385eb | ||
|
|
50baa8db50 | ||
|
|
45cdbdfe4f | ||
|
|
514225621f | ||
|
|
5452efab5b | ||
|
|
5d2afb9582 | ||
|
|
af7d6e8a39 | ||
|
|
cf8a9545bc | ||
|
|
176ef7b9d9 | ||
|
|
1eba58f599 | ||
|
|
f759dbfeec | ||
|
|
82493fe61d | ||
|
|
945b971637 | ||
|
|
64c3098922 | ||
|
|
3fc998c3ca | ||
|
|
caffd41f95 | ||
|
|
1722f34da2 | ||
|
|
bdc7072176 | ||
|
|
e90d850871 | ||
|
|
8ab818f94a | ||
|
|
317a65ce08 | ||
|
|
5f9a1e493a | ||
|
|
6e869794ef | ||
|
|
84528e67ed | ||
|
|
81759d9b2b | ||
|
|
515988d9fc | ||
|
|
ac5eb257b0 | ||
|
|
45244ce98e | ||
|
|
549251b2a3 | ||
|
|
cdd2d6d34f | ||
|
|
5ab59fd709 | ||
|
|
c52d7181d6 | ||
|
|
874bc9b576 | ||
|
|
92dc986332 | ||
|
|
dc5fcfe507 | ||
|
|
f7f761789c | ||
|
|
f793e36dc6 | ||
|
|
089172fe0c | ||
|
|
b427da2517 | ||
|
|
00685ec546 | ||
|
|
2bd33c12fd | ||
|
|
5e15db92b6 | ||
|
|
e99d1d8957 | ||
|
|
0e43f0b161 | ||
|
|
f21d4b4b49 | ||
|
|
6bee825db7 | ||
|
|
19497ca229 | ||
|
|
4190710ec4 |
43
ChangeLog
43
ChangeLog
@ -1,3 +1,46 @@
|
||||
|
||||
* 4.3.5 - Aug 19 2019
|
||||
|
||||
- Increase times in unit tests to ensure passing on slow systems
|
||||
|
||||
- Allow instantiating FunctionTimedOut exception without arguments ( will replace function name with "Unknown Function" and timedOutAfter with "Unknown" when absent)
|
||||
|
||||
- Update runTests.py from latest GoodTests distrib, 3.0.5 from 2.1.1
|
||||
|
||||
|
||||
* 4.3.4 - Aug 19 2019
|
||||
|
||||
- Merge patch by Rafal Florczak to use threading.Thread.is_alive vs now deprecated threading.Thread.isAlive
|
||||
|
||||
- Regenerate docs
|
||||
|
||||
|
||||
* 4.3.3 - May 13 2019
|
||||
|
||||
- More documentation updates
|
||||
|
||||
|
||||
* 4.3.2 - May 13 2019
|
||||
|
||||
- Update docs, update README, note that this still works with Python 3.7
|
||||
|
||||
- Update link to pydocs to be on githubpreview, as python hosted is broke right now.
|
||||
|
||||
|
||||
* 4.3.0 - Aug 10 2017
|
||||
|
||||
- Ensure that functions wrapped with @func_set_timeout do not lose meta
|
||||
information (such as name, docstrings, etc.). Special thanks to belongwqz on
|
||||
github for noting the issue and suggesting functools.wraps
|
||||
|
||||
|
||||
* 4.2.0 - Jun 4 2017
|
||||
- Add "stop" method to StoppableThread (same as previous _stopThread method -
|
||||
but with a 'public' name)
|
||||
- Document StoppableThread a lot more
|
||||
- Add "raiseEvery" to StoppableThread.stop to control the "repeatEvery" arg to
|
||||
JoinThread (how often after the first raise the exception is retried)
|
||||
|
||||
* 4.1.0 - May 24 2017
|
||||
|
||||
- If func_timeout completes the function call in the alloted time, explicitly
|
||||
|
||||
210
README.md
210
README.md
@ -2,10 +2,12 @@
|
||||
Python module to support running any existing function with a given timeout.
|
||||
|
||||
|
||||
Package Includes
|
||||
----------------
|
||||
Function Timeout
|
||||
================
|
||||
|
||||
**func\_timeout**
|
||||
|
||||
func\_timeout
|
||||
-------------
|
||||
|
||||
This is the function wherein you pass the timeout, the function you want to call, and any arguments, and it runs it for up to #timeout# seconds, and will return/raise anything the passed function would otherwise return or raise.
|
||||
|
||||
@ -25,38 +27,10 @@ This is the function wherein you pass the timeout, the function you want to call
|
||||
@return - The return value that #func# gives
|
||||
'''
|
||||
|
||||
**func\_set\_timeout**
|
||||
|
||||
This is a decorator you can use on functions to apply func\_timeout.
|
||||
|
||||
Takes two arguments, "timeout" and "allowOverride"
|
||||
|
||||
If "allowOverride" is present, an optional keyword argument is added to the wrapped function, 'forceTimeout'. When provided, this will override the timeout used on this function.
|
||||
**Example**
|
||||
|
||||
|
||||
The "timeout" parameter can be either a number (for a fixed timeout), or a function/lambda. If a function/lambda is used, it will be passed the same arguments as the called function was passed. It should return a number which will be used as the timeout for that paticular run. For example, if you have a method that calculates data, you'll want a higher timeout for 1 million records than 50 records.
|
||||
|
||||
Example:
|
||||
|
||||
@func_set_timeout(2.5)
|
||||
def myFunction(self, arg1, arg2):
|
||||
...
|
||||
|
||||
|
||||
**FunctionTimedOut**
|
||||
|
||||
Exception raised if the function times out.
|
||||
|
||||
|
||||
Has a "retry" method which takes the following arguments:
|
||||
|
||||
* No argument - Retry same args, same function, same timeout
|
||||
* Number argument - Retry same args, same function, provided timeout
|
||||
* None - Retry same args, same function, no timeout
|
||||
|
||||
|
||||
Example
|
||||
-------
|
||||
So, for esxample, if you have a function "doit('arg1', 'arg2')" that you want to limit to running for 5 seconds, with func\_timeout you can call it like this:
|
||||
|
||||
|
||||
@ -74,6 +48,41 @@ So, for esxample, if you have a function "doit('arg1', 'arg2')" that you want to
|
||||
# Handle any exceptions that doit might raise here
|
||||
|
||||
|
||||
|
||||
func\_set\_timeout
|
||||
------------------
|
||||
|
||||
|
||||
This is a decorator you can use on functions to apply func\_timeout.
|
||||
|
||||
Takes two arguments, "timeout" and "allowOverride"
|
||||
|
||||
If "allowOverride" is present, an optional keyword argument is added to the wrapped function, 'forceTimeout'. When provided, this will override the timeout used on this function.
|
||||
|
||||
|
||||
The "timeout" parameter can be either a number (for a fixed timeout), or a function/lambda. If a function/lambda is used, it will be passed the same arguments as the called function was passed. It should return a number which will be used as the timeout for that paticular run. For example, if you have a method that calculates data, you'll want a higher timeout for 1 million records than 50 records.
|
||||
|
||||
|
||||
**Example:**
|
||||
|
||||
@func_set_timeout(2.5)
|
||||
def myFunction(self, arg1, arg2):
|
||||
...
|
||||
|
||||
|
||||
FunctionTimedOut
|
||||
----------------
|
||||
|
||||
Exception raised if the function times out.
|
||||
|
||||
|
||||
Has a "retry" method which takes the following arguments:
|
||||
|
||||
* No argument - Retry same args, same function, same timeout
|
||||
* Number argument - Retry same args, same function, provided timeout
|
||||
* None - Retry same args, same function, no timeout
|
||||
|
||||
|
||||
How it works
|
||||
------------
|
||||
|
||||
@ -83,16 +92,143 @@ If there is a return or an exception raised, it will be returned/raised as norma
|
||||
If the timeout has exceeded, the "FunctionTimedOut" exception will be raised in the context of the function being called, as well as from the context of "func\_timeout". You should have your function catch the "FunctionTimedOut" exception and exit cleanly if possible. Every 2 seconds until your function is terminated, it will continue to raise FunctionTimedOut. The terminating of the timed-out function happens in the context of the thread and will not block main execution.
|
||||
|
||||
|
||||
Pydoc
|
||||
-----
|
||||
StoppableThread
|
||||
===============
|
||||
|
||||
Find pydoc at https://pythonhosted.org/func_timeout
|
||||
StoppableThread is a subclass of threading.Thread, which supports stopping the thread (supports both python2 and python3). It will work to stop even in C code.
|
||||
|
||||
The way it works is that you pass it an exception, and it raises it via the cpython api (So the next time a "python" function is called from C api, or the next line is processed in python code, the exception is raised).
|
||||
|
||||
|
||||
Using StoppableThread
|
||||
---------------------
|
||||
|
||||
You can use StoppableThread one of two ways:
|
||||
|
||||
**As a Parent Class**
|
||||
|
||||
|
||||
Your thread can extend func\_timeout.StoppableThread\.StoppableThread and implement the "run" method, same as a normal thread.
|
||||
|
||||
|
||||
from func_timeout.StoppableThread import StoppableThread
|
||||
|
||||
class MyThread(StoppableThread):
|
||||
|
||||
def run(self):
|
||||
|
||||
# Code here
|
||||
return
|
||||
|
||||
|
||||
Then, you can create and start this thread like:
|
||||
|
||||
myThread = MyThread()
|
||||
|
||||
# Uncomment next line to start thread in "daemon mode" -- i.e. will terminate/join automatically upon main thread exit
|
||||
|
||||
#myThread.daemon = True
|
||||
|
||||
myThread.start()
|
||||
|
||||
|
||||
Then, at any time during the thread's execution, you can call \.stop( StopExceptionType ) to stop it ( more in "Stopping a Thread" below
|
||||
|
||||
**Direct Thread To Execute A Function**
|
||||
|
||||
Alternatively, you can instantiate StoppableThread directly and pass the "target", "args", and "kwargs" arguments to the constructor
|
||||
|
||||
myThread = StoppableThread( target=myFunction, args=('ordered', 'args', 'here'), kwargs={ 'keyword args' : 'here' } )
|
||||
|
||||
# Uncomment next line to start thread in "daemon mode" -- i.e. will terminate/join automatically upon main thread exit
|
||||
|
||||
#myThread.daemon = True
|
||||
|
||||
myThread.start()
|
||||
|
||||
|
||||
This will allow you to call functions in stoppable threads, for example handlers in an event loop, which can be stopped later via the \.stop() method.
|
||||
|
||||
|
||||
Stopping a Thread
|
||||
-----------------
|
||||
|
||||
|
||||
The *StoppableThread* class (you must extend this for your thread) adds a function, *stop*, which can be called to stop the thread.
|
||||
|
||||
|
||||
def stop(self, exception, raiseEvery=2.0):
|
||||
'''
|
||||
Stops the thread by raising a given exception.
|
||||
|
||||
@param exception <Exception type> - Exception to throw. Likely, you want to use something
|
||||
|
||||
that inherits from BaseException (so except Exception as e: continue; isn't a problem)
|
||||
|
||||
This should be a class/type, NOT an instance, i.e. MyExceptionType not MyExceptionType()
|
||||
|
||||
|
||||
@param raiseEvery <float> Default 2.0 - We will keep raising this exception every #raiseEvery seconds,
|
||||
|
||||
until the thread terminates.
|
||||
|
||||
If your code traps a specific exception type, this will allow you #raiseEvery seconds to cleanup before exit.
|
||||
|
||||
If you're calling third-party code you can't control, which catches BaseException, set this to a low number
|
||||
|
||||
to break out of their exception handler.
|
||||
|
||||
|
||||
@return <None>
|
||||
'''
|
||||
|
||||
|
||||
The "exception" param must be a type, and it must be instantiable with no arguments (i.e. MyExceptionType() must create the object).
|
||||
|
||||
Consider using a custom exception type which extends BaseException, which you can then use to do basic cleanup ( flush any open files, etc. ).
|
||||
|
||||
The exception type you pass will be raised every #raiseEvery seconds in the context of that stoppable thread. You can tweak this value to give yourself more time for cleanups, or you can shrink it down to break out of empty exception handlers ( try/except with bare except ).
|
||||
|
||||
|
||||
**Notes on Exception Type**
|
||||
|
||||
It is recommended that you create an exception that extends BaseException instead of Exception, otherwise code like this will never stop:
|
||||
|
||||
while True:
|
||||
try:
|
||||
doSomething()
|
||||
except Exception as e:
|
||||
continue
|
||||
|
||||
If you can't avoid such code (third-party lib?) you can set the "repeatEvery" to a very very low number (like .00001 ), so hopefully it will raise, go to the except clause, and then raise again before "continue" is hit.
|
||||
|
||||
|
||||
|
||||
You may want to consider using singleton types with fixed error messages, so that tracebacks, etc. log that the call timed out.
|
||||
|
||||
For example:
|
||||
|
||||
class ServerShutdownExceptionType(BaseException):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
BaseException.__init__(self, 'Server is shutting down')
|
||||
|
||||
|
||||
This will force 'Server is shutting down' as the message held by this exception.
|
||||
|
||||
|
||||
|
||||
Pydoc
|
||||
=====
|
||||
|
||||
Find the latest pydoc at http://htmlpreview.github.io/?https://github.com/kata198/func_timeout/blob/master/doc/func_timeout.html?vers=4.3.5 .
|
||||
|
||||
|
||||
Support
|
||||
-------
|
||||
=======
|
||||
|
||||
I've tested func\_timeout with python 2.7, 3.4, 3.5, 3.6. It should work on other versions as well.
|
||||
I've tested func\_timeout with python 2.7, 3.4, 3.5, 3.6, 3.7. It should work on other versions as well.
|
||||
|
||||
Works on windows, linux/unix, cygwin, mac
|
||||
|
||||
|
||||
248
README.rst
248
README.rst
@ -1,45 +1,70 @@
|
||||
func_timeout
|
||||
func\_timeout
|
||||
=============
|
||||
|
||||
Python module to support running any existing function with a given timeout.
|
||||
|
||||
|
||||
Package Includes
|
||||
----------------
|
||||
Function Timeout
|
||||
================
|
||||
|
||||
**func_timeout**
|
||||
|
||||
func\_timeout
|
||||
-------------
|
||||
|
||||
This is the function wherein you pass the timeout, the function you want to call, and any arguments, and it runs it for up to #timeout# seconds, and will return/raise anything the passed function would otherwise return or raise.
|
||||
|
||||
def func_timeout(timeout, func, args=(), kwargs=None):
|
||||
def func\_timeout(timeout, func, args=(), kwargs=None):
|
||||
|
||||
'''
|
||||
|
||||
func_timeout - Runs the given function for up to #timeout# seconds.
|
||||
|
||||
func\_timeout \- Runs the given function for up to #timeout# seconds.
|
||||
|
||||
Raises any exceptions #func# would raise, returns what #func# would return (unless timeout is exceeded), in which case it raises FunctionTimedOut
|
||||
|
||||
@param timeout <float> \- Maximum number of seconds to run #func# before terminating
|
||||
|
||||
@param timeout <float> - Maximum number of seconds to run #func# before terminating
|
||||
@param func <function> \- The function to call
|
||||
|
||||
@param func <function> - The function to call
|
||||
@param args <tuple> \- Any ordered arguments to pass to the function
|
||||
|
||||
@param args <tuple> - Any ordered arguments to pass to the function
|
||||
@param kwargs <dict/None> \- Keyword arguments to pass to the function.
|
||||
|
||||
@param kwargs <dict/None> - Keyword arguments to pass to the function.
|
||||
@raises \- FunctionTimedOut if #timeout# is exceeded, otherwise anything #func# could raise will be raised
|
||||
|
||||
|
||||
@raises - FunctionTimedOut if #timeout# is exceeded, otherwise anything #func# could raise will be raised
|
||||
|
||||
|
||||
@return - The return value that #func# gives
|
||||
@return \- The return value that #func# gives
|
||||
|
||||
'''
|
||||
|
||||
|
||||
**func_set_timeout**
|
||||
**Example**
|
||||
|
||||
This is a decorator you can use on functions to apply func_timeout.
|
||||
|
||||
So, for esxample, if you have a function "doit('arg1', 'arg2')" that you want to limit to running for 5 seconds, with func\_timeout you can call it like this:
|
||||
|
||||
|
||||
from func\_timeout import func\_timeout, FunctionTimedOut
|
||||
|
||||
...
|
||||
|
||||
try:
|
||||
|
||||
doitReturnValue = func\_timeout(5, doit, args=('arg1', 'arg2'))
|
||||
|
||||
except FunctionTimedOut:
|
||||
|
||||
print ( "doit('arg1', 'arg2') could not complete within 5 seconds and was terminated.\\n")
|
||||
|
||||
except Exception as e:
|
||||
|
||||
# Handle any exceptions that doit might raise here
|
||||
|
||||
|
||||
|
||||
func\_set\_timeout
|
||||
------------------
|
||||
|
||||
|
||||
This is a decorator you can use on functions to apply func\_timeout.
|
||||
|
||||
Takes two arguments, "timeout" and "allowOverride"
|
||||
|
||||
@ -48,75 +73,190 @@ If "allowOverride" is present, an optional keyword argument is added to the wrap
|
||||
|
||||
The "timeout" parameter can be either a number (for a fixed timeout), or a function/lambda. If a function/lambda is used, it will be passed the same arguments as the called function was passed. It should return a number which will be used as the timeout for that paticular run. For example, if you have a method that calculates data, you'll want a higher timeout for 1 million records than 50 records.
|
||||
|
||||
Example:
|
||||
|
||||
@func_set_timeout(2.5)
|
||||
**Example:**
|
||||
|
||||
@func\_set\_timeout(2.5)
|
||||
|
||||
def myFunction(self, arg1, arg2):
|
||||
|
||||
...
|
||||
|
||||
|
||||
**FunctionTimedOut**
|
||||
FunctionTimedOut
|
||||
----------------
|
||||
|
||||
Exception raised if the function times out.
|
||||
|
||||
|
||||
Has a "retry" method which takes the following arguments:
|
||||
|
||||
* No argument - Retry same args, same function, same timeout
|
||||
\* No argument \- Retry same args, same function, same timeout
|
||||
|
||||
* Number argument - Retry same args, same function, provided timeout
|
||||
\* Number argument \- Retry same args, same function, provided timeout
|
||||
|
||||
* None - Retry same args, same function, no timeout
|
||||
\* None \- Retry same args, same function, no timeout
|
||||
|
||||
Example
|
||||
-------
|
||||
So, for esxample, if you have a function "doit('arg1', 'arg2')" that you want to limit to running for 5 seconds, with func_timeout you can call it like this:
|
||||
|
||||
|
||||
from func_timeout import func_timeout, FunctionTimedOut
|
||||
|
||||
|
||||
...
|
||||
|
||||
|
||||
try:
|
||||
|
||||
|
||||
doitReturnValue = func_timeout(5, doit, args=('arg1', 'arg2'))
|
||||
|
||||
|
||||
except FunctionTimedOut:
|
||||
|
||||
print ( "doit('arg1', 'arg2') could not complete within 5 seconds and was terminated.\n")
|
||||
|
||||
except Exception as e:
|
||||
|
||||
# Handle any exceptions that doit might raise here
|
||||
|
||||
How it works
|
||||
------------
|
||||
|
||||
func_timeout will run the specified function in a thread with the specified arguments until it returns, raises an exception, or the timeout is exceeded.
|
||||
func\_timeout will run the specified function in a thread with the specified arguments until it returns, raises an exception, or the timeout is exceeded.
|
||||
|
||||
If there is a return or an exception raised, it will be returned/raised as normal.
|
||||
|
||||
If the timeout has exceeded, the "FunctionTimedOut" exception will be raised in the context of the function being called, as well as from the context of "func_timeout". You should have your function catch the "FunctionTimedOut" exception and exit cleanly if possible. Every 2 seconds until your function is terminated, it will continue to raise FunctionTimedOut. The terminating of the timed-out function happens in the context of the thread and will not block main execution.
|
||||
If the timeout has exceeded, the "FunctionTimedOut" exception will be raised in the context of the function being called, as well as from the context of "func\_timeout". You should have your function catch the "FunctionTimedOut" exception and exit cleanly if possible. Every 2 seconds until your function is terminated, it will continue to raise FunctionTimedOut. The terminating of the timed-out function happens in the context of the thread and will not block main execution.
|
||||
|
||||
|
||||
StoppableThread
|
||||
===============
|
||||
|
||||
StoppableThread is a subclass of threading.Thread, which supports stopping the thread (supports both python2 and python3). It will work to stop even in C code.
|
||||
|
||||
The way it works is that you pass it an exception, and it raises it via the cpython api (So the next time a "python" function is called from C api, or the next line is processed in python code, the exception is raised).
|
||||
|
||||
|
||||
Using StoppableThread
|
||||
---------------------
|
||||
|
||||
You can use StoppableThread one of two ways:
|
||||
|
||||
**As a Parent Class**
|
||||
|
||||
|
||||
Your thread can extend func\_timeout.StoppableThread\.StoppableThread and implement the "run" method, same as a normal thread.
|
||||
|
||||
|
||||
from func\_timeout.StoppableThread import StoppableThread
|
||||
|
||||
class MyThread(StoppableThread):
|
||||
|
||||
def run(self):
|
||||
|
||||
# Code here
|
||||
|
||||
return
|
||||
|
||||
|
||||
Then, you can create and start this thread like:
|
||||
|
||||
myThread = MyThread()
|
||||
|
||||
# Uncomment next line to start thread in "daemon mode" \-\- i.e. will terminate/join automatically upon main thread exit
|
||||
|
||||
#myThread.daemon = True
|
||||
|
||||
myThread.start()
|
||||
|
||||
|
||||
Then, at any time during the thread's execution, you can call \.stop( StopExceptionType ) to stop it ( more in "Stopping a Thread" below
|
||||
|
||||
**Direct Thread To Execute A Function**
|
||||
|
||||
Alternatively, you can instantiate StoppableThread directly and pass the "target", "args", and "kwargs" arguments to the constructor
|
||||
|
||||
myThread = StoppableThread( target=myFunction, args=('ordered', 'args', 'here'), kwargs={ 'keyword args' : 'here' } )
|
||||
|
||||
# Uncomment next line to start thread in "daemon mode" \-\- i.e. will terminate/join automatically upon main thread exit
|
||||
|
||||
#myThread.daemon = True
|
||||
|
||||
myThread.start()
|
||||
|
||||
|
||||
This will allow you to call functions in stoppable threads, for example handlers in an event loop, which can be stopped later via the \.stop() method.
|
||||
|
||||
|
||||
Stopping a Thread
|
||||
-----------------
|
||||
|
||||
|
||||
The *StoppableThread* class (you must extend this for your thread) adds a function, *stop*, which can be called to stop the thread.
|
||||
|
||||
|
||||
def stop(self, exception, raiseEvery=2.0):
|
||||
|
||||
'''
|
||||
|
||||
Stops the thread by raising a given exception.
|
||||
|
||||
@param exception <Exception type> \- Exception to throw. Likely, you want to use something
|
||||
|
||||
that inherits from BaseException (so except Exception as e: continue; isn't a problem)
|
||||
|
||||
This should be a class/type, NOT an instance, i.e. MyExceptionType not MyExceptionType()
|
||||
|
||||
|
||||
@param raiseEvery <float> Default 2.0 \- We will keep raising this exception every #raiseEvery seconds,
|
||||
|
||||
until the thread terminates.
|
||||
|
||||
If your code traps a specific exception type, this will allow you #raiseEvery seconds to cleanup before exit.
|
||||
|
||||
If you're calling third\-party code you can't control, which catches BaseException, set this to a low number
|
||||
|
||||
to break out of their exception handler.
|
||||
|
||||
|
||||
@return <None>
|
||||
|
||||
'''
|
||||
|
||||
|
||||
The "exception" param must be a type, and it must be instantiable with no arguments (i.e. MyExceptionType() must create the object).
|
||||
|
||||
Consider using a custom exception type which extends BaseException, which you can then use to do basic cleanup ( flush any open files, etc. ).
|
||||
|
||||
The exception type you pass will be raised every #raiseEvery seconds in the context of that stoppable thread. You can tweak this value to give yourself more time for cleanups, or you can shrink it down to break out of empty exception handlers ( try/except with bare except ).
|
||||
|
||||
|
||||
**Notes on Exception Type**
|
||||
|
||||
It is recommended that you create an exception that extends BaseException instead of Exception, otherwise code like this will never stop:
|
||||
|
||||
while True:
|
||||
|
||||
try:
|
||||
|
||||
doSomething()
|
||||
|
||||
except Exception as e:
|
||||
|
||||
continue
|
||||
|
||||
If you can't avoid such code (third-party lib?) you can set the "repeatEvery" to a very very low number (like .00001 ), so hopefully it will raise, go to the except clause, and then raise again before "continue" is hit.
|
||||
|
||||
|
||||
|
||||
You may want to consider using singleton types with fixed error messages, so that tracebacks, etc. log that the call timed out.
|
||||
|
||||
For example:
|
||||
|
||||
class ServerShutdownExceptionType(BaseException):
|
||||
|
||||
def \_\_init\_\_(self, \*args, \*\*kwargs):
|
||||
|
||||
BaseException.\_\_init\_\_(self, 'Server is shutting down')
|
||||
|
||||
|
||||
This will force 'Server is shutting down' as the message held by this exception.
|
||||
|
||||
|
||||
|
||||
Pydoc
|
||||
-----
|
||||
=====
|
||||
|
||||
Find pydoc at https://pythonhosted.org/func_timeout
|
||||
Find the latest pydoc at http://htmlpreview.github.io/?https://github.com/kata198/func_timeout/blob/master/doc/func_timeout.html?vers=4.3.5 .
|
||||
|
||||
|
||||
Support
|
||||
-------
|
||||
=======
|
||||
|
||||
I've tested func_timeout with python 2.7, 3.4, 3.5, 3.6. It should work on other versions as well.
|
||||
I've tested func\_timeout with python 2.7, 3.4, 3.5, 3.6, 3.7. It should work on other versions as well.
|
||||
|
||||
Works on windows, linux/unix, cygwin, mac
|
||||
|
||||
ChangeLog can be found at https://raw.githubusercontent.com/kata198/func_timeout/master/ChangeLog
|
||||
|
||||
Pydoc can be found at: http://htmlpreview.github.io/?https://github.com/kata198/func_timeout/blob/master/doc/func_timeout.html?vers=1
|
||||
|
||||
|
||||
@ -1,306 +1,174 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||||
<html ><head ><title >Python: module func_timeout.StoppableThread</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
</head><body bgcolor="#f0f0f8" >
|
||||
<html><head><title>Python: class StoppableThread</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
</head><body bgcolor="#f0f0f8">
|
||||
<p>
|
||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||||
<tr bgcolor="#ffc8d8">
|
||||
<td colspan=3 valign=bottom> <br>
|
||||
<font color="#000000" face="helvetica, arial"><strong>func_timeout.StoppableThread</strong> = <a name="func_timeout.StoppableThread">class StoppableThread</a>(<a href="threading.html#Thread">threading.Thread</a>)</font></td></tr>
|
||||
|
||||
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
|
||||
<td colspan=2><tt>func_timeout.StoppableThread(group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)<br>
|
||||
<br>
|
||||
StoppableThread - A thread that can be stopped by forcing an exception in the execution context.<br>
|
||||
<br>
|
||||
This works both to interrupt code that is in C or in python code, at either the next call to a python function,<br>
|
||||
or the next line in python code.<br>
|
||||
<br>
|
||||
It is recommended that if you call stop ( @see StoppableThread.stop ) that you use an exception that inherits BaseException, to ensure it likely isn't caught.<br>
|
||||
<br>
|
||||
Also, beware unmarked exception handlers in your code. Code like this:<br>
|
||||
<br>
|
||||
while True:<br>
|
||||
try:<br>
|
||||
doSomething()<br>
|
||||
except:<br>
|
||||
continue<br>
|
||||
<br>
|
||||
will never be able to abort, because the exception you raise is immediately caught.<br>
|
||||
<br>
|
||||
The exception is raised over and over, with a specifed delay (default 2.0 seconds)<br> </tt></td></tr>
|
||||
<tr><td> </td>
|
||||
<td width="100%"><dl><dt>Method resolution order:</dt>
|
||||
<dd><a href="func_timeout.StoppableThread.html#StoppableThread">StoppableThread</a></dd>
|
||||
<dd><a href="threading.html#Thread">threading.Thread</a></dd>
|
||||
<dd><a href="builtins.html#object">builtins.object</a></dd>
|
||||
</dl>
|
||||
<hr>
|
||||
Methods defined here:<br>
|
||||
<dl><dt><a name="StoppableThread-stop"><strong>stop</strong></a>(self, exception, raiseEvery=2.0)</dt><dd><tt>Stops the thread by raising a given exception.<br>
|
||||
<br>
|
||||
@param exception <Exception type> - Exception to throw. Likely, you want to use something<br>
|
||||
<br>
|
||||
that inherits from BaseException (so except Exception as e: continue; isn't a problem)<br>
|
||||
<br>
|
||||
This should be a class/type, NOT an instance, i.e. MyExceptionType not MyExceptionType()<br>
|
||||
<br>
|
||||
<br>
|
||||
@param raiseEvery <float> Default 2.0 - We will keep raising this exception every #raiseEvery seconds,<br>
|
||||
<br>
|
||||
until the thread terminates.<br>
|
||||
<br>
|
||||
If your code traps a specific exception type, this will allow you #raiseEvery seconds to cleanup before exit.<br>
|
||||
<br>
|
||||
If you're calling third-party code you can't control, which catches BaseException, set this to a low number<br>
|
||||
<br>
|
||||
to break out of their exception handler.<br>
|
||||
<br>
|
||||
<br>
|
||||
@return <None></tt></dd></dl>
|
||||
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="heading" >
|
||||
<tr bgcolor="#7799ee" >
|
||||
<td valign="bottom" > <br />
|
||||
<font color="#ffffff" face="helvetica, arial" > <br /><big ><big ><strong ><a href="func_timeout.html" ><font color="#ffffff" >func_timeout</font></a>.StoppableThread</strong></big></big></font></td><td align="right" valign="bottom" ><font color="#ffffff" face="helvetica, arial" ><a href="func_timeout.html" >index</a></font></td></tr></table>
|
||||
<p ><tt >Copyright (c) 2016, 2017 Tim Savannah All Rights Reserved.<br />
|
||||
<br />
|
||||
Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as<br />
|
||||
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE</tt></p>
|
||||
<p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
<tr bgcolor="#aa55cc" >
|
||||
<td colspan="3" valign="bottom" > <br />
|
||||
<font color="#ffffff" face="helvetica, arial" ><big ><strong >Modules</strong></big></font></td></tr>
|
||||
|
||||
<tr ><td bgcolor="#aa55cc" ><tt > </tt></td><td > </td>
|
||||
<td width="100%" ><table width="100%" summary="list" ><tr ><td width="25%" valign="top" ><a href="ctypes.html" >ctypes</a><br />
|
||||
<a href="os.html" >os</a><br />
|
||||
</td><td width="25%" valign="top" ><a href="sys.html" >sys</a><br />
|
||||
<a href="threading.html" >threading</a><br />
|
||||
</td><td width="25%" valign="top" ><a href="time.html" >time</a><br />
|
||||
</td><td width="25%" valign="top" ></td></tr></table></td></tr></table><p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
<tr bgcolor="#ee77aa" >
|
||||
<td colspan="3" valign="bottom" > <br />
|
||||
<font color="#ffffff" face="helvetica, arial" ><big ><strong >Classes</strong></big></font></td></tr>
|
||||
|
||||
<tr ><td bgcolor="#ee77aa" ><tt > </tt></td><td > </td>
|
||||
<td width="100%" ><dl >
|
||||
<dt ><font face="helvetica, arial" ><a href="threading.html#Thread" >threading.Thread</a>(<a href="builtins.html#object" >builtins.object</a>)
|
||||
</font></dt><dd >
|
||||
<dl >
|
||||
<dt ><font face="helvetica, arial" ><a href="func_timeout.StoppableThread.html#JoinThread" >JoinThread</a>
|
||||
</font></dt><dt ><font face="helvetica, arial" ><a href="func_timeout.StoppableThread.html#StoppableThread" >StoppableThread</a>
|
||||
</font></dt></dl>
|
||||
</dd>
|
||||
</dl>
|
||||
<p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
<tr bgcolor="#ffc8d8" >
|
||||
<td colspan="3" valign="bottom" > <br />
|
||||
<font color="#000000" face="helvetica, arial" ><a name="JoinThread" >class <strong >JoinThread</strong></a>(<a href="threading.html#Thread" >threading.Thread</a>)</font></td></tr>
|
||||
|
||||
<tr bgcolor="#ffc8d8" ><td rowspan="2" ><tt > </tt></td>
|
||||
<td colspan="2" ><tt ><a href="#JoinThread" >JoinThread</a> - The workhouse that stops the <a href="#StoppableThread" >StoppableThread</a><br /> </tt></td></tr>
|
||||
<tr ><td > </td>
|
||||
<td width="100%" ><dl ><dt >Method resolution order:</dt>
|
||||
<dd ><a href="func_timeout.StoppableThread.html#JoinThread" >JoinThread</a></dd>
|
||||
<dd ><a href="threading.html#Thread" >threading.Thread</a></dd>
|
||||
<dd ><a href="builtins.html#object" >builtins.object</a></dd>
|
||||
</dl>
|
||||
<hr />
|
||||
Methods defined here:<br />
|
||||
<dl ><dt ><a name="JoinThread-__init__" ><strong >__init__</strong></a>(self, otherThread, exception, repeatEvery=2.0)</dt><dd ><tt >This constructor should always be called with keyword arguments. Arguments are:<br />
|
||||
<br />
|
||||
*group* should be None; reserved for future extension when a ThreadGroup<br />
|
||||
class is implemented.<br />
|
||||
<br />
|
||||
*target* is the callable object to be invoked by the <a href="#JoinThread-run" >run</a>()<br />
|
||||
method. Defaults to None, meaning nothing is called.<br />
|
||||
<br />
|
||||
*name* is the thread name. By default, a unique name is constructed of<br />
|
||||
the form "<a href="threading.html#Thread" >Thread</a>-N" where N is a small decimal number.<br />
|
||||
<br />
|
||||
*args* is the argument tuple for the target invocation. Defaults to ().<br />
|
||||
<br />
|
||||
*kwargs* is a dictionary of keyword arguments for the target<br />
|
||||
invocation. Defaults to {}.<br />
|
||||
<br />
|
||||
If a subclass overrides the constructor, it must make sure to invoke<br />
|
||||
the base class constructor (<a href="threading.html#Thread" >Thread</a>.<a href="#JoinThread-__init__" >__init__</a>()) before doing anything<br />
|
||||
<hr>
|
||||
Methods inherited from <a href="threading.html#Thread">threading.Thread</a>:<br>
|
||||
<dl><dt><a name="StoppableThread-__init__"><strong>__init__</strong></a>(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)</dt><dd><tt>This constructor should always be called with keyword arguments. Arguments are:<br>
|
||||
<br>
|
||||
*group* should be None; reserved for future extension when a ThreadGroup<br>
|
||||
class is implemented.<br>
|
||||
<br>
|
||||
*target* is the callable object to be invoked by the <a href="#func_timeout.StoppableThread-run">run</a>()<br>
|
||||
method. Defaults to None, meaning nothing is called.<br>
|
||||
<br>
|
||||
*name* is the thread name. By default, a unique name is constructed of<br>
|
||||
the form "Thread-N" where N is a small decimal number.<br>
|
||||
<br>
|
||||
*args* is the argument tuple for the target invocation. Defaults to ().<br>
|
||||
<br>
|
||||
*kwargs* is a dictionary of keyword arguments for the target<br>
|
||||
invocation. Defaults to {}.<br>
|
||||
<br>
|
||||
If a subclass overrides the constructor, it must make sure to invoke<br>
|
||||
the base class constructor (Thread.<a href="#func_timeout.StoppableThread-__init__">__init__</a>()) before doing anything<br>
|
||||
else to the thread.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-run" ><strong >run</strong></a>(self)</dt><dd ><tt >Method representing the thread's activity.<br />
|
||||
<br />
|
||||
You may override this method in a subclass. The standard <a href="#JoinThread-run" >run</a>() method<br />
|
||||
invokes the callable object passed to the object's constructor as the<br />
|
||||
target argument, if any, with sequential and keyword arguments taken<br />
|
||||
from the args and kwargs arguments, respectively.</tt></dd></dl>
|
||||
<dl><dt><a name="StoppableThread-__repr__"><strong>__repr__</strong></a>(self)</dt><dd><tt>Return repr(self).</tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Methods inherited from <a href="threading.html#Thread" >threading.Thread</a>:<br />
|
||||
<dl ><dt ><a name="JoinThread-__repr__" ><strong >__repr__</strong></a>(self)</dt><dd ><tt >Return repr(self).</tt></dd></dl>
|
||||
<dl><dt><a name="StoppableThread-getName"><strong>getName</strong></a>(self)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-getName" ><strong >getName</strong></a>(self)</dt></dl>
|
||||
<dl><dt><a name="StoppableThread-isAlive"><strong>isAlive</strong></a>(self)</dt><dd><tt>Return whether the thread is alive.<br>
|
||||
<br>
|
||||
This method is deprecated, use <a href="#func_timeout.StoppableThread-is_alive">is_alive</a>() instead.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-isAlive" ><strong >isAlive</strong></a> = is_alive(self)</dt><dd ><tt >Return whether the thread is alive.<br />
|
||||
<br />
|
||||
This method returns True just before the <a href="#JoinThread-run" >run</a>() method starts until just<br />
|
||||
after the <a href="#JoinThread-run" >run</a>() method terminates. The module function enumerate()<br />
|
||||
<dl><dt><a name="StoppableThread-isDaemon"><strong>isDaemon</strong></a>(self)</dt></dl>
|
||||
|
||||
<dl><dt><a name="StoppableThread-is_alive"><strong>is_alive</strong></a>(self)</dt><dd><tt>Return whether the thread is alive.<br>
|
||||
<br>
|
||||
This method returns True just before the <a href="#func_timeout.StoppableThread-run">run</a>() method starts until just<br>
|
||||
after the <a href="#func_timeout.StoppableThread-run">run</a>() method terminates. The module function enumerate()<br>
|
||||
returns a list of all alive threads.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-isDaemon" ><strong >isDaemon</strong></a>(self)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-is_alive" ><strong >is_alive</strong></a>(self)</dt><dd ><tt >Return whether the thread is alive.<br />
|
||||
<br />
|
||||
This method returns True just before the <a href="#JoinThread-run" >run</a>() method starts until just<br />
|
||||
after the <a href="#JoinThread-run" >run</a>() method terminates. The module function enumerate()<br />
|
||||
returns a list of all alive threads.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-join" ><strong >join</strong></a>(self, timeout=None)</dt><dd ><tt >Wait until the thread terminates.<br />
|
||||
<br />
|
||||
This blocks the calling thread until the thread whose <a href="#JoinThread-join" >join</a>() method is<br />
|
||||
called terminates -- either normally or through an unhandled exception<br />
|
||||
or until the optional timeout occurs.<br />
|
||||
<br />
|
||||
When the timeout argument is present and not None, it should be a<br />
|
||||
floating point number specifying a timeout for the operation in seconds<br />
|
||||
(or fractions thereof). As <a href="#JoinThread-join" >join</a>() always returns None, you must call<br />
|
||||
<a href="#JoinThread-isAlive" >isAlive</a>() after <a href="#JoinThread-join" >join</a>() to decide whether a timeout happened -- if the<br />
|
||||
thread is still alive, the <a href="#JoinThread-join" >join</a>() call timed out.<br />
|
||||
<br />
|
||||
When the timeout argument is not present or None, the operation will<br />
|
||||
block until the thread terminates.<br />
|
||||
<br />
|
||||
A thread can be <a href="#JoinThread-join" >join</a>()ed many times.<br />
|
||||
<br />
|
||||
<a href="#JoinThread-join" >join</a>() raises a RuntimeError if an attempt is made to join the current<br />
|
||||
thread as that would cause a deadlock. It is also an error to <a href="#JoinThread-join" >join</a>() a<br />
|
||||
thread before it has been started and attempts to do so raises the same<br />
|
||||
<dl><dt><a name="StoppableThread-join"><strong>join</strong></a>(self, timeout=None)</dt><dd><tt>Wait until the thread terminates.<br>
|
||||
<br>
|
||||
This blocks the calling thread until the thread whose <a href="#func_timeout.StoppableThread-join">join</a>() method is<br>
|
||||
called terminates -- either normally or through an unhandled exception<br>
|
||||
or until the optional timeout occurs.<br>
|
||||
<br>
|
||||
When the timeout argument is present and not None, it should be a<br>
|
||||
floating point number specifying a timeout for the operation in seconds<br>
|
||||
(or fractions thereof). As <a href="#func_timeout.StoppableThread-join">join</a>() always returns None, you must call<br>
|
||||
<a href="#func_timeout.StoppableThread-is_alive">is_alive</a>() after <a href="#func_timeout.StoppableThread-join">join</a>() to decide whether a timeout happened -- if the<br>
|
||||
thread is still alive, the <a href="#func_timeout.StoppableThread-join">join</a>() call timed out.<br>
|
||||
<br>
|
||||
When the timeout argument is not present or None, the operation will<br>
|
||||
block until the thread terminates.<br>
|
||||
<br>
|
||||
A thread can be <a href="#func_timeout.StoppableThread-join">join</a>()ed many times.<br>
|
||||
<br>
|
||||
<a href="#func_timeout.StoppableThread-join">join</a>() raises a RuntimeError if an attempt is made to join the current<br>
|
||||
thread as that would cause a deadlock. It is also an error to <a href="#func_timeout.StoppableThread-join">join</a>() a<br>
|
||||
thread before it has been started and attempts to do so raises the same<br>
|
||||
exception.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-setDaemon" ><strong >setDaemon</strong></a>(self, daemonic)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-setName" ><strong >setName</strong></a>(self, name)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="JoinThread-start" ><strong >start</strong></a>(self)</dt><dd ><tt >Start the thread's activity.<br />
|
||||
<br />
|
||||
It must be called at most once per thread object. It arranges for the<br />
|
||||
object's <a href="#JoinThread-run" >run</a>() method to be invoked in a separate thread of control.<br />
|
||||
<br />
|
||||
This method will raise a RuntimeError if called more than once on the<br />
|
||||
same thread object.</tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Data descriptors inherited from <a href="threading.html#Thread" >threading.Thread</a>:<br />
|
||||
<dl ><dt ><strong >__dict__</strong></dt>
|
||||
<dd ><tt >dictionary for instance variables (if defined)</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >__weakref__</strong></dt>
|
||||
<dd ><tt >list of weak references to the object (if defined)</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >daemon</strong></dt>
|
||||
<dd ><tt >A boolean value indicating whether this thread is a daemon thread.<br />
|
||||
<br />
|
||||
This must be set before start() is called, otherwise RuntimeError is<br />
|
||||
raised. Its initial value is inherited from the creating thread; the<br />
|
||||
main thread is not a daemon thread and therefore all threads created in<br />
|
||||
the main thread default to daemon = False.<br />
|
||||
<br />
|
||||
The entire Python program exits when no alive non-daemon threads are<br />
|
||||
left.</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >ident</strong></dt>
|
||||
<dd ><tt >Thread identifier of this thread or None if it has not been started.<br />
|
||||
<br />
|
||||
This is a nonzero integer. See the thread.get_ident() function. Thread<br />
|
||||
identifiers may be recycled when a thread exits and another thread is<br />
|
||||
created. The identifier is available even after the thread has exited.</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >name</strong></dt>
|
||||
<dd ><tt >A string used for identification purposes only.<br />
|
||||
<br />
|
||||
It has no semantics. Multiple threads may be given the same name. The<br />
|
||||
initial name is set by the constructor.</tt></dd>
|
||||
</dl>
|
||||
</td></tr></table> <p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
<tr bgcolor="#ffc8d8" >
|
||||
<td colspan="3" valign="bottom" > <br />
|
||||
<font color="#000000" face="helvetica, arial" ><a name="StoppableThread" >class <strong >StoppableThread</strong></a>(<a href="threading.html#Thread" >threading.Thread</a>)</font></td></tr>
|
||||
|
||||
<tr bgcolor="#ffc8d8" ><td rowspan="2" ><tt > </tt></td>
|
||||
<td colspan="2" ><tt ><a href="#StoppableThread" >StoppableThread</a> - A thread that can be stopped by forcing an exception in the execution context.<br /> </tt></td></tr>
|
||||
<tr ><td > </td>
|
||||
<td width="100%" ><dl ><dt >Method resolution order:</dt>
|
||||
<dd ><a href="func_timeout.StoppableThread.html#StoppableThread" >StoppableThread</a></dd>
|
||||
<dd ><a href="threading.html#Thread" >threading.Thread</a></dd>
|
||||
<dd ><a href="builtins.html#object" >builtins.object</a></dd>
|
||||
</dl>
|
||||
<hr />
|
||||
Methods inherited from <a href="threading.html#Thread" >threading.Thread</a>:<br />
|
||||
<dl ><dt ><a name="StoppableThread-__init__" ><strong >__init__</strong></a>(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)</dt><dd ><tt >This constructor should always be called with keyword arguments. Arguments are:<br />
|
||||
<br />
|
||||
*group* should be None; reserved for future extension when a ThreadGroup<br />
|
||||
class is implemented.<br />
|
||||
<br />
|
||||
*target* is the callable object to be invoked by the <a href="#StoppableThread-run" >run</a>()<br />
|
||||
method. Defaults to None, meaning nothing is called.<br />
|
||||
<br />
|
||||
*name* is the thread name. By default, a unique name is constructed of<br />
|
||||
the form "<a href="threading.html#Thread" >Thread</a>-N" where N is a small decimal number.<br />
|
||||
<br />
|
||||
*args* is the argument tuple for the target invocation. Defaults to ().<br />
|
||||
<br />
|
||||
*kwargs* is a dictionary of keyword arguments for the target<br />
|
||||
invocation. Defaults to {}.<br />
|
||||
<br />
|
||||
If a subclass overrides the constructor, it must make sure to invoke<br />
|
||||
the base class constructor (<a href="threading.html#Thread" >Thread</a>.<a href="#StoppableThread-__init__" >__init__</a>()) before doing anything<br />
|
||||
else to the thread.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-__repr__" ><strong >__repr__</strong></a>(self)</dt><dd ><tt >Return repr(self).</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-getName" ><strong >getName</strong></a>(self)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-isAlive" ><strong >isAlive</strong></a> = is_alive(self)</dt><dd ><tt >Return whether the thread is alive.<br />
|
||||
<br />
|
||||
This method returns True just before the <a href="#StoppableThread-run" >run</a>() method starts until just<br />
|
||||
after the <a href="#StoppableThread-run" >run</a>() method terminates. The module function enumerate()<br />
|
||||
returns a list of all alive threads.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-isDaemon" ><strong >isDaemon</strong></a>(self)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-is_alive" ><strong >is_alive</strong></a>(self)</dt><dd ><tt >Return whether the thread is alive.<br />
|
||||
<br />
|
||||
This method returns True just before the <a href="#StoppableThread-run" >run</a>() method starts until just<br />
|
||||
after the <a href="#StoppableThread-run" >run</a>() method terminates. The module function enumerate()<br />
|
||||
returns a list of all alive threads.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-join" ><strong >join</strong></a>(self, timeout=None)</dt><dd ><tt >Wait until the thread terminates.<br />
|
||||
<br />
|
||||
This blocks the calling thread until the thread whose <a href="#StoppableThread-join" >join</a>() method is<br />
|
||||
called terminates -- either normally or through an unhandled exception<br />
|
||||
or until the optional timeout occurs.<br />
|
||||
<br />
|
||||
When the timeout argument is present and not None, it should be a<br />
|
||||
floating point number specifying a timeout for the operation in seconds<br />
|
||||
(or fractions thereof). As <a href="#StoppableThread-join" >join</a>() always returns None, you must call<br />
|
||||
<a href="#StoppableThread-isAlive" >isAlive</a>() after <a href="#StoppableThread-join" >join</a>() to decide whether a timeout happened -- if the<br />
|
||||
thread is still alive, the <a href="#StoppableThread-join" >join</a>() call timed out.<br />
|
||||
<br />
|
||||
When the timeout argument is not present or None, the operation will<br />
|
||||
block until the thread terminates.<br />
|
||||
<br />
|
||||
A thread can be <a href="#StoppableThread-join" >join</a>()ed many times.<br />
|
||||
<br />
|
||||
<a href="#StoppableThread-join" >join</a>() raises a RuntimeError if an attempt is made to join the current<br />
|
||||
thread as that would cause a deadlock. It is also an error to <a href="#StoppableThread-join" >join</a>() a<br />
|
||||
thread before it has been started and attempts to do so raises the same<br />
|
||||
exception.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-run" ><strong >run</strong></a>(self)</dt><dd ><tt >Method representing the thread's activity.<br />
|
||||
<br />
|
||||
You may override this method in a subclass. The standard <a href="#StoppableThread-run" >run</a>() method<br />
|
||||
invokes the callable object passed to the object's constructor as the<br />
|
||||
target argument, if any, with sequential and keyword arguments taken<br />
|
||||
<dl><dt><a name="StoppableThread-run"><strong>run</strong></a>(self)</dt><dd><tt>Method representing the thread's activity.<br>
|
||||
<br>
|
||||
You may override this method in a subclass. The standard <a href="#func_timeout.StoppableThread-run">run</a>() method<br>
|
||||
invokes the callable object passed to the object's constructor as the<br>
|
||||
target argument, if any, with sequential and keyword arguments taken<br>
|
||||
from the args and kwargs arguments, respectively.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-setDaemon" ><strong >setDaemon</strong></a>(self, daemonic)</dt></dl>
|
||||
<dl><dt><a name="StoppableThread-setDaemon"><strong>setDaemon</strong></a>(self, daemonic)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-setName" ><strong >setName</strong></a>(self, name)</dt></dl>
|
||||
<dl><dt><a name="StoppableThread-setName"><strong>setName</strong></a>(self, name)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-start" ><strong >start</strong></a>(self)</dt><dd ><tt >Start the thread's activity.<br />
|
||||
<br />
|
||||
It must be called at most once per thread object. It arranges for the<br />
|
||||
object's <a href="#StoppableThread-run" >run</a>() method to be invoked in a separate thread of control.<br />
|
||||
<br />
|
||||
This method will raise a RuntimeError if called more than once on the<br />
|
||||
<dl><dt><a name="StoppableThread-start"><strong>start</strong></a>(self)</dt><dd><tt>Start the thread's activity.<br>
|
||||
<br>
|
||||
It must be called at most once per thread object. It arranges for the<br>
|
||||
object's <a href="#func_timeout.StoppableThread-run">run</a>() method to be invoked in a separate thread of control.<br>
|
||||
<br>
|
||||
This method will raise a RuntimeError if called more than once on the<br>
|
||||
same thread object.</tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Data descriptors inherited from <a href="threading.html#Thread" >threading.Thread</a>:<br />
|
||||
<dl ><dt ><strong >__dict__</strong></dt>
|
||||
<dd ><tt >dictionary for instance variables (if defined)</tt></dd>
|
||||
<hr>
|
||||
Data descriptors inherited from <a href="threading.html#Thread">threading.Thread</a>:<br>
|
||||
<dl><dt><strong>__dict__</strong></dt>
|
||||
<dd><tt>dictionary for instance variables (if defined)</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >__weakref__</strong></dt>
|
||||
<dd ><tt >list of weak references to the object (if defined)</tt></dd>
|
||||
<dl><dt><strong>__weakref__</strong></dt>
|
||||
<dd><tt>list of weak references to the object (if defined)</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >daemon</strong></dt>
|
||||
<dd ><tt >A boolean value indicating whether this thread is a daemon thread.<br />
|
||||
<br />
|
||||
This must be set before start() is called, otherwise RuntimeError is<br />
|
||||
raised. Its initial value is inherited from the creating thread; the<br />
|
||||
main thread is not a daemon thread and therefore all threads created in<br />
|
||||
the main thread default to daemon = False.<br />
|
||||
<br />
|
||||
The entire Python program exits when no alive non-daemon threads are<br />
|
||||
<dl><dt><strong>daemon</strong></dt>
|
||||
<dd><tt>A boolean value indicating whether this thread is a daemon thread.<br>
|
||||
<br>
|
||||
This must be set before start() is called, otherwise RuntimeError is<br>
|
||||
raised. Its initial value is inherited from the creating thread; the<br>
|
||||
main thread is not a daemon thread and therefore all threads created in<br>
|
||||
the main thread default to daemon = False.<br>
|
||||
<br>
|
||||
The entire Python program exits when no alive non-daemon threads are<br>
|
||||
left.</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >ident</strong></dt>
|
||||
<dd ><tt >Thread identifier of this thread or None if it has not been started.<br />
|
||||
<br />
|
||||
This is a nonzero integer. See the thread.get_ident() function. Thread<br />
|
||||
identifiers may be recycled when a thread exits and another thread is<br />
|
||||
<dl><dt><strong>ident</strong></dt>
|
||||
<dd><tt>Thread identifier of this thread or None if it has not been started.<br>
|
||||
<br>
|
||||
This is a nonzero integer. See the get_ident() function. Thread<br>
|
||||
identifiers may be recycled when a thread exits and another thread is<br>
|
||||
created. The identifier is available even after the thread has exited.</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >name</strong></dt>
|
||||
<dd ><tt >A string used for identification purposes only.<br />
|
||||
<br />
|
||||
It has no semantics. Multiple threads may be given the same name. The<br />
|
||||
<dl><dt><strong>name</strong></dt>
|
||||
<dd><tt>A string used for identification purposes only.<br>
|
||||
<br>
|
||||
It has no semantics. Multiple threads may be given the same name. The<br>
|
||||
initial name is set by the constructor.</tt></dd>
|
||||
</dl>
|
||||
</td></tr></table></p></p></td></tr></table><p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
<tr bgcolor="#55aa55" >
|
||||
<td colspan="3" valign="bottom" > <br />
|
||||
<font color="#ffffff" face="helvetica, arial" ><big ><strong >Data</strong></big></font></td></tr>
|
||||
|
||||
<tr ><td bgcolor="#55aa55" ><tt > </tt></td><td > </td>
|
||||
<td width="100%" ><strong >__all__</strong> = ('StoppableThread', 'JoinThread')</td></tr></table>
|
||||
</p></p></p></body></html>
|
||||
</td></tr></table>
|
||||
</body></html>
|
||||
@ -38,34 +38,34 @@ LICENSE, otherwise it is available at https://gith
|
||||
If #timeout is provided as a lambda/function, it will be called<br />
|
||||
prior to each invocation of the decorated function to calculate the timeout to be used<br />
|
||||
for that call, based on the arguments passed to the decorated function.<br />
|
||||
<br />
|
||||
<br />
|
||||
For example, you may have a "processData" function whose execution time<br />
|
||||
depends on the number of "data" elements, so you may want a million elements to have a <br />
|
||||
depends on the number of "data" elements, so you may want a million elements to have a<br />
|
||||
much higher timeout than seven elements.)<br />
|
||||
<br />
|
||||
If #allowOverride is True AND a kwarg of "forceTimeout" is passed to the wrapped function, that timeout<br />
|
||||
will be used for that single call.<br />
|
||||
<br />
|
||||
@param timeout <float OR lambda/function> - <br />
|
||||
@param timeout <float OR lambda/function> -<br />
|
||||
<br />
|
||||
**If float:**<br />
|
||||
Default number of seconds max to allow function to execute<br />
|
||||
before throwing FunctionTimedOut<br />
|
||||
<br />
|
||||
<br />
|
||||
**If lambda/function:<br />
|
||||
<br />
|
||||
If a function/lambda is provided, it will be called for every<br />
|
||||
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed) <br />
|
||||
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed)<br />
|
||||
to determine the timeout to use based on the arguments to the decorated function.<br />
|
||||
<br />
|
||||
The arguments as passed into the decorated function will be passed to this function.<br />
|
||||
They either must match exactly to what the decorated function has, OR<br />
|
||||
if you prefer to get the *args (list of ordered args) and **kwargs ( key : value keyword args form),<br />
|
||||
define your calculate function like:<br />
|
||||
<br />
|
||||
<br />
|
||||
def calculateTimeout(*args, **kwargs):<br />
|
||||
...<br />
|
||||
<br />
|
||||
<br />
|
||||
or lambda like:<br />
|
||||
<br />
|
||||
calculateTimeout = lambda *args, **kwargs : ...<br />
|
||||
|
||||
@ -33,7 +33,9 @@ LICENSE, otherwise it is available at https://gith
|
||||
<font color="#000000" face="helvetica, arial" ><a name="FunctionTimedOut" >class <strong >FunctionTimedOut</strong></a>(<a href="builtins.html#BaseException" >builtins.BaseException</a>)</font></td></tr>
|
||||
|
||||
<tr bgcolor="#ffc8d8" ><td rowspan="2" ><tt > </tt></td>
|
||||
<td colspan="2" ><tt ><a href="#FunctionTimedOut" >FunctionTimedOut</a> - Exception raised when a function times out<br />
|
||||
<td colspan="2" ><tt ><a href="#FunctionTimedOut" >FunctionTimedOut</a>(msg='', timedOutAfter=None, timedOutFunction=None, timedOutArgs=None, timedOutKwargs=None)<br />
|
||||
<br />
|
||||
<a href="#FunctionTimedOut" >FunctionTimedOut</a> - Exception raised when a function times out<br />
|
||||
<br />
|
||||
@property timedOutAfter - Number of seconds before timeout was triggered<br />
|
||||
<br />
|
||||
@ -51,7 +53,19 @@ LICENSE, otherwise it is available at https://gith
|
||||
</dl>
|
||||
<hr />
|
||||
Methods defined here:<br />
|
||||
<dl ><dt ><a name="FunctionTimedOut-__init__" ><strong >__init__</strong></a>(self, msg='', timedOutAfter=None, timedOutFunction=None, timedOutArgs=None, timedOutKwargs=None)</dt><dd ><tt >Initialize self. See help(type(self)) for accurate signature.</tt></dd></dl>
|
||||
<dl ><dt ><a name="FunctionTimedOut-__init__" ><strong >__init__</strong></a>(self, msg='', timedOutAfter=None, timedOutFunction=None, timedOutArgs=None, timedOutKwargs=None)</dt><dd ><tt >__init__ - Create this exception type.<br />
|
||||
<br />
|
||||
You should not need to do this outside of testing, it will be created by the func_timeout API<br />
|
||||
<br />
|
||||
@param msg <str> - A predefined message, otherwise we will attempt to generate one from the other arguments.<br />
|
||||
<br />
|
||||
@param timedOutAfter <None/float> - Number of seconds before timing-out. Filled-in by API, None will produce "Unknown"<br />
|
||||
<br />
|
||||
@param timedOutFunction <None/function> - Reference to the function that timed-out. Filled-in by API." None will produce "Unknown Function"<br />
|
||||
<br />
|
||||
@param timedOutArgs <None/tuple/list> - List of fixed-order arguments ( *args ), or None for no args.<br />
|
||||
<br />
|
||||
@param timedOutKwargs <None/dict> - Dict of keyword arg ( **kwargs ) names to values, or None for no kwargs.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-getMsg" ><strong >getMsg</strong></a>(self)</dt><dd ><tt >getMsg - Generate a default message based on parameters to <a href="#FunctionTimedOut" >FunctionTimedOut</a> exception'<br />
|
||||
<br />
|
||||
@ -60,7 +74,7 @@ Methods defined here:<br />
|
||||
<dl ><dt ><a name="FunctionTimedOut-retry" ><strong >retry</strong></a>(self, timeout='RETRY_SAME_TIMEOUT')</dt><dd ><tt >retry - Retry the timed-out function with same arguments.<br />
|
||||
<br />
|
||||
@param timeout <float/RETRY_SAME_TIMEOUT/None> Default RETRY_SAME_TIMEOUT<br />
|
||||
<br />
|
||||
<br />
|
||||
If RETRY_SAME_TIMEOUT : Will retry the function same args, same timeout<br />
|
||||
If a float/int : Will retry the function same args with provided timeout<br />
|
||||
If None : Will retry function same args no timeout<br />
|
||||
@ -78,9 +92,7 @@ Methods inherited from <a href="builtins.html#BaseException" >builtins.BaseExcep
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-__getattribute__" ><strong >__getattribute__</strong></a>(self, name, /)</dt><dd ><tt >Return getattr(self, name).</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-__new__" ><strong >__new__</strong></a>(*args, **kwargs)<font color="#909090" ><font face="helvetica, arial" > from <a href="builtins.html#type" >builtins.type</a></font></font></dt><dd ><tt >Create and return a new object. See help(type) for accurate signature.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-__reduce__" ><strong >__reduce__</strong></a>(...)</dt><dd ><tt >helper for pickle</tt></dd></dl>
|
||||
<dl ><dt ><a name="FunctionTimedOut-__reduce__" ><strong >__reduce__</strong></a>(...)</dt><dd ><tt >Helper for pickle.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-__repr__" ><strong >__repr__</strong></a>(self, /)</dt><dd ><tt >Return repr(self).</tt></dd></dl>
|
||||
|
||||
@ -93,6 +105,10 @@ Methods inherited from <a href="builtins.html#BaseException" >builtins.BaseExcep
|
||||
<dl ><dt ><a name="FunctionTimedOut-with_traceback" ><strong >with_traceback</strong></a>(...)</dt><dd ><tt >Exception.<a href="#FunctionTimedOut-with_traceback" >with_traceback</a>(tb) --<br />
|
||||
set self.<strong >__traceback__</strong> to tb and return self.</tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Static methods inherited from <a href="builtins.html#BaseException" >builtins.BaseException</a>:<br />
|
||||
<dl ><dt ><a name="FunctionTimedOut-__new__" ><strong >__new__</strong></a>(*args, **kwargs)<font color="#909090" ><font face="helvetica, arial" > from <a href="builtins.html#type" >builtins.type</a></font></font></dt><dd ><tt >Create and return a new object. See help(type) for accurate signature.</tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Data descriptors inherited from <a href="builtins.html#BaseException" >builtins.BaseException</a>:<br />
|
||||
<dl ><dt ><strong >__cause__</strong></dt>
|
||||
|
||||
@ -6,8 +6,8 @@
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="heading" >
|
||||
<tr bgcolor="#7799ee" >
|
||||
<td valign="bottom" > <br />
|
||||
<font color="#ffffff" face="helvetica, arial" > <br /><big ><big ><strong >func_timeout</strong></big></big> (version 4.1.0)</font></td><td align="right" valign="bottom" ><font color="#ffffff" face="helvetica, arial" ><a href="func_timeout.html" >index</a></font></td></tr></table>
|
||||
<p ><tt >Copyright (c) 2016, 2017 Tim Savannah All Rights Reserved.<br />
|
||||
<font color="#ffffff" face="helvetica, arial" > <br /><big ><big ><strong >func_timeout</strong></big></big> (version 4.3.5)</font></td><td align="right" valign="bottom" ><font color="#ffffff" face="helvetica, arial" ><a href="func_timeout.html" >index</a></font></td></tr></table>
|
||||
<p ><tt >Copyright (c) 2016, 2017, 2019 Tim Savannah All Rights Reserved.<br />
|
||||
<br />
|
||||
Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as<br />
|
||||
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE</tt></p>
|
||||
@ -37,6 +37,12 @@ LICENSE, otherwise it is available at https://gith
|
||||
<dt ><font face="helvetica, arial" ><a href="func_timeout.exceptions.html#FunctionTimedOut" >func_timeout.exceptions.FunctionTimedOut</a>
|
||||
</font></dt></dl>
|
||||
</dd>
|
||||
<dt ><font face="helvetica, arial" ><a href="threading.html#Thread" >threading.Thread</a>(<a href="builtins.html#object" >builtins.object</a>)
|
||||
</font></dt><dd >
|
||||
<dl >
|
||||
<dt ><font face="helvetica, arial" ><a href="func_timeout.StoppableThread.html#StoppableThread" >func_timeout.StoppableThread.StoppableThread</a>
|
||||
</font></dt></dl>
|
||||
</dd>
|
||||
</dl>
|
||||
<p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
@ -45,7 +51,9 @@ LICENSE, otherwise it is available at https://gith
|
||||
<font color="#000000" face="helvetica, arial" ><a name="FunctionTimedOut" >class <strong >FunctionTimedOut</strong></a>(<a href="builtins.html#BaseException" >builtins.BaseException</a>)</font></td></tr>
|
||||
|
||||
<tr bgcolor="#ffc8d8" ><td rowspan="2" ><tt > </tt></td>
|
||||
<td colspan="2" ><tt ><a href="#FunctionTimedOut" >FunctionTimedOut</a> - Exception raised when a function times out<br />
|
||||
<td colspan="2" ><tt ><a href="#FunctionTimedOut" >FunctionTimedOut</a>(msg='', timedOutAfter=None, timedOutFunction=None, timedOutArgs=None, timedOutKwargs=None)<br />
|
||||
<br />
|
||||
<a href="#FunctionTimedOut" >FunctionTimedOut</a> - Exception raised when a function times out<br />
|
||||
<br />
|
||||
@property timedOutAfter - Number of seconds before timeout was triggered<br />
|
||||
<br />
|
||||
@ -63,7 +71,19 @@ LICENSE, otherwise it is available at https://gith
|
||||
</dl>
|
||||
<hr />
|
||||
Methods defined here:<br />
|
||||
<dl ><dt ><a name="FunctionTimedOut-__init__" ><strong >__init__</strong></a>(self, msg='', timedOutAfter=None, timedOutFunction=None, timedOutArgs=None, timedOutKwargs=None)</dt><dd ><tt >Initialize self. See help(type(self)) for accurate signature.</tt></dd></dl>
|
||||
<dl ><dt ><a name="FunctionTimedOut-__init__" ><strong >__init__</strong></a>(self, msg='', timedOutAfter=None, timedOutFunction=None, timedOutArgs=None, timedOutKwargs=None)</dt><dd ><tt >__init__ - Create this exception type.<br />
|
||||
<br />
|
||||
You should not need to do this outside of testing, it will be created by the func_timeout API<br />
|
||||
<br />
|
||||
@param msg <str> - A predefined message, otherwise we will attempt to generate one from the other arguments.<br />
|
||||
<br />
|
||||
@param timedOutAfter <None/float> - Number of seconds before timing-out. Filled-in by API, None will produce "Unknown"<br />
|
||||
<br />
|
||||
@param timedOutFunction <None/function> - Reference to the function that timed-out. Filled-in by API." None will produce "Unknown Function"<br />
|
||||
<br />
|
||||
@param timedOutArgs <None/tuple/list> - List of fixed-order arguments ( *args ), or None for no args.<br />
|
||||
<br />
|
||||
@param timedOutKwargs <None/dict> - Dict of keyword arg ( **kwargs ) names to values, or None for no kwargs.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-getMsg" ><strong >getMsg</strong></a>(self)</dt><dd ><tt >getMsg - Generate a default message based on parameters to <a href="#FunctionTimedOut" >FunctionTimedOut</a> exception'<br />
|
||||
<br />
|
||||
@ -72,7 +92,7 @@ Methods defined here:<br />
|
||||
<dl ><dt ><a name="FunctionTimedOut-retry" ><strong >retry</strong></a>(self, timeout='RETRY_SAME_TIMEOUT')</dt><dd ><tt >retry - Retry the timed-out function with same arguments.<br />
|
||||
<br />
|
||||
@param timeout <float/RETRY_SAME_TIMEOUT/None> Default RETRY_SAME_TIMEOUT<br />
|
||||
<br />
|
||||
<br />
|
||||
If RETRY_SAME_TIMEOUT : Will retry the function same args, same timeout<br />
|
||||
If a float/int : Will retry the function same args with provided timeout<br />
|
||||
If None : Will retry function same args no timeout<br />
|
||||
@ -90,9 +110,7 @@ Methods inherited from <a href="builtins.html#BaseException" >builtins.BaseExcep
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-__getattribute__" ><strong >__getattribute__</strong></a>(self, name, /)</dt><dd ><tt >Return getattr(self, name).</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-__new__" ><strong >__new__</strong></a>(*args, **kwargs)<font color="#909090" ><font face="helvetica, arial" > from <a href="builtins.html#type" >builtins.type</a></font></font></dt><dd ><tt >Create and return a new object. See help(type) for accurate signature.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-__reduce__" ><strong >__reduce__</strong></a>(...)</dt><dd ><tt >helper for pickle</tt></dd></dl>
|
||||
<dl ><dt ><a name="FunctionTimedOut-__reduce__" ><strong >__reduce__</strong></a>(...)</dt><dd ><tt >Helper for pickle.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="FunctionTimedOut-__repr__" ><strong >__repr__</strong></a>(self, /)</dt><dd ><tt >Return repr(self).</tt></dd></dl>
|
||||
|
||||
@ -105,6 +123,10 @@ Methods inherited from <a href="builtins.html#BaseException" >builtins.BaseExcep
|
||||
<dl ><dt ><a name="FunctionTimedOut-with_traceback" ><strong >with_traceback</strong></a>(...)</dt><dd ><tt >Exception.<a href="#FunctionTimedOut-with_traceback" >with_traceback</a>(tb) --<br />
|
||||
set self.<strong >__traceback__</strong> to tb and return self.</tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Static methods inherited from <a href="builtins.html#BaseException" >builtins.BaseException</a>:<br />
|
||||
<dl ><dt ><a name="FunctionTimedOut-__new__" ><strong >__new__</strong></a>(*args, **kwargs)<font color="#909090" ><font face="helvetica, arial" > from <a href="builtins.html#type" >builtins.type</a></font></font></dt><dd ><tt >Create and return a new object. See help(type) for accurate signature.</tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Data descriptors inherited from <a href="builtins.html#BaseException" >builtins.BaseException</a>:<br />
|
||||
<dl ><dt ><strong >__cause__</strong></dt>
|
||||
@ -121,7 +143,175 @@ Data descriptors inherited from <a href="builtins.html#BaseException" >builtins.
|
||||
</dl>
|
||||
<dl ><dt ><strong >args</strong></dt>
|
||||
</dl>
|
||||
</td></tr></table></p></td></tr></table><p >
|
||||
</td></tr></table> <p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
<tr bgcolor="#ffc8d8" >
|
||||
<td colspan="3" valign="bottom" > <br />
|
||||
<font color="#000000" face="helvetica, arial" ><a name="StoppableThread" >class <strong >StoppableThread</strong></a>(<a href="threading.html#Thread" >threading.Thread</a>)</font></td></tr>
|
||||
|
||||
<tr bgcolor="#ffc8d8" ><td rowspan="2" ><tt > </tt></td>
|
||||
<td colspan="2" ><tt ><a href="#StoppableThread" >StoppableThread</a>(group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)<br />
|
||||
<br />
|
||||
<a href="#StoppableThread" >StoppableThread</a> - A thread that can be stopped by forcing an exception in the execution context.<br />
|
||||
<br />
|
||||
This works both to interrupt code that is in C or in python code, at either the next call to a python function,<br />
|
||||
or the next line in python code.<br />
|
||||
<br />
|
||||
It is recommended that if you call stop ( @see <a href="#StoppableThread" >StoppableThread</a>.stop ) that you use an exception that inherits <a href="builtins.html#BaseException" >BaseException</a>, to ensure it likely isn't caught.<br />
|
||||
<br />
|
||||
Also, beware unmarked exception handlers in your code. Code like this:<br />
|
||||
<br />
|
||||
while True:<br />
|
||||
try:<br />
|
||||
doSomething()<br />
|
||||
except:<br />
|
||||
continue<br />
|
||||
<br />
|
||||
will never be able to abort, because the exception you raise is immediately caught.<br />
|
||||
<br />
|
||||
The exception is raised over and over, with a specifed delay (default 2.0 seconds)<br /> </tt></td></tr>
|
||||
<tr ><td > </td>
|
||||
<td width="100%" ><dl ><dt >Method resolution order:</dt>
|
||||
<dd ><a href="func_timeout.StoppableThread.html#StoppableThread" >StoppableThread</a></dd>
|
||||
<dd ><a href="threading.html#Thread" >threading.Thread</a></dd>
|
||||
<dd ><a href="builtins.html#object" >builtins.object</a></dd>
|
||||
</dl>
|
||||
<hr />
|
||||
Methods defined here:<br />
|
||||
<dl ><dt ><a name="StoppableThread-stop" ><strong >stop</strong></a>(self, exception, raiseEvery=2.0)</dt><dd ><tt >Stops the thread by raising a given exception.<br />
|
||||
<br />
|
||||
@param exception <Exception type> - Exception to throw. Likely, you want to use something<br />
|
||||
<br />
|
||||
that inherits from <a href="builtins.html#BaseException" >BaseException</a> (so except Exception as e: continue; isn't a problem)<br />
|
||||
<br />
|
||||
This should be a class/type, NOT an instance, i.e. MyExceptionType not MyExceptionType()<br />
|
||||
<br />
|
||||
<br />
|
||||
@param raiseEvery <float> Default 2.0 - We will keep raising this exception every #raiseEvery seconds,<br />
|
||||
<br />
|
||||
until the thread terminates.<br />
|
||||
<br />
|
||||
If your code traps a specific exception type, this will allow you #raiseEvery seconds to cleanup before exit.<br />
|
||||
<br />
|
||||
If you're calling third-party code you can't control, which catches <a href="builtins.html#BaseException" >BaseException</a>, set this to a low number<br />
|
||||
<br />
|
||||
to break out of their exception handler.<br />
|
||||
<br />
|
||||
<br />
|
||||
@return <None></tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Methods inherited from <a href="threading.html#Thread" >threading.Thread</a>:<br />
|
||||
<dl ><dt ><a name="StoppableThread-__init__" ><strong >__init__</strong></a>(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)</dt><dd ><tt >This constructor should always be called with keyword arguments. Arguments are:<br />
|
||||
<br />
|
||||
*group* should be None; reserved for future extension when a ThreadGroup<br />
|
||||
class is implemented.<br />
|
||||
<br />
|
||||
*target* is the callable object to be invoked by the <a href="#StoppableThread-run" >run</a>()<br />
|
||||
method. Defaults to None, meaning nothing is called.<br />
|
||||
<br />
|
||||
*name* is the thread name. By default, a unique name is constructed of<br />
|
||||
the form "<a href="threading.html#Thread" >Thread</a>-N" where N is a small decimal number.<br />
|
||||
<br />
|
||||
*args* is the argument tuple for the target invocation. Defaults to ().<br />
|
||||
<br />
|
||||
*kwargs* is a dictionary of keyword arguments for the target<br />
|
||||
invocation. Defaults to {}.<br />
|
||||
<br />
|
||||
If a subclass overrides the constructor, it must make sure to invoke<br />
|
||||
the base class constructor (<a href="threading.html#Thread" >Thread</a>.<a href="#StoppableThread-__init__" >__init__</a>()) before doing anything<br />
|
||||
else to the thread.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-__repr__" ><strong >__repr__</strong></a>(self)</dt><dd ><tt >Return repr(self).</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-getName" ><strong >getName</strong></a>(self)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-isAlive" ><strong >isAlive</strong></a>(self)</dt><dd ><tt >Return whether the thread is alive.<br />
|
||||
<br />
|
||||
This method is deprecated, use <a href="#StoppableThread-is_alive" >is_alive</a>() instead.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-isDaemon" ><strong >isDaemon</strong></a>(self)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-is_alive" ><strong >is_alive</strong></a>(self)</dt><dd ><tt >Return whether the thread is alive.<br />
|
||||
<br />
|
||||
This method returns True just before the <a href="#StoppableThread-run" >run</a>() method starts until just<br />
|
||||
after the <a href="#StoppableThread-run" >run</a>() method terminates. The module function enumerate()<br />
|
||||
returns a list of all alive threads.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-join" ><strong >join</strong></a>(self, timeout=None)</dt><dd ><tt >Wait until the thread terminates.<br />
|
||||
<br />
|
||||
This blocks the calling thread until the thread whose <a href="#StoppableThread-join" >join</a>() method is<br />
|
||||
called terminates -- either normally or through an unhandled exception<br />
|
||||
or until the optional timeout occurs.<br />
|
||||
<br />
|
||||
When the timeout argument is present and not None, it should be a<br />
|
||||
floating point number specifying a timeout for the operation in seconds<br />
|
||||
(or fractions thereof). As <a href="#StoppableThread-join" >join</a>() always returns None, you must call<br />
|
||||
<a href="#StoppableThread-is_alive" >is_alive</a>() after <a href="#StoppableThread-join" >join</a>() to decide whether a timeout happened -- if the<br />
|
||||
thread is still alive, the <a href="#StoppableThread-join" >join</a>() call timed out.<br />
|
||||
<br />
|
||||
When the timeout argument is not present or None, the operation will<br />
|
||||
block until the thread terminates.<br />
|
||||
<br />
|
||||
A thread can be <a href="#StoppableThread-join" >join</a>()ed many times.<br />
|
||||
<br />
|
||||
<a href="#StoppableThread-join" >join</a>() raises a RuntimeError if an attempt is made to join the current<br />
|
||||
thread as that would cause a deadlock. It is also an error to <a href="#StoppableThread-join" >join</a>() a<br />
|
||||
thread before it has been started and attempts to do so raises the same<br />
|
||||
exception.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-run" ><strong >run</strong></a>(self)</dt><dd ><tt >Method representing the thread's activity.<br />
|
||||
<br />
|
||||
You may override this method in a subclass. The standard <a href="#StoppableThread-run" >run</a>() method<br />
|
||||
invokes the callable object passed to the object's constructor as the<br />
|
||||
target argument, if any, with sequential and keyword arguments taken<br />
|
||||
from the args and kwargs arguments, respectively.</tt></dd></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-setDaemon" ><strong >setDaemon</strong></a>(self, daemonic)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-setName" ><strong >setName</strong></a>(self, name)</dt></dl>
|
||||
|
||||
<dl ><dt ><a name="StoppableThread-start" ><strong >start</strong></a>(self)</dt><dd ><tt >Start the thread's activity.<br />
|
||||
<br />
|
||||
It must be called at most once per thread object. It arranges for the<br />
|
||||
object's <a href="#StoppableThread-run" >run</a>() method to be invoked in a separate thread of control.<br />
|
||||
<br />
|
||||
This method will raise a RuntimeError if called more than once on the<br />
|
||||
same thread object.</tt></dd></dl>
|
||||
|
||||
<hr />
|
||||
Data descriptors inherited from <a href="threading.html#Thread" >threading.Thread</a>:<br />
|
||||
<dl ><dt ><strong >__dict__</strong></dt>
|
||||
<dd ><tt >dictionary for instance variables (if defined)</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >__weakref__</strong></dt>
|
||||
<dd ><tt >list of weak references to the object (if defined)</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >daemon</strong></dt>
|
||||
<dd ><tt >A boolean value indicating whether this thread is a daemon thread.<br />
|
||||
<br />
|
||||
This must be set before start() is called, otherwise RuntimeError is<br />
|
||||
raised. Its initial value is inherited from the creating thread; the<br />
|
||||
main thread is not a daemon thread and therefore all threads created in<br />
|
||||
the main thread default to daemon = False.<br />
|
||||
<br />
|
||||
The entire Python program exits when no alive non-daemon threads are<br />
|
||||
left.</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >ident</strong></dt>
|
||||
<dd ><tt >Thread identifier of this thread or None if it has not been started.<br />
|
||||
<br />
|
||||
This is a nonzero integer. See the get_ident() function. Thread<br />
|
||||
identifiers may be recycled when a thread exits and another thread is<br />
|
||||
created. The identifier is available even after the thread has exited.</tt></dd>
|
||||
</dl>
|
||||
<dl ><dt ><strong >name</strong></dt>
|
||||
<dd ><tt >A string used for identification purposes only.<br />
|
||||
<br />
|
||||
It has no semantics. Multiple threads may be given the same name. The<br />
|
||||
initial name is set by the constructor.</tt></dd>
|
||||
</dl>
|
||||
</td></tr></table></p></p></td></tr></table><p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
<tr bgcolor="#eeaa77" >
|
||||
<td colspan="3" valign="bottom" > <br />
|
||||
@ -135,34 +325,34 @@ Data descriptors inherited from <a href="builtins.html#BaseException" >builtins.
|
||||
If #timeout is provided as a lambda/function, it will be called<br />
|
||||
prior to each invocation of the decorated function to calculate the timeout to be used<br />
|
||||
for that call, based on the arguments passed to the decorated function.<br />
|
||||
<br />
|
||||
<br />
|
||||
For example, you may have a "processData" function whose execution time<br />
|
||||
depends on the number of "data" elements, so you may want a million elements to have a <br />
|
||||
depends on the number of "data" elements, so you may want a million elements to have a<br />
|
||||
much higher timeout than seven elements.)<br />
|
||||
<br />
|
||||
If #allowOverride is True AND a kwarg of "forceTimeout" is passed to the wrapped function, that timeout<br />
|
||||
will be used for that single call.<br />
|
||||
<br />
|
||||
@param timeout <float OR lambda/function> - <br />
|
||||
@param timeout <float OR lambda/function> -<br />
|
||||
<br />
|
||||
**If float:**<br />
|
||||
Default number of seconds max to allow function to execute<br />
|
||||
before throwing <a href="#FunctionTimedOut" >FunctionTimedOut</a><br />
|
||||
<br />
|
||||
<br />
|
||||
**If lambda/function:<br />
|
||||
<br />
|
||||
If a function/lambda is provided, it will be called for every<br />
|
||||
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed) <br />
|
||||
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed)<br />
|
||||
to determine the timeout to use based on the arguments to the decorated function.<br />
|
||||
<br />
|
||||
The arguments as passed into the decorated function will be passed to this function.<br />
|
||||
They either must match exactly to what the decorated function has, OR<br />
|
||||
if you prefer to get the *args (list of ordered args) and **kwargs ( key : value keyword args form),<br />
|
||||
define your calculate function like:<br />
|
||||
<br />
|
||||
<br />
|
||||
def calculateTimeout(*args, **kwargs):<br />
|
||||
...<br />
|
||||
<br />
|
||||
<br />
|
||||
or lambda like:<br />
|
||||
<br />
|
||||
calculateTimeout = lambda *args, **kwargs : ...<br />
|
||||
@ -204,6 +394,6 @@ to return cleanly, but in most cases it
|
||||
<font color="#ffffff" face="helvetica, arial" ><big ><strong >Data</strong></big></font></td></tr>
|
||||
|
||||
<tr ><td bgcolor="#55aa55" ><tt > </tt></td><td > </td>
|
||||
<td width="100%" ><strong >__all__</strong> = ('func_timeout', 'func_set_timeout', 'FunctionTimedOut')<br />
|
||||
<strong >__version_tuple__</strong> = (4, 1, 0)</td></tr></table>
|
||||
<td width="100%" ><strong >__all__</strong> = ('func_timeout', 'func_set_timeout', 'FunctionTimedOut', 'StoppableThread')<br />
|
||||
<strong >__version_tuple__</strong> = (4, 3, 5)</td></tr></table>
|
||||
</p></p></p></p></body></html>
|
||||
@ -1,23 +1,20 @@
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||||
<html><head><title>Python: module func_timeout.py2_raise</title>
|
||||
<meta charset="utf-8">
|
||||
</head><body bgcolor="#f0f0f8">
|
||||
<html ><head ><title >Python: module func_timeout.py2_raise</title>
|
||||
<meta charset="utf-8" />
|
||||
</head><body bgcolor="#f0f0f8" >
|
||||
|
||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
|
||||
<tr bgcolor="#7799ee">
|
||||
<td valign=bottom> <br>
|
||||
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="func_timeout.html"><font color="#ffffff">func_timeout</font></a>.py2_raise</strong></big></big></font></td
|
||||
><td align=right valign=bottom
|
||||
><font color="#ffffff" face="helvetica, arial"><a href="func_timeout.html">index</a><br></td></tr></table>
|
||||
<p><tt># Python2 allows specifying an alternate traceback.</tt></p>
|
||||
<p>
|
||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||||
<tr bgcolor="#eeaa77">
|
||||
<td colspan=3 valign=bottom> <br>
|
||||
<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="heading" >
|
||||
<tr bgcolor="#7799ee" >
|
||||
<td valign="bottom" > <br />
|
||||
<font color="#ffffff" face="helvetica, arial" > <br /><big ><big ><strong ><a href="func_timeout.html" ><font color="#ffffff" >func_timeout</font></a>.py2_raise</strong></big></big></font></td><td align="right" valign="bottom" ><font color="#ffffff" face="helvetica, arial" ><a href="func_timeout.html" >index</a></font></td></tr></table>
|
||||
<p ><tt ># Python2 allows specifying an alternate traceback.</tt></p>
|
||||
<p >
|
||||
<table width="100%" cellspacing="0" cellpadding="2" border="0" summary="section" >
|
||||
<tr bgcolor="#eeaa77" >
|
||||
<td colspan="3" valign="bottom" > <br />
|
||||
<font color="#ffffff" face="helvetica, arial" ><big ><strong >Functions</strong></big></font></td></tr>
|
||||
|
||||
<tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td>
|
||||
<td width="100%"><dl><dt><a name="-raise_exception"><strong>raise_exception</strong></a>(exception)</dt><dd><tt># Python2 allows specifying an alternate traceback.</tt></dd></dl>
|
||||
<tr ><td bgcolor="#eeaa77" ><tt > </tt></td><td > </td>
|
||||
<td width="100%" ><dl ><dt ><a name="-raise_exception" ><strong >raise_exception</strong></a>(exception)</dt><dd ><tt ># Python2 allows specifying an alternate traceback.</tt></dd></dl>
|
||||
</td></tr></table>
|
||||
</body></html>
|
||||
</p></body></html>
|
||||
@ -1,5 +1,5 @@
|
||||
'''
|
||||
Copyright (c) 2016, 2017 Tim Savannah All Rights Reserved.
|
||||
Copyright (c) 2016, 2017, 2019 Timothy Savannah All Rights Reserved.
|
||||
|
||||
Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as
|
||||
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE
|
||||
@ -8,43 +8,101 @@
|
||||
import os
|
||||
import ctypes
|
||||
import threading
|
||||
import time
|
||||
import sys
|
||||
|
||||
__all__ = ('StoppableThread', 'JoinThread')
|
||||
|
||||
class StoppableThread(threading.Thread):
|
||||
'''
|
||||
StoppableThread - A thread that can be stopped by forcing an exception in the execution context.
|
||||
|
||||
This works both to interrupt code that is in C or in python code, at either the next call to a python function,
|
||||
or the next line in python code.
|
||||
|
||||
It is recommended that if you call stop ( @see StoppableThread.stop ) that you use an exception that inherits BaseException, to ensure it likely isn't caught.
|
||||
|
||||
Also, beware unmarked exception handlers in your code. Code like this:
|
||||
|
||||
while True:
|
||||
try:
|
||||
doSomething()
|
||||
except:
|
||||
continue
|
||||
|
||||
will never be able to abort, because the exception you raise is immediately caught.
|
||||
|
||||
The exception is raised over and over, with a specifed delay (default 2.0 seconds)
|
||||
'''
|
||||
|
||||
|
||||
def _stopThread(self, exception):
|
||||
if self.isAlive() is False:
|
||||
def _stopThread(self, exception, raiseEvery=2.0):
|
||||
'''
|
||||
_stopThread - @see StoppableThread.stop
|
||||
'''
|
||||
if self.is_alive() is False:
|
||||
return True
|
||||
|
||||
self._stderr = open(os.devnull, 'w')
|
||||
joinThread = JoinThread(self, exception)
|
||||
|
||||
# Create "joining" thread which will raise the provided exception
|
||||
# on a repeat, until the thread stops.
|
||||
joinThread = JoinThread(self, exception, repeatEvery=raiseEvery)
|
||||
|
||||
# Try to prevent spurrious prints
|
||||
joinThread._stderr = self._stderr
|
||||
joinThread.start()
|
||||
joinThread._stderr = self._stderr
|
||||
|
||||
def stop(self, exception):
|
||||
|
||||
def stop(self, exception, raiseEvery=2.0):
|
||||
'''
|
||||
Stops the thread by raising a given exception.
|
||||
|
||||
@param exception <Exception> - Exception to throw. Likely, you want to use something
|
||||
@param exception <Exception type> - Exception to throw. Likely, you want to use something
|
||||
|
||||
that inherits from BaseException (so except Exception as e: continue; isn't a problem)
|
||||
|
||||
This should be a class/type, NOT an instance, i.e. MyExceptionType not MyExceptionType()
|
||||
|
||||
|
||||
@param raiseEvery <float> Default 2.0 - We will keep raising this exception every #raiseEvery seconds,
|
||||
|
||||
until the thread terminates.
|
||||
|
||||
If your code traps a specific exception type, this will allow you #raiseEvery seconds to cleanup before exit.
|
||||
|
||||
If you're calling third-party code you can't control, which catches BaseException, set this to a low number
|
||||
|
||||
to break out of their exception handler.
|
||||
|
||||
|
||||
@return <None>
|
||||
'''
|
||||
return self._stopThread(exception)
|
||||
return self._stopThread(exception, raiseEvery)
|
||||
|
||||
|
||||
class JoinThread(threading.Thread):
|
||||
'''
|
||||
JoinThread - The workhouse that stops the StoppableThread
|
||||
JoinThread - The workhouse that stops the StoppableThread.
|
||||
|
||||
Takes an exception, and upon being started immediately raises that exception in the current context
|
||||
of the thread's execution (so next line of python gets it, or next call to a python api function in C code ).
|
||||
|
||||
@see StoppableThread for more details
|
||||
'''
|
||||
|
||||
def __init__(self, otherThread, exception, repeatEvery=2.0):
|
||||
'''
|
||||
__init__ - Create a JoinThread (don't forget to call .start() ! )
|
||||
|
||||
@param otherThread <threading.Thread> - A thread
|
||||
|
||||
@param exception <BaseException> - An exception. Should be a BaseException, to prevent "catch Exception as e: continue" type code
|
||||
from never being terminated. If such code is unavoidable, you can try setting #repeatEvery to a very low number, like .00001,
|
||||
and it will hopefully raise within the context of the catch, and be able to break free.
|
||||
|
||||
@param repeatEvery <float> Default 2.0 - After starting, the given exception is immediately raised. Then, every #repeatEvery seconds,
|
||||
it is raised again, until the thread terminates.
|
||||
'''
|
||||
threading.Thread.__init__(self)
|
||||
self.otherThread = otherThread
|
||||
self.exception = exception
|
||||
@ -52,6 +110,9 @@ class JoinThread(threading.Thread):
|
||||
self.daemon = True
|
||||
|
||||
def run(self):
|
||||
'''
|
||||
run - The thread main. Will attempt to stop and join the attached thread.
|
||||
'''
|
||||
|
||||
# Try to silence default exception printing.
|
||||
self.otherThread._Thread__stderr = self._stderr
|
||||
@ -59,7 +120,7 @@ class JoinThread(threading.Thread):
|
||||
# If py2, call this first to start thread termination cleanly.
|
||||
# Python3 does not need such ( nor does it provide.. )
|
||||
self.otherThread._Thread__stop()
|
||||
while self.otherThread.isAlive():
|
||||
while self.otherThread.is_alive():
|
||||
# We loop raising exception incase it's caught hopefully this breaks us far out.
|
||||
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(self.otherThread.ident), ctypes.py_object(self.exception))
|
||||
self.otherThread.join(self.repeatEvery)
|
||||
@ -69,4 +130,4 @@ class JoinThread(threading.Thread):
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
# vim: set ts=4 sw=4 expandtab :
|
||||
|
||||
@ -1,15 +1,16 @@
|
||||
'''
|
||||
Copyright (c) 2016, 2017 Tim Savannah All Rights Reserved.
|
||||
Copyright (c) 2016, 2017, 2019 Tim Savannah All Rights Reserved.
|
||||
|
||||
Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as
|
||||
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE
|
||||
'''
|
||||
|
||||
|
||||
__version__ = '4.1.0'
|
||||
__version_tuple__ = (4, 1, 0)
|
||||
__version__ = '4.3.5'
|
||||
__version_tuple__ = (4, 3, 5)
|
||||
|
||||
__all__ = ('func_timeout', 'func_set_timeout', 'FunctionTimedOut')
|
||||
__all__ = ('func_timeout', 'func_set_timeout', 'FunctionTimedOut', 'StoppableThread')
|
||||
|
||||
from .exceptions import FunctionTimedOut
|
||||
from .dafunc import func_timeout, func_set_timeout
|
||||
from .StoppableThread import StoppableThread
|
||||
|
||||
@ -25,6 +25,8 @@ except SyntaxError:
|
||||
except ImportError:
|
||||
from .py2_raise import raise_exception
|
||||
|
||||
from functools import wraps
|
||||
|
||||
__all__ = ('func_timeout', 'func_set_timeout')
|
||||
|
||||
|
||||
@ -72,7 +74,7 @@ def func_timeout(timeout, func, args=(), kwargs=None):
|
||||
if isStopped is False:
|
||||
# Assemble the alternate traceback, excluding this function
|
||||
# from the trace (by going to next frame)
|
||||
# Pytohn3 reads native from __traceback__,
|
||||
# Pytohn3 reads native from __traceback__,
|
||||
# python2 has a different form for "raise"
|
||||
e.__traceback__ = exc_info[2].tb_next
|
||||
exception.append( e )
|
||||
@ -84,7 +86,7 @@ def func_timeout(timeout, func, args=(), kwargs=None):
|
||||
thread.join(timeout)
|
||||
|
||||
stopException = None
|
||||
if thread.isAlive():
|
||||
if thread.is_alive():
|
||||
isStopped = True
|
||||
|
||||
class FunctionTimedOutTempType(FunctionTimedOut):
|
||||
@ -118,34 +120,34 @@ def func_set_timeout(timeout, allowOverride=False):
|
||||
If #timeout is provided as a lambda/function, it will be called
|
||||
prior to each invocation of the decorated function to calculate the timeout to be used
|
||||
for that call, based on the arguments passed to the decorated function.
|
||||
|
||||
|
||||
For example, you may have a "processData" function whose execution time
|
||||
depends on the number of "data" elements, so you may want a million elements to have a
|
||||
depends on the number of "data" elements, so you may want a million elements to have a
|
||||
much higher timeout than seven elements.)
|
||||
|
||||
If #allowOverride is True AND a kwarg of "forceTimeout" is passed to the wrapped function, that timeout
|
||||
will be used for that single call.
|
||||
|
||||
@param timeout <float OR lambda/function> -
|
||||
|
||||
@param timeout <float OR lambda/function> -
|
||||
|
||||
**If float:**
|
||||
Default number of seconds max to allow function to execute
|
||||
before throwing FunctionTimedOut
|
||||
|
||||
|
||||
**If lambda/function:
|
||||
|
||||
If a function/lambda is provided, it will be called for every
|
||||
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed)
|
||||
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed)
|
||||
to determine the timeout to use based on the arguments to the decorated function.
|
||||
|
||||
The arguments as passed into the decorated function will be passed to this function.
|
||||
They either must match exactly to what the decorated function has, OR
|
||||
if you prefer to get the *args (list of ordered args) and **kwargs ( key : value keyword args form),
|
||||
define your calculate function like:
|
||||
|
||||
|
||||
def calculateTimeout(*args, **kwargs):
|
||||
...
|
||||
|
||||
|
||||
or lambda like:
|
||||
|
||||
calculateTimeout = lambda *args, **kwargs : ...
|
||||
@ -180,7 +182,7 @@ def func_set_timeout(timeout, allowOverride=False):
|
||||
# Only defaultTimeout provided. Simple function wrapper
|
||||
def _function_decorator(func):
|
||||
|
||||
return lambda *args, **kwargs : func_timeout(defaultTimeout, func, args=args, kwargs=kwargs)
|
||||
return wraps(func)(lambda *args, **kwargs : func_timeout(defaultTimeout, func, args=args, kwargs=kwargs))
|
||||
|
||||
# def _function_wrapper(*args, **kwargs):
|
||||
# return func_timeout(defaultTimeout, func, args=args, kwargs=kwargs)
|
||||
@ -198,7 +200,7 @@ def func_set_timeout(timeout, allowOverride=False):
|
||||
|
||||
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
||||
|
||||
return _function_wrapper
|
||||
return wraps(func)(_function_wrapper)
|
||||
return _function_decorator
|
||||
|
||||
|
||||
@ -217,7 +219,7 @@ def func_set_timeout(timeout, allowOverride=False):
|
||||
|
||||
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
||||
|
||||
return _function_wrapper
|
||||
return wraps(func)(_function_wrapper)
|
||||
return _function_decorator
|
||||
|
||||
# Cannot override, and calculate timeout function
|
||||
@ -227,7 +229,7 @@ def func_set_timeout(timeout, allowOverride=False):
|
||||
|
||||
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
||||
|
||||
return _function_wrapper
|
||||
return wraps(func)(_function_wrapper)
|
||||
return _function_decorator
|
||||
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
'''
|
||||
Copyright (c) 2016 Tim Savannah All Rights Reserved.
|
||||
Copyright (c) 2016, 2017, 2019 Tim Savannah All Rights Reserved.
|
||||
|
||||
Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as
|
||||
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE
|
||||
@ -25,6 +25,22 @@ class FunctionTimedOut(BaseException):
|
||||
|
||||
|
||||
def __init__(self, msg='', timedOutAfter=None, timedOutFunction=None, timedOutArgs=None, timedOutKwargs=None):
|
||||
'''
|
||||
__init__ - Create this exception type.
|
||||
|
||||
You should not need to do this outside of testing, it will be created by the func_timeout API
|
||||
|
||||
@param msg <str> - A predefined message, otherwise we will attempt to generate one from the other arguments.
|
||||
|
||||
@param timedOutAfter <None/float> - Number of seconds before timing-out. Filled-in by API, None will produce "Unknown"
|
||||
|
||||
@param timedOutFunction <None/function> - Reference to the function that timed-out. Filled-in by API." None will produce "Unknown Function"
|
||||
|
||||
@param timedOutArgs <None/tuple/list> - List of fixed-order arguments ( *args ), or None for no args.
|
||||
|
||||
@param timedOutKwargs <None/dict> - Dict of keyword arg ( **kwargs ) names to values, or None for no kwargs.
|
||||
|
||||
'''
|
||||
|
||||
self.timedOutAfter = timedOutAfter
|
||||
|
||||
@ -46,14 +62,25 @@ class FunctionTimedOut(BaseException):
|
||||
|
||||
@return <str> - Message
|
||||
'''
|
||||
return 'Function %s (args=%s) (kwargs=%s) timed out after %f seconds.\n' %(self.timedOutFunction.__name__, repr(self.timedOutArgs), repr(self.timedOutKwargs), self.timedOutAfter)
|
||||
# Try to gather the function name, if available.
|
||||
# If it is not, default to an "unknown" string to allow default instantiation
|
||||
if self.timedOutFunction is not None:
|
||||
timedOutFuncName = self.timedOutFunction.__name__
|
||||
else:
|
||||
timedOutFuncName = 'Unknown Function'
|
||||
if self.timedOutAfter is not None:
|
||||
timedOutAfterStr = "%f" %(self.timedOutAfter, )
|
||||
else:
|
||||
timedOutAfterStr = "Unknown"
|
||||
|
||||
return 'Function %s (args=%s) (kwargs=%s) timed out after %s seconds.\n' %(timedOutFuncName, repr(self.timedOutArgs), repr(self.timedOutKwargs), timedOutAfterStr)
|
||||
|
||||
def retry(self, timeout=RETRY_SAME_TIMEOUT):
|
||||
'''
|
||||
retry - Retry the timed-out function with same arguments.
|
||||
|
||||
@param timeout <float/RETRY_SAME_TIMEOUT/None> Default RETRY_SAME_TIMEOUT
|
||||
|
||||
|
||||
If RETRY_SAME_TIMEOUT : Will retry the function same args, same timeout
|
||||
If a float/int : Will retry the function same args with provided timeout
|
||||
If None : Will retry function same args no timeout
|
||||
@ -62,7 +89,7 @@ class FunctionTimedOut(BaseException):
|
||||
'''
|
||||
if timeout is None:
|
||||
return self.timedOutFunction(*(self.timedOutArgs), **self.timedOutKwargs)
|
||||
|
||||
|
||||
from .dafunc import func_timeout
|
||||
|
||||
if timeout == RETRY_SAME_TIMEOUT:
|
||||
|
||||
6
mkdoc.sh
6
mkdoc.sh
@ -1,9 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Ensure we are in the project root directory
|
||||
cd "$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||
|
||||
shopt -s nullglob
|
||||
ALL_MODS="$(echo func_timeout/*.py | tr ' ' '\n' | sed -e 's|/|.|g' -e 's|.py$||g' -e 's|.__init__$||g' | tr '\n' ' ')"
|
||||
|
||||
pydoc -w ${ALL_MODS}
|
||||
mv *.html doc/
|
||||
mv func_timeout*.html doc/
|
||||
pushd doc >/dev/null 2>&1
|
||||
rm -f index.html
|
||||
|
||||
|
||||
9
setup.py
9
setup.py
@ -20,7 +20,7 @@ if __name__ == '__main__':
|
||||
if dirName and os.getcwd() != dirName:
|
||||
os.chdir(dirName)
|
||||
|
||||
summary = 'Python module which allows you to specify timeouts when calling any existing function'
|
||||
summary = 'Python module which allows you to specify timeouts when calling any existing function. Also provides support for stoppable-threads'
|
||||
|
||||
try:
|
||||
with open('README.rst', 'rt') as f:
|
||||
@ -30,7 +30,7 @@ if __name__ == '__main__':
|
||||
log_description = summary
|
||||
|
||||
setup(name='func_timeout',
|
||||
version='4.1.0',
|
||||
version='4.3.7',
|
||||
packages=['func_timeout'],
|
||||
author='Tim Savannah',
|
||||
author_email='kata198@gmail.com',
|
||||
@ -48,6 +48,11 @@ if __name__ == '__main__':
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
'Programming Language :: Python :: 3.5',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: 3.7',
|
||||
'Programming Language :: Python :: 3.8',
|
||||
'Programming Language :: Python :: 3.9',
|
||||
'Programming Language :: Python :: 3.10',
|
||||
'Topic :: Software Development :: Libraries :: Python Modules'
|
||||
]
|
||||
)
|
||||
|
||||
@ -1,4 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
testit.py - Example code for ad-hoc function timeouts.
|
||||
|
||||
Newer tests for all features found in "tests" directory.
|
||||
'''
|
||||
|
||||
from func_timeout import func_timeout, FunctionTimedOut
|
||||
import time
|
||||
@ -23,6 +28,7 @@ if __name__ == '__main__':
|
||||
myException = e
|
||||
pass
|
||||
|
||||
print ( "\nRetrying with longer timeout, should get 16+17=33:" )
|
||||
if myException is not None:
|
||||
print ( "\nGot: %s\n" %( str(myException.retry(2.5)), ) )
|
||||
else:
|
||||
|
||||
@ -160,6 +160,7 @@ def compareTimes(timeEnd, timeStart, cmpTime, roundTo=None, deltaFixed=.05, delt
|
||||
@param roundTo <None/int> - Number of digits to round-off to
|
||||
|
||||
@param deltaFixed <float/None> Default .05, If provided and if difference is within this much, the two values are considered equal
|
||||
|
||||
@param deltaPct <float/None> Default None, if provided and if difference is within this much, the two values are considered equal. 1 = 100%, .5 = 50%
|
||||
|
||||
Example: if trying to determine if function ran for 2 seconds with an error of .05 seconds,
|
||||
|
||||
@ -25,7 +25,7 @@ class TestBasic(object):
|
||||
'''
|
||||
|
||||
def test_funcTimeout(self):
|
||||
sleepFunction = getSleepLambda(1.25)
|
||||
sleepFunction = getSleepLambda(2.00)
|
||||
|
||||
expectedResult = 5 + 13
|
||||
|
||||
@ -36,9 +36,9 @@ class TestBasic(object):
|
||||
assert result == expectedResult , 'Did not get return from sleepFunction'
|
||||
|
||||
try:
|
||||
result = func_timeout(1.5, sleepFunction, args=(5, 13))
|
||||
result = func_timeout(2.5, sleepFunction, args=(5, 13))
|
||||
except FunctionTimedOut as te:
|
||||
raise AssertionError('Got unexpected timeout at 1.5 second timeout for 1.25 second function: %s' %(str(te),))
|
||||
raise AssertionError('Got unexpected timeout at 2.5 second timeout for 2.00 second function: %s' %(str(te),))
|
||||
|
||||
assert result == expectedResult , 'Got wrong return from func_timeout.\nGot: %s\nExpected: %s\n' %(repr(result), repr(expectedResult))
|
||||
|
||||
@ -51,16 +51,16 @@ class TestBasic(object):
|
||||
assert gotException , 'Expected to get FunctionTimedOut exception for 1.25 sec function at 1s timeout'
|
||||
|
||||
try:
|
||||
result = func_timeout(1.5, sleepFunction, args=(5,), kwargs={ 'b' : 13})
|
||||
result = func_timeout(2.5, sleepFunction, args=(5,), kwargs={ 'b' : 13})
|
||||
except FunctionTimedOut as te:
|
||||
raise AssertionError('Got unexpected timeout at 1.5 second timeout for 1.25 second function: %s' %(str(te), ))
|
||||
raise AssertionError('Got unexpected timeout at 2.5 second timeout for 2.00 second function: %s' %(str(te), ))
|
||||
except Exception as e:
|
||||
raise AssertionError('Got unknown exception mixing args and kwargs: < %s > %s' %(e.__class__.__name__, str(e)))
|
||||
|
||||
assert result == expectedResult , 'Got wrong result when mixing args and kwargs'
|
||||
|
||||
def test_retry(self):
|
||||
sleepFunction = getSleepLambda(.5)
|
||||
sleepFunction = getSleepLambda(1.2)
|
||||
|
||||
expectedResult = 5 + 19
|
||||
|
||||
@ -69,14 +69,14 @@ class TestBasic(object):
|
||||
|
||||
startTime = time.time()
|
||||
try:
|
||||
result = func_timeout(.3, sleepFunction, args=(5, 19))
|
||||
result = func_timeout(.8, sleepFunction, args=(5, 19))
|
||||
except FunctionTimedOut as fte:
|
||||
functionTimedOut = fte
|
||||
gotException = True
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception'
|
||||
assert compareTimes(endTime, startTime, .3, 3, None, .10) == 0 , 'Expected to wait .3 seconds. Was: %f - %f = %f' %(endTime, startTime, round(endTime - startTime, 3))
|
||||
assert compareTimes(endTime, startTime, .8, 3, deltaFixed=.15) == 0 , 'Expected to wait .8 seconds. Was: %f - %f = %f' %(endTime, startTime, round(endTime - startTime, 3))
|
||||
|
||||
gotException = False
|
||||
startTime = time.time()
|
||||
@ -87,7 +87,7 @@ class TestBasic(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception on retry.'
|
||||
assert compareTimes(endTime, startTime, .3, 3, None, .10) == 0 , 'Expected retry with no arguments to use same timeout of .3'
|
||||
assert compareTimes(endTime, startTime, .8, 3, deltaFixed=.15) == 0 , 'Expected retry with no arguments to use same timeout of .8'
|
||||
|
||||
gotException = False
|
||||
startTime = time.time()
|
||||
@ -98,19 +98,19 @@ class TestBasic(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Did NOT to get exception with no timeout'
|
||||
assert compareTimes(endTime, startTime, .5, 3, None, .10) == 0 , 'Expected retry with None as timeout to last full length of function'
|
||||
assert compareTimes(endTime, startTime, 1.2, 3, deltaFixed=.15) == 0 , 'Expected retry with None as timeout to last full length of function'
|
||||
|
||||
gotException = False
|
||||
startTime = time.time()
|
||||
try:
|
||||
result = functionTimedOut.retry(.4)
|
||||
result = functionTimedOut.retry(.55)
|
||||
except FunctionTimedOut:
|
||||
gotException = True
|
||||
finally:
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to time out after .4 seconds when providing .4'
|
||||
assert compareTimes(endTime, startTime, .4, 3, .05, None) == 0 , 'Expected providing .4 would allow timeout of up to .4 seconds'
|
||||
assert gotException , 'Expected to time out after .55 seconds when providing .55'
|
||||
assert compareTimes(endTime, startTime, .55, 3, deltaFixed=.15) == 0 , 'Expected providing .55 would allow timeout of up to .55 seconds'
|
||||
|
||||
threadsCleanedUp = False
|
||||
|
||||
@ -122,11 +122,11 @@ class TestBasic(object):
|
||||
threadsCleanedUp = True
|
||||
break
|
||||
|
||||
|
||||
|
||||
assert threadsCleanedUp , 'Expected other threads to get cleaned up after gc collection'
|
||||
|
||||
def test_exception(self):
|
||||
sleepFunction = getSleepLambda(.5)
|
||||
sleepFunction = getSleepLambda(.85)
|
||||
|
||||
expectedResult = 5 + 19
|
||||
|
||||
@ -135,7 +135,7 @@ class TestBasic(object):
|
||||
|
||||
startTime = time.time()
|
||||
try:
|
||||
result = func_timeout(.3, sleepFunction, args=(5, 19))
|
||||
result = func_timeout(.5, sleepFunction, args=(5, 19))
|
||||
except FunctionTimedOut as fte:
|
||||
functionTimedOut = fte
|
||||
gotException = True
|
||||
@ -144,14 +144,43 @@ class TestBasic(object):
|
||||
assert gotException , 'Expected to get exception'
|
||||
|
||||
assert 'timed out after ' in functionTimedOut.msg , 'Expected message to be constructed. Got: %s' %(repr(functionTimedOut.msg), )
|
||||
assert round(functionTimedOut.timedOutAfter, 1) == .3 , 'Expected timedOutAfter to equal timeout ( .3 ). Got: %s' %(str(round(functionTimedOut.timedOutAfter, 1)), )
|
||||
assert round(functionTimedOut.timedOutAfter, 1) == .5 , 'Expected timedOutAfter to equal timeout ( .5 ). Got: %s' %(str(round(functionTimedOut.timedOutAfter, 1)), )
|
||||
assert functionTimedOut.timedOutFunction == sleepFunction , 'Expected timedOutFunction to equal sleepFunction'
|
||||
assert functionTimedOut.timedOutArgs == (5, 19) , 'Expected args to equal (5, 19)'
|
||||
assert functionTimedOut.timedOutKwargs == {} , 'Expected timedOutKwargs to equal {}'
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def test_instantiateExceptionNoArgs(self):
|
||||
|
||||
gotException = False
|
||||
|
||||
try:
|
||||
exc = FunctionTimedOut()
|
||||
msg = str(exc)
|
||||
msg2 = exc.getMsg()
|
||||
|
||||
except Exception as _e:
|
||||
sys.stderr.write('Got unexpected exception in test_instantiateExceptionNoArgs with no arguments. %s %s\n\n' %(str(type(_e)), str(_e)))
|
||||
gotException = True
|
||||
|
||||
assert gotException is False, 'Expected to be able to create FunctionTimedOut exception without arguments.'
|
||||
|
||||
gotException = False
|
||||
|
||||
try:
|
||||
exc = FunctionTimedOut('test')
|
||||
msg = str(exc)
|
||||
msg2 = str(exc.getMsg())
|
||||
|
||||
except Exception as _e:
|
||||
sys.stderr.write('Got unexpected exception in test_instantiateExceptionNoArgs with fixed message string. %s %s\n\n' %(str(type(_e)), str(_e)))
|
||||
gotException = True
|
||||
|
||||
assert gotException is False , 'Expected to be able to create a FunctionTimedOut exception with a fixed message.'
|
||||
|
||||
# Other forms (providing the function name) are tested elsewhere.
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(subprocess.Popen('GoodTests.py -n1 "%s" %s' %(sys.argv[0], ' '.join(['"%s"' %(arg.replace('"', '\\"'), ) for arg in sys.argv[1:]]) ), shell=True).wait())
|
||||
|
||||
@ -19,7 +19,7 @@ from func_timeout import func_timeout, FunctionTimedOut, func_set_timeout
|
||||
|
||||
from TestUtils import ARG_NO_DEFAULT, getSleepLambda, getSleepLambdaWithArgs, compareTimes
|
||||
|
||||
SLEEP_TIME = .5
|
||||
SLEEP_TIME = 1.25
|
||||
|
||||
def doSleep(a, b):
|
||||
time.sleep(a)
|
||||
@ -52,7 +52,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception at sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep up to sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, deltaFixed=.15) == 0 , 'Expected to sleep up to sleep time'
|
||||
|
||||
expected = SLEEP_TIME * .8 + 4
|
||||
gotException = False
|
||||
@ -64,7 +64,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected not to get exception at 80% sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to only sleep for 80% of sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, deltaFixed=.15) == 0 , 'Expected to only sleep for 80% of sleep time'
|
||||
assert result == expected , 'Got wrong result'
|
||||
|
||||
def test_funcSetTimeoutOverride(self):
|
||||
@ -85,7 +85,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception at 130% sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * 1.2, None, .1) == 0 , 'Expected to sleep up to 120% of sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * 1.2, None, deltaFixed=.15) == 0 , 'Expected to sleep up to 120% of sleep time'
|
||||
|
||||
@func_set_timeout(SLEEP_TIME, allowOverride=False)
|
||||
def doSleepFuncNoOverride(a, b):
|
||||
@ -113,7 +113,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected not to get exception with forced 115% sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for SLEEP_TIME'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, deltaFixed=.15) == 0 , 'Expected to sleep for SLEEP_TIME'
|
||||
assert result == expected , 'Got wrong result'
|
||||
|
||||
def test_funcSetTimeCalculate(self):
|
||||
@ -145,7 +145,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected not to get exception with calculated 120% timeout on sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for SLEEP_TIME'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, deltaFixed=.15) == 0 , 'Expected to sleep for SLEEP_TIME'
|
||||
assert result == expected , 'Got wrong result'
|
||||
|
||||
|
||||
@ -164,7 +164,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception with calculated 80% timeout on sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to sleep for 80% SLEEP_TIME'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, deltaFixed=.15) == 0 , 'Expected to sleep for 80% SLEEP_TIME'
|
||||
|
||||
@func_set_timeout(calculateSleepOverArgs, allowOverride=False)
|
||||
def doSleepFuncOverArgs(a, b):
|
||||
@ -181,7 +181,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected not to get exception with calculated 120% timeout on sleep time using *args'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for SLEEP_TIME using *args'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, deltaFixed=.15) == 0 , 'Expected to sleep for SLEEP_TIME using *args'
|
||||
assert result == expected , 'Got wrong result'
|
||||
|
||||
@func_set_timeout(calculateSleepUnderArgs, allowOverride=False)
|
||||
@ -199,7 +199,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception with calculated 80% timeout on sleep time using *args'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to sleep for 80% SLEEP_TIME using *args'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, deltaFixed=.15) == 0 , 'Expected to sleep for 80% SLEEP_TIME using *args'
|
||||
|
||||
|
||||
def test_funcSetTimeCalculateWithOverride(self):
|
||||
@ -231,7 +231,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected not to get exception with calculated 120% timeout on sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for SLEEP_TIME'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, deltaFixed=.15) == 0 , 'Expected to sleep for SLEEP_TIME'
|
||||
assert result == expected , 'Got wrong result'
|
||||
|
||||
expected = SLEEP_TIME + 4
|
||||
@ -244,7 +244,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected not to get exception with calculated 120% timeout on sleep time but 150% timeout on override'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for SLEEP_TIME with 150% timeout on override'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, deltaFixed=.15) == 0 , 'Expected to sleep for SLEEP_TIME with 150% timeout on override'
|
||||
assert result == expected , 'Got wrong result'
|
||||
|
||||
|
||||
@ -258,7 +258,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception with calculated 120% timeout on sleep time but 70% timeout on override'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .7, None, .1) == 0 , 'Expected to sleep for 70% SLEEP_TIME with 70% timeout on override'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .7, None, deltaFixed=.15) == 0 , 'Expected to sleep for 70% SLEEP_TIME with 70% timeout on override'
|
||||
|
||||
|
||||
@func_set_timeout(calculateSleepUnder, allowOverride=True)
|
||||
@ -276,7 +276,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception with calculated 80% timeout on sleep time'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to sleep for 80% SLEEP_TIME'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, deltaFixed=.15) == 0 , 'Expected to sleep for 80% SLEEP_TIME'
|
||||
|
||||
expected = SLEEP_TIME + 4
|
||||
gotException = False
|
||||
@ -288,7 +288,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected not to get exception with calculated 80% timeout on sleep time but 120% timeout on override'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME , None, .1) == 0 , 'Expected to sleep for SLEEP_TIME with 120% timeout on override'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME , None, deltaFixed=.15) == 0 , 'Expected to sleep for SLEEP_TIME with 120% timeout on override'
|
||||
|
||||
|
||||
def test_setFuncTimeoutetry(self):
|
||||
@ -322,7 +322,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception with calculated 80% timeout'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to sleep for 80% SLEEP_TIME with 80% timeout'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, deltaFixed=.15) == 0 , 'Expected to sleep for 80% SLEEP_TIME with 80% timeout'
|
||||
|
||||
gotException = False
|
||||
startTime = time.time()
|
||||
@ -335,7 +335,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception with calculated same 80% timeout on retry'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, .1) == 0 , 'Expected to sleep for 80% SLEEP_TIME with same 80% timeout on retry'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .8, None, deltaFixed=.15) == 0 , 'Expected to sleep for 80% SLEEP_TIME with same 80% timeout on retry'
|
||||
|
||||
result = None
|
||||
gotException = False
|
||||
@ -349,7 +349,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected to get exception with calculated 80% timeout on retry ( None ) [ No timeout ]'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, .1) == 0 , 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( None ) [ No timeout ]'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME, None, deltaFixed=.15) == 0 , 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( None ) [ No timeout ]'
|
||||
assert result == expected , 'Got wrong result'
|
||||
|
||||
|
||||
@ -365,7 +365,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception with calculated 80% timeout overriden by 60% timeout on retry'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .6, None, .1) == 0 , 'Expected to sleep for 60% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * .6 ) [ 60% timeout ]'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME * .6, None, deltaFixed=.15) == 0 , 'Expected to sleep for 60% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * .6 ) [ 60% timeout ]'
|
||||
|
||||
result = None
|
||||
gotException = False
|
||||
@ -379,7 +379,7 @@ class TestDecorator(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Expected to get exception with calculated 80% timeout overriden by 150% timeout on retry'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME , None, .1) == 0 , 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * 1.5 ) [ 150% timeout ]'
|
||||
assert compareTimes(endTime, startTime, SLEEP_TIME , None, deltaFixed=.15) == 0 , 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * 1.5 ) [ 150% timeout ]'
|
||||
assert result == expected
|
||||
|
||||
threadsCleanedUp = False
|
||||
@ -394,6 +394,45 @@ class TestDecorator(object):
|
||||
|
||||
|
||||
assert threadsCleanedUp , 'Expected other threads to get cleaned up after gc collection'
|
||||
|
||||
def test_nameRetained(self):
|
||||
|
||||
# Case of just timeout
|
||||
@func_set_timeout(2, allowOverride=False)
|
||||
def hello():
|
||||
pass
|
||||
|
||||
assert hello.__name__ == 'hello'
|
||||
|
||||
del hello
|
||||
|
||||
def getTimeoutFunc():
|
||||
return 2
|
||||
|
||||
# Timeout is function
|
||||
@func_set_timeout(getTimeoutFunc, allowOverride=False)
|
||||
def hello2():
|
||||
pass
|
||||
|
||||
assert hello2.__name__ == 'hello2'
|
||||
|
||||
del hello2
|
||||
|
||||
# Now the same with allowOverride=True
|
||||
|
||||
@func_set_timeout(2, allowOverride=True)
|
||||
def hello3():
|
||||
pass
|
||||
|
||||
assert hello3.__name__ == 'hello3'
|
||||
|
||||
del hello3
|
||||
|
||||
@func_set_timeout(getTimeoutFunc, allowOverride=True)
|
||||
def hello4():
|
||||
pass
|
||||
|
||||
assert hello4.__name__ == 'hello4'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
159
tests/FuncTimeoutTests/test_StoppableThread.py
Executable file
159
tests/FuncTimeoutTests/test_StoppableThread.py
Executable file
@ -0,0 +1,159 @@
|
||||
#!/usr/bin/env python
|
||||
# vim: set ts=4 sw=4 expandtab :
|
||||
|
||||
'''
|
||||
Copyright (c) 2017 Tim Savannah All Rights Reserved.
|
||||
|
||||
Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as
|
||||
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE
|
||||
'''
|
||||
|
||||
import copy
|
||||
import gc
|
||||
import sys
|
||||
import time
|
||||
import threading
|
||||
import subprocess
|
||||
|
||||
from func_timeout import func_timeout, FunctionTimedOut, func_set_timeout
|
||||
|
||||
from TestUtils import ARG_NO_DEFAULT, getSleepLambda, getSleepLambdaWithArgs, compareTimes
|
||||
|
||||
class TestBasic(object):
|
||||
'''
|
||||
TestBasic - Perform tests using the basic func_timeout function
|
||||
'''
|
||||
|
||||
def test_funcTimeout(self):
|
||||
sleepFunction = getSleepLambda(1.25)
|
||||
|
||||
expectedResult = 5 + 13
|
||||
|
||||
startTime = time.time()
|
||||
result = sleepFunction(5, 13)
|
||||
endTime = time.time()
|
||||
|
||||
assert result == expectedResult , 'Did not get return from sleepFunction'
|
||||
|
||||
try:
|
||||
result = func_timeout(1.5, sleepFunction, args=(5, 13))
|
||||
except FunctionTimedOut as te:
|
||||
raise AssertionError('Got unexpected timeout at 1.5 second timeout for 1.25 second function: %s' %(str(te),))
|
||||
|
||||
assert result == expectedResult , 'Got wrong return from func_timeout.\nGot: %s\nExpected: %s\n' %(repr(result), repr(expectedResult))
|
||||
|
||||
gotException = False
|
||||
try:
|
||||
result = func_timeout(1, sleepFunction, args=(5, 13))
|
||||
except FunctionTimedOut as te:
|
||||
gotException = True
|
||||
|
||||
assert gotException , 'Expected to get FunctionTimedOut exception for 1.25 sec function at 1s timeout'
|
||||
|
||||
try:
|
||||
result = func_timeout(1.5, sleepFunction, args=(5,), kwargs={ 'b' : 13})
|
||||
except FunctionTimedOut as te:
|
||||
raise AssertionError('Got unexpected timeout at 1.5 second timeout for 1.25 second function: %s' %(str(te), ))
|
||||
except Exception as e:
|
||||
raise AssertionError('Got unknown exception mixing args and kwargs: < %s > %s' %(e.__class__.__name__, str(e)))
|
||||
|
||||
assert result == expectedResult , 'Got wrong result when mixing args and kwargs'
|
||||
|
||||
def test_retry(self):
|
||||
sleepFunction = getSleepLambda(.5)
|
||||
|
||||
expectedResult = 5 + 19
|
||||
|
||||
gotException = False
|
||||
functionTimedOut = None
|
||||
|
||||
startTime = time.time()
|
||||
try:
|
||||
result = func_timeout(.3, sleepFunction, args=(5, 19))
|
||||
except FunctionTimedOut as fte:
|
||||
functionTimedOut = fte
|
||||
gotException = True
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception'
|
||||
assert compareTimes(endTime, startTime, .3, 3, .15, None) == 0 , 'Expected to wait .3 seconds. Was: %f - %f = %f' %(endTime, startTime, round(endTime - startTime, 3))
|
||||
|
||||
gotException = False
|
||||
startTime = time.time()
|
||||
try:
|
||||
result = functionTimedOut.retry()
|
||||
except FunctionTimedOut:
|
||||
gotException = True
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception on retry.'
|
||||
assert compareTimes(endTime, startTime, .3, 3, .1, None) == 0 , 'Expected retry with no arguments to use same timeout of .3'
|
||||
|
||||
gotException = False
|
||||
startTime = time.time()
|
||||
try:
|
||||
result = functionTimedOut.retry(None)
|
||||
except FunctionTimedOut:
|
||||
gotException = True
|
||||
endTime = time.time()
|
||||
|
||||
assert not gotException , 'Did NOT to get exception with no timeout'
|
||||
assert compareTimes(endTime, startTime, .5, 3, .1, None) == 0 , 'Expected retry with None as timeout to last full length of function'
|
||||
|
||||
gotException = False
|
||||
startTime = time.time()
|
||||
try:
|
||||
result = functionTimedOut.retry(.4)
|
||||
except FunctionTimedOut:
|
||||
gotException = True
|
||||
finally:
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to time out after .5 seconds when providing .5'
|
||||
assert compareTimes(endTime, startTime, .5, 3, .1, None) == 0 , 'Expected providing .5 would allow timeout of up to .5 seconds'
|
||||
|
||||
threadsCleanedUp = False
|
||||
|
||||
for i in range(5):
|
||||
time.sleep(1)
|
||||
gc.collect()
|
||||
|
||||
if threading.active_count() == 1:
|
||||
threadsCleanedUp = True
|
||||
break
|
||||
|
||||
|
||||
assert threadsCleanedUp , 'Expected other threads to get cleaned up after gc collection'
|
||||
|
||||
def test_exception(self):
|
||||
sleepFunction = getSleepLambda(.5)
|
||||
|
||||
expectedResult = 5 + 19
|
||||
|
||||
gotException = False
|
||||
functionTimedOut = None
|
||||
|
||||
startTime = time.time()
|
||||
try:
|
||||
result = func_timeout(.3, sleepFunction, args=(5, 19))
|
||||
except FunctionTimedOut as fte:
|
||||
functionTimedOut = fte
|
||||
gotException = True
|
||||
endTime = time.time()
|
||||
|
||||
assert gotException , 'Expected to get exception'
|
||||
|
||||
assert 'timed out after ' in functionTimedOut.msg , 'Expected message to be constructed. Got: %s' %(repr(functionTimedOut.msg), )
|
||||
assert round(functionTimedOut.timedOutAfter, 1) == .3 , 'Expected timedOutAfter to equal timeout ( .3 ). Got: %s' %(str(round(functionTimedOut.timedOutAfter, 1)), )
|
||||
assert functionTimedOut.timedOutFunction == sleepFunction , 'Expected timedOutFunction to equal sleepFunction'
|
||||
assert functionTimedOut.timedOutArgs == (5, 19) , 'Expected args to equal (5, 19)'
|
||||
assert functionTimedOut.timedOutKwargs == {} , 'Expected timedOutKwargs to equal {}'
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(subprocess.Popen('GoodTests.py -n1 "%s" %s' %(sys.argv[0], ' '.join(['"%s"' %(arg.replace('"', '\\"'), ) for arg in sys.argv[1:]]) ), shell=True).wait())
|
||||
|
||||
# vim: set ts=4 sw=4 expandtab :
|
||||
@ -35,7 +35,7 @@ class TestBasicSleepWithArgs(object):
|
||||
raise AssertionError('Expected to have 1 default arg and 2 standard. Tried 3 args')
|
||||
endTime = time.time()
|
||||
|
||||
assert compareTimes(endTime, startTime, 2, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambdaWithArgs(2) to take 2 seconds.'
|
||||
assert compareTimes(endTime, startTime, 2, 2, deltaFixed=.15, deltaPct=None) == 0 , 'Expected getSleepLambdaWithArgs(2) to take 2 seconds.'
|
||||
|
||||
try:
|
||||
sleepLambda(4, 7, 12)
|
||||
@ -54,7 +54,7 @@ class TestBasicSleepWithArgs(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert result == expectedResult , 'Got wrong result'
|
||||
assert compareTimes(endTime, startTime, 1.75, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambdaWithArgs(1.75) to take 1.75 seconds.'
|
||||
assert compareTimes(endTime, startTime, 1.75, 2, deltaFixed=.15, deltaPct=None) == 0 , 'Expected getSleepLambdaWithArgs(1.75) to take 1.75 seconds.'
|
||||
|
||||
expectedResult = 5 + 13
|
||||
|
||||
@ -63,7 +63,7 @@ class TestBasicSleepWithArgs(object):
|
||||
endTime = time.time()
|
||||
|
||||
assert result == expectedResult , 'Did not get return from sleepFunction'
|
||||
assert compareTimes(endTime, startTime, 1.75, 2, deltaFixed=.1, deltaPct=None) == 0 , 'Expected getSleepLambda(1.75) to take 1.75 seconds.'
|
||||
assert compareTimes(endTime, startTime, 1.75, 2, deltaFixed=.15, deltaPct=None) == 0 , 'Expected getSleepLambda(1.75) to take 1.75 seconds.'
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(subprocess.Popen('GoodTests.py -n1 "%s" %s' %(sys.argv[0], ' '.join(['"%s"' %(arg.replace('"', '\\"'), ) for arg in sys.argv[1:]]) ), shell=True).wait())
|
||||
|
||||
@ -12,7 +12,62 @@
|
||||
# NOTE: Since version 1.2.3, you can also import this (like from a graphical application) and call the "main()" function.
|
||||
# All of the following globals are the defaults, but can be overridden when calling main() (params have the same name as the globals).
|
||||
|
||||
import imp
|
||||
# Assign a local function, "find_mod" to the interface to search
|
||||
# PYTHONPATH for importable module
|
||||
try:
|
||||
# imp.find_module has been deprecated as of python 3.7, so
|
||||
# prefer some alternate/newer interfaces first.
|
||||
import importlib
|
||||
|
||||
try:
|
||||
# If we have the newest and therefore least-deprecated
|
||||
# way, use it.
|
||||
_findModSpec = importlib.util.find_spec
|
||||
def find_mod(modName):
|
||||
'''
|
||||
find_mod - Find a module by name.
|
||||
|
||||
Similar to import #modName but only finds importable module,
|
||||
does not actually import.
|
||||
|
||||
@raises ImportError on failure
|
||||
'''
|
||||
modSpec = _findModSpec(modName)
|
||||
if not modSpec:
|
||||
# imp.find_module raises import error if cannot find,
|
||||
# but find_spec just returns None
|
||||
# So simulate the ImportError for common interface
|
||||
raise ImportError('No module named %s' %(modName, ))
|
||||
|
||||
return modSpec
|
||||
|
||||
except AttributeError:
|
||||
# We have importlib, but don't have importlib.util.find_spec
|
||||
|
||||
# We could use importlib.import_module which is present in
|
||||
# python 2.7, but that changes behaviour by actually
|
||||
# importing (and thus additionally checking syntax/other).
|
||||
#
|
||||
# So just fall back to the old imp.find_module in this case
|
||||
|
||||
try:
|
||||
# Clean up namespace
|
||||
del importlib
|
||||
except:
|
||||
pass
|
||||
# Fall back to imp.find_module implementation below
|
||||
raise ImportError('importlib but no importlib.util')
|
||||
#find_mod = lambda modName : importlib.import_module(modName)
|
||||
|
||||
except:
|
||||
# importlib is not present or has an unknown/dated interface,
|
||||
# so fallback to the deprecated but oldest form
|
||||
import imp
|
||||
|
||||
# Use a lambda to ensure only one arg is passed as that is
|
||||
# our standard interface
|
||||
find_mod = lambda modName : imp.find_module(modName)
|
||||
|
||||
import os
|
||||
|
||||
import subprocess
|
||||
@ -32,8 +87,8 @@ ALLOW_SITE_INSTALL = False
|
||||
# This is the test directory that should contain all your tests. This should be a directory in your "tests" folder
|
||||
MY_TEST_DIRECTORY = 'FuncTimeoutTests'
|
||||
|
||||
__version__ = '2.1.1'
|
||||
__version_tuple__ = (2, 1, 1)
|
||||
__version__ = '3.0.5'
|
||||
__version_tuple__ = (3, 0, 5)
|
||||
|
||||
def findGoodTests():
|
||||
'''
|
||||
@ -65,8 +120,83 @@ def findGoodTests():
|
||||
"success" : success
|
||||
}
|
||||
|
||||
def findExecutable(execName):
|
||||
'''
|
||||
findExecutable - Search PATH for an executable
|
||||
|
||||
@return <dict> {
|
||||
'path' <str> -> Path to executable (if found, see "success")
|
||||
'success' <bool> -> True/False if we successfully found requested executable
|
||||
}
|
||||
'''
|
||||
|
||||
pathSplit = os.environ['PATH'].split(':')
|
||||
if '.' not in pathSplit:
|
||||
pathSplit = ['.'] + pathSplit
|
||||
os.environ['PATH'] = ':'.join(pathSplit)
|
||||
|
||||
result = ''
|
||||
success = False
|
||||
for path in pathSplit:
|
||||
if path.endswith(os.sep):
|
||||
path = path[:-1]
|
||||
guess = path + os.sep + execName
|
||||
if os.path.exists(guess):
|
||||
success = True
|
||||
result = guess
|
||||
break
|
||||
|
||||
return {
|
||||
"path" : result,
|
||||
"success" : success
|
||||
}
|
||||
|
||||
def findGoodTests():
|
||||
return findExecutable('GoodTests.py')
|
||||
|
||||
|
||||
def try_pip_install():
|
||||
pipe = subprocess.Popen('pip install GoodTests', shell=True)
|
||||
'''
|
||||
try to pip install GoodTests.py
|
||||
|
||||
First, try via pip module.
|
||||
|
||||
If that fails, try to locate pip by dirname(current python executable) + os.sep + pip
|
||||
If that does not exist, scan PATH for pip
|
||||
|
||||
If found a valid pip executable, invoke it to install GoodTests
|
||||
otherwise, fail.
|
||||
'''
|
||||
|
||||
didImport = False
|
||||
try:
|
||||
import pip
|
||||
didImport = True
|
||||
except:
|
||||
pass
|
||||
|
||||
if didImport is True:
|
||||
print ( "Found pip as module=pip")
|
||||
res = pip.main(['install', 'GoodTests'])
|
||||
if res == 0:
|
||||
return 0
|
||||
sys.stderr.write('Failed to install GoodTests via pip module. Falling back to pip executable...\n\n')
|
||||
|
||||
pipPath = os.path.dirname(sys.executable) + os.sep + 'pip'
|
||||
print ( 'Searching for pip at "%s"' %(pipPath, ) )
|
||||
if not os.path.exists(pipPath):
|
||||
print ( '"%s" does not exist. Scanning PATH to locate a usable pip executable' %(pipPath, ))
|
||||
pipPath = None
|
||||
searchResults = findExecutable('pip')
|
||||
if not searchResults['success']:
|
||||
sys.stderr.write('Failed to find a usable pip executable in PATH.\n')
|
||||
return 1 # Failed to locate a usable pip
|
||||
|
||||
pipPath = searchResults['path']
|
||||
|
||||
print ( 'Found pip executable at "%s"' %(pipPath, ) )
|
||||
print ( "Executing: %s %s 'install' 'GoodTests'" %(sys.executable, pipPath) )
|
||||
pipe = subprocess.Popen([sys.executable, pipPath, 'install', 'GoodTests'], shell=False, env=os.environ)
|
||||
res = pipe.wait()
|
||||
|
||||
return res
|
||||
@ -195,7 +325,7 @@ def main(thisDir=None, additionalArgs=[], MY_PACKAGE_MODULE=None, ALLOW_SITE_INS
|
||||
elif dirName == '':
|
||||
inCurrentDir = False
|
||||
try:
|
||||
imp.find_module(MY_PACKAGE_MODULE)
|
||||
find_mod(MY_PACKAGE_MODULE)
|
||||
inCurrentDir = True
|
||||
except ImportError:
|
||||
# COMPAT WITH PREVIOUS runTests.py: Try plain module in parent directory
|
||||
@ -203,7 +333,7 @@ def main(thisDir=None, additionalArgs=[], MY_PACKAGE_MODULE=None, ALLOW_SITE_INS
|
||||
oldSysPath = sys.path[:]
|
||||
sys.path = [os.path.realpath(os.getcwd() + os.sep + '..' + os.sep)]
|
||||
try:
|
||||
imp.find_module(MY_PACKAGE_MODULE)
|
||||
find_mod(MY_PACKAGE_MODULE)
|
||||
foundIt = True
|
||||
sys.path = oldSysPath
|
||||
except ImportError as e:
|
||||
@ -234,8 +364,15 @@ def main(thisDir=None, additionalArgs=[], MY_PACKAGE_MODULE=None, ALLOW_SITE_INS
|
||||
if baseName.endswith(('.py', '.pyc', '.pyo')):
|
||||
MY_PACKAGE_MODULE = baseName[ : baseName.rindex('.')]
|
||||
|
||||
if e.name != MY_PACKAGE_MODULE:
|
||||
sys.stderr.write('Error while importing %s: %s\n Likely this is another dependency that needs to be installed\nPerhaps run "pip install %s" or install the providing package.\n\n' %(e.name, str(e), e.name))
|
||||
try:
|
||||
eName = e.name
|
||||
except AttributeError as noNameE:
|
||||
# Some platforms python2 does not have this attribute
|
||||
# so pull it from the message
|
||||
eName = e.message.split()[-1]
|
||||
|
||||
if eName != MY_PACKAGE_MODULE:
|
||||
sys.stderr.write('Error while importing %s: %s\n Likely this is another dependency that needs to be installed\nPerhaps run "pip install %s" or install the providing package.\n\n' %(eName, str(e), eName))
|
||||
return 1
|
||||
sys.stderr.write('Could not import %s. Either install it or otherwise add to PYTHONPATH\n%s\n' %(MY_PACKAGE_MODULE, str(e)))
|
||||
return 1
|
||||
@ -253,8 +390,7 @@ def main(thisDir=None, additionalArgs=[], MY_PACKAGE_MODULE=None, ALLOW_SITE_INS
|
||||
|
||||
|
||||
didTerminate = False
|
||||
|
||||
pipe = subprocess.Popen([goodTestsInfo['path']] + additionalArgs + [MY_TEST_DIRECTORY], env=os.environ, shell=False)
|
||||
pipe = subprocess.Popen([sys.executable, goodTestsInfo['path']] + additionalArgs + [MY_TEST_DIRECTORY], env=os.environ, shell=False)
|
||||
while True:
|
||||
try:
|
||||
pipe.wait()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user