utils.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from __future__ import annotations
  2. import asyncio
  3. import contextvars
  4. import sys
  5. import time
  6. from asyncio import get_running_loop
  7. from types import TracebackType
  8. from typing import Any, Awaitable, Callable, TypeVar, cast
  9. __all__ = [
  10. "run_in_executor_with_context",
  11. "call_soon_threadsafe",
  12. "get_traceback_from_context",
  13. ]
  14. _T = TypeVar("_T")
  15. def run_in_executor_with_context(
  16. func: Callable[..., _T],
  17. *args: Any,
  18. loop: asyncio.AbstractEventLoop | None = None,
  19. ) -> Awaitable[_T]:
  20. """
  21. Run a function in an executor, but make sure it uses the same contextvars.
  22. This is required so that the function will see the right application.
  23. See also: https://bugs.python.org/issue34014
  24. """
  25. loop = loop or get_running_loop()
  26. ctx: contextvars.Context = contextvars.copy_context()
  27. return loop.run_in_executor(None, ctx.run, func, *args)
  28. def call_soon_threadsafe(
  29. func: Callable[[], None],
  30. max_postpone_time: float | None = None,
  31. loop: asyncio.AbstractEventLoop | None = None,
  32. ) -> None:
  33. """
  34. Wrapper around asyncio's `call_soon_threadsafe`.
  35. This takes a `max_postpone_time` which can be used to tune the urgency of
  36. the method.
  37. Asyncio runs tasks in first-in-first-out. However, this is not what we
  38. want for the render function of the prompt_toolkit UI. Rendering is
  39. expensive, but since the UI is invalidated very often, in some situations
  40. we render the UI too often, so much that the rendering CPU usage slows down
  41. the rest of the processing of the application. (Pymux is an example where
  42. we have to balance the CPU time spend on rendering the UI, and parsing
  43. process output.)
  44. However, we want to set a deadline value, for when the rendering should
  45. happen. (The UI should stay responsive).
  46. """
  47. loop2 = loop or get_running_loop()
  48. # If no `max_postpone_time` has been given, schedule right now.
  49. if max_postpone_time is None:
  50. loop2.call_soon_threadsafe(func)
  51. return
  52. max_postpone_until = time.time() + max_postpone_time
  53. def schedule() -> None:
  54. # When there are no other tasks scheduled in the event loop. Run it
  55. # now.
  56. # Notice: uvloop doesn't have this _ready attribute. In that case,
  57. # always call immediately.
  58. if not getattr(loop2, "_ready", []):
  59. func()
  60. return
  61. # If the timeout expired, run this now.
  62. if time.time() > max_postpone_until:
  63. func()
  64. return
  65. # Schedule again for later.
  66. loop2.call_soon_threadsafe(schedule)
  67. loop2.call_soon_threadsafe(schedule)
  68. def get_traceback_from_context(context: dict[str, Any]) -> TracebackType | None:
  69. """
  70. Get the traceback object from the context.
  71. """
  72. exception = context.get("exception")
  73. if exception:
  74. if hasattr(exception, "__traceback__"):
  75. return cast(TracebackType, exception.__traceback__)
  76. else:
  77. # call_exception_handler() is usually called indirectly
  78. # from an except block. If it's not the case, the traceback
  79. # is undefined...
  80. return sys.exc_info()[2]
  81. return None