|
| 1 | +import threading |
| 2 | +import logging |
| 3 | + |
| 4 | + |
| 5 | +logger = logging.getLogger(__name__) |
| 6 | + |
| 7 | + |
| 8 | +class FFProxy: |
| 9 | + """Proxies a target object and runs its methods in the background. |
| 10 | +
|
| 11 | + All method calls to this object are forwarded to the target and executed |
| 12 | + in a background thread. Method calls return immediately. Exceptions from |
| 13 | + the target method are turned into warnings. At most one method from the |
| 14 | + target object may be executed in the background; if a new call is |
| 15 | + submitted while the previous one is still executing, a warning is printed |
| 16 | + and the new call is dropped. |
| 17 | +
|
| 18 | + This feature is typically used to wrap slow and non-critical RPCs in |
| 19 | + experiments. |
| 20 | + """ |
| 21 | + def __init__(self, target): |
| 22 | + self.target = target |
| 23 | + self._thread = None |
| 24 | + |
| 25 | + def ff_join(self): |
| 26 | + """Waits until any background method finishes its execution.""" |
| 27 | + if self._thread is not None: |
| 28 | + self._thread.join() |
| 29 | + |
| 30 | + def __getattr__(self, k): |
| 31 | + def run_in_thread(*args, **kwargs): |
| 32 | + if self._thread is not None and self._thread.is_alive(): |
| 33 | + logger.warning("skipping fire-and-forget call to %r.%s as " |
| 34 | + "previous call did not complete", |
| 35 | + self.target, k) |
| 36 | + return |
| 37 | + def thread_body(): |
| 38 | + try: |
| 39 | + getattr(self.target, k)(*args, **kwargs) |
| 40 | + except: |
| 41 | + logger.warning("fire-and-forget call to %r.%s raised an " |
| 42 | + "exception:", self.target, k, exc_info=True) |
| 43 | + self._thread = threading.Thread(target=thread_body) |
| 44 | + self._thread.start() |
| 45 | + return run_in_thread |
0 commit comments