Function Utilities and Decorators
Module: ppc_robot_lib.utils.func
- per_thread_result(func)[source]
Caches the function result in the thread local storage. Can be used as a decorator:
>>> import time, threading >>> from concurrent.futures.thread import ThreadPoolExecutor >>> @per_thread_result >>> def get_init_data(): ... print('get_init_data called') ... return 42 >>> def usage(*args): ... for n in range(3): ... print(f'{threading.get_ident()}: {get_init_data()}') ... time.sleep(0.1) >>> pool = ThreadPoolExecutor(max_workers=2) >>> for _ in range(2): ... pool.submit(usage) get_init_data called 11032: 42 get_init_data called 2092: 42 11032: 42 2092: 42 11032: 42 2092: 42
In the example above, each thread calls the function multiple times. The function is called only on the first time and the value is saved. On subsequent calls from the same thread, the underlying function will not be called and the previously saved result will be used.