web_read_coalescing.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """Coalesce concurrent synchronous HTTP reads before threadpool admission."""
  2. import asyncio
  3. import inspect
  4. from functools import partial, wraps
  5. from weakref import WeakKeyDictionary
  6. from typing import get_type_hints
  7. from anyio.to_thread import run_sync
  8. from hermes_constants import get_hermes_home
  9. def _freeze(value):
  10. if isinstance(value, dict):
  11. return (type(value), frozenset((_freeze(k), _freeze(v)) for k, v in value.items()))
  12. if isinstance(value, (list, tuple)):
  13. return (type(value), tuple(map(_freeze, value)))
  14. if isinstance(value, (set, frozenset)):
  15. return (type(value), frozenset(map(_freeze, value)))
  16. return (type(value), value)
  17. def coalesced_read(func, *, thread_runner=run_sync):
  18. """Return an async, in-flight-only wrapper around a synchronous read.
  19. One worker per decorated function/event loop; identical home + bound
  20. arguments share its result. Arguments must be hashable or built-in
  21. containers of hashable values and must not be mutated during the read.
  22. A custom async thread_runner must await the supplied zero-argument callable
  23. through a context-preserving threadpool, not abandon running work.
  24. """
  25. signature = inspect.signature(func)
  26. hints = get_type_hints(func, include_extras=True)
  27. signature = signature.replace(
  28. parameters=[p.replace(annotation=hints.get(p.name, p.annotation))
  29. for p in signature.parameters.values()],
  30. return_annotation=hints.get("return", signature.return_annotation),
  31. )
  32. states = WeakKeyDictionary()
  33. @wraps(func)
  34. async def wrapped(*args, **kwargs):
  35. loop = asyncio.get_running_loop()
  36. bound = signature.bind(*args, **kwargs)
  37. bound.apply_defaults()
  38. key = (str(get_hermes_home()), _freeze(bound.arguments))
  39. pending, admission = states.setdefault(loop, ({}, asyncio.Semaphore(1)))
  40. task = pending.get(key)
  41. if task is None:
  42. async def execute():
  43. try:
  44. async with admission:
  45. return await thread_runner(partial(func, *args, **kwargs))
  46. finally:
  47. pending.pop(key, None)
  48. if not pending:
  49. states.pop(loop, None)
  50. task = loop.create_task(execute())
  51. pending[key] = task
  52. # Observe failures even if every HTTP caller has disconnected.
  53. task.add_done_callback(lambda done: None if done.cancelled() else done.exception())
  54. # A disconnected caller must not release admission for a live worker.
  55. return await asyncio.shield(task)
  56. wrapped.__signature__ = signature
  57. wrapped.__annotations__ = hints
  58. return wrapped