X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=thread_utils.py;h=c4a293794a99cb1f479da105a499cb2ce93b564e;hb=02302bbd9363facb59c4df2c1f4013087702cfa6;hp=5903782ae031773894ae1452671d39e7d90705e0;hpb=1e858172519e9339e4720b8bf9b39b6d9801e305;p=python_utils.git diff --git a/thread_utils.py b/thread_utils.py index 5903782..c4a2937 100644 --- a/thread_utils.py +++ b/thread_utils.py @@ -17,10 +17,12 @@ logger = logging.getLogger(__name__) def current_thread_id() -> str: - """Returns a string composed of the parent process' id, the current - process' id and the current thread identifier. The former two are - numbers (pids) whereas the latter is a thread id passed during thread - creation time. + """ + Returns: + a string composed of the parent process' id, the current + process' id and the current thread identifier. The former two are + numbers (pids) whereas the latter is a thread id passed during thread + creation time. >>> ret = current_thread_id() >>> (ppid, pid, tid) = ret.split('/') @@ -37,8 +39,10 @@ def current_thread_id() -> str: def is_current_thread_main_thread() -> bool: - """Returns True is the current (calling) thread is the process' main - thread and False otherwise. + """ + Returns: + True is the current (calling) thread is the process' main + thread and False otherwise. >>> is_current_thread_main_thread() True @@ -68,10 +72,6 @@ def background_thread( ) -> Callable[..., Tuple[threading.Thread, threading.Event]]: """A function decorator to create a background thread. - *** Please note: the decorated function must take an shutdown *** - *** event as an input parameter and should periodically check *** - *** it and stop if the event is set. *** - Usage:: @background_thread @@ -89,10 +89,12 @@ def background_thread( event.set() thread.join() - Note: in addition to any other arguments the function has, it must - take a stop_event as the last unnamed argument which it should - periodically check. If the event is set, it means the thread has - been requested to terminate ASAP. + .. warning:: + + In addition to any other arguments the function has, it must + take a stop_event as the last unnamed argument which it should + periodically check. If the event is set, it means the thread has + been requested to terminate ASAP. """ def wrapper(funct: Callable): @@ -123,14 +125,23 @@ def periodically_invoke( stop_after: Optional[int], ): """ - Periodically invoke a decorated function. Stop after N invocations - (or, if stop_after is None, call forever). Delay period_sec between - invocations. + Periodically invoke the decorated function. + + Args: + period_sec: the delay period in seconds between invocations + stop_after: total number of invocations to make or, if None, + call forever - Returns a Thread object and an Event that, when signaled, will stop - the invocations. Note that it is possible to be invoked one time - after the Event is set. This event can be used to stop infinite - invocation style or finite invocation style decorations.:: + Returns: + a :class:Thread object and an :class:Event that, when + signaled, will stop the invocations. + + .. note:: + It is possible to be invoked one time after the :class:Event + is set. This event can be used to stop infinite + invocation style or finite invocation style decorations. + + Usage:: @periodically_invoke(period_sec=0.5, stop_after=None) def there(name: str, age: int) -> None: @@ -139,7 +150,6 @@ def periodically_invoke( @periodically_invoke(period_sec=1.0, stop_after=3) def hello(name: str) -> None: print(f"Hello, {name}") - """ def decorator_repeat(func):