9/27/2023 0 Comments A fun time out![]() StoppableThread is a subclass of threading.Thread, which supports stopping the thread (supports both python2 and python3). The terminating of the timed-out function happens in the context of the thread and will not block main execution. Every 2 seconds until your function is terminated, it will continue to raise FunctionTimedOut. ![]() You should have your function catch the “FunctionTimedOut” exception and exit cleanly if possible. 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”. If there is a return or an exception raised, it will be returned/raised as normal. * None - Retry same args, same function, no timeout How it worksįunc_timeout will run the specified function in a thread with the specified arguments until it returns, raises an exception, or the timeout is exceeded. * Number argument - Retry same args, same function, provided timeout * No argument - Retry same args, same function, same timeout Has a “retry” method which takes the following arguments: For example, if you have a method that calculates data, you’ll want a higher timeout for 1 million records than 50 myFunction(self, arg1, arg2):Įxception raised if the function times out. It should return a number which will be used as the timeout for that paticular run. If a function/lambda is used, it will be passed the same arguments as the called function was passed. The “timeout” parameter can be either a number (for a fixed timeout), or a function/lambda. When provided, this will override the timeout used on this function. If “allowOverride” is present, an optional keyword argument is added to the wrapped function, ‘forceTimeout’. Takes two arguments, “timeout” and “allowOverride” This is a decorator you can use on functions to apply func_timeout. # Handle any exceptions that doit might raise here func_set_timeout Print ( “doit(‘arg1’, ‘arg2’) could not complete within 5 seconds and was terminated.\n”) 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:įrom func_timeout import func_timeout, FunctionTimedOutĭoitReturnValue = func_timeout(5, doit, args=(‘arg1’, ‘arg2’)) Raises any exceptions #func# would raise, returns what #func# would return (unless timeout is exceeded), in which case it raises timeout - Maximum number of seconds to run #func# before func - The function to args - Any ordered arguments to pass to the kwargs - Keyword arguments to pass to the - FunctionTimedOut if #timeout# is exceeded, otherwise anything #func# could raise will be - The return value that #func# gives ![]() 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.ĭef func_timeout(timeout, func, args=(), kwargs=None):įunc_timeout - Runs the given function for up to #timeout# seconds. Python module to support running any existing function with a given timeout.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |