concurrency.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # util/concurrency.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: allow-untyped-defs, allow-untyped-calls
  8. from __future__ import annotations
  9. import asyncio # noqa
  10. import typing
  11. have_greenlet = False
  12. greenlet_error = None
  13. try:
  14. import greenlet # type: ignore # noqa: F401
  15. except ImportError as e:
  16. greenlet_error = str(e)
  17. pass
  18. else:
  19. have_greenlet = True
  20. from ._concurrency_py3k import await_only as await_only
  21. from ._concurrency_py3k import await_fallback as await_fallback
  22. from ._concurrency_py3k import in_greenlet as in_greenlet
  23. from ._concurrency_py3k import greenlet_spawn as greenlet_spawn
  24. from ._concurrency_py3k import is_exit_exception as is_exit_exception
  25. from ._concurrency_py3k import AsyncAdaptedLock as AsyncAdaptedLock
  26. from ._concurrency_py3k import (
  27. _util_async_run as _util_async_run,
  28. ) # noqa: F401
  29. from ._concurrency_py3k import (
  30. _util_async_run_coroutine_function as _util_async_run_coroutine_function, # noqa: F401, E501
  31. )
  32. if not typing.TYPE_CHECKING and not have_greenlet:
  33. def _not_implemented():
  34. # this conditional is to prevent pylance from considering
  35. # greenlet_spawn() etc as "no return" and dimming out code below it
  36. if have_greenlet:
  37. return None
  38. raise ValueError(
  39. "the greenlet library is required to use this function."
  40. " %s" % greenlet_error
  41. if greenlet_error
  42. else ""
  43. )
  44. def is_exit_exception(e): # noqa: F811
  45. return not isinstance(e, Exception)
  46. def await_only(thing): # type: ignore # noqa: F811
  47. _not_implemented()
  48. def await_fallback(thing): # type: ignore # noqa: F811
  49. return thing
  50. def in_greenlet(): # type: ignore # noqa: F811
  51. _not_implemented()
  52. def greenlet_spawn(fn, *args, **kw): # type: ignore # noqa: F811
  53. _not_implemented()
  54. def AsyncAdaptedLock(*args, **kw): # type: ignore # noqa: F811
  55. _not_implemented()
  56. def _util_async_run(fn, *arg, **kw): # type: ignore # noqa: F811
  57. return fn(*arg, **kw)
  58. def _util_async_run_coroutine_function(fn, *arg, **kw): # type: ignore # noqa: F811,E501
  59. _not_implemented()