From f21d4b4b49a76685df5dc5f573e61665739c0ae4 Mon Sep 17 00:00:00 2001 From: Tim Savannah Date: Sun, 4 Jun 2017 04:43:56 -0400 Subject: [PATCH] Update READMEs to note StoppableThread --- README.md | 18 ++++++++++++++++++ README.rst | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/README.md b/README.md index 7e77c92..1a79e14 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,24 @@ Has a "retry" method which takes the following arguments: * None - Retry same args, same function, no timeout +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). + +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. + + 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: diff --git a/README.rst b/README.rst index 5515948..5540f32 100644 --- a/README.rst +++ b/README.rst @@ -70,6 +70,31 @@ Has a "retry" method which takes the following arguments: * None - Retry same args, same function, no timeout + +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). + +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. + + + 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: