once_per_worker

once_per_worker(func: Callable[[], Any])[source]

Create a Delayed object for the return value of func, which runs at most once per process.

func must take no arguments.

Example

>>> import time
>>> import os
>>> import distributed
>>> from once_per_worker import once_per_worker
>>>
>>> def v_slow(x: int) -> int:
...     print(f"sleeping for {x} seconds on PID {os.getpid()}")
...     time.sleep(x)
...     return x
>>> slow_result = once_per_worker(lambda: v_slow(5))
>>> many_slows = sum([slow_result] * 10)
>>>
>>> client = distributed.Client(processes=True)
>>> # This should only take ~5sec, and you shouldn't see the same PID sleep more than once.
>>> many_slows.compute()