concurrency.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # util/concurrency.py
  2. # Copyright (C) 2005-2022 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. from . import compat
  8. have_greenlet = False
  9. greenlet_error = None
  10. if compat.py3k:
  11. try:
  12. import greenlet # noqa F401
  13. except ImportError as e:
  14. greenlet_error = str(e)
  15. else:
  16. have_greenlet = True
  17. from ._concurrency_py3k import await_only
  18. from ._concurrency_py3k import await_fallback
  19. from ._concurrency_py3k import greenlet_spawn
  20. from ._concurrency_py3k import is_exit_exception
  21. from ._concurrency_py3k import AsyncAdaptedLock
  22. from ._concurrency_py3k import _util_async_run # noqa F401
  23. from ._concurrency_py3k import (
  24. _util_async_run_coroutine_function,
  25. ) # noqa F401, E501
  26. from ._concurrency_py3k import asyncio # noqa F401
  27. # does not need greennlet, just Python 3
  28. from ._compat_py3k import asynccontextmanager # noqa F401
  29. if not have_greenlet:
  30. asyncio = None # noqa F811
  31. def _not_implemented():
  32. # this conditional is to prevent pylance from considering
  33. # greenlet_spawn() etc as "no return" and dimming out code below it
  34. if have_greenlet:
  35. return None
  36. if not compat.py3k:
  37. raise ValueError("Cannot use this function in py2.")
  38. else:
  39. raise ValueError(
  40. "the greenlet library is required to use this function."
  41. " %s" % greenlet_error
  42. if greenlet_error
  43. else ""
  44. )
  45. def is_exit_exception(e): # noqa F811
  46. return not isinstance(e, Exception)
  47. def await_only(thing): # noqa F811
  48. _not_implemented()
  49. def await_fallback(thing): # noqa F81
  50. return thing
  51. def greenlet_spawn(fn, *args, **kw): # noqa F81
  52. _not_implemented()
  53. def AsyncAdaptedLock(*args, **kw): # noqa F81
  54. _not_implemented()
  55. def _util_async_run(fn, *arg, **kw): # noqa F81
  56. return fn(*arg, **kw)
  57. def _util_async_run_coroutine_function(fn, *arg, **kw): # noqa F81
  58. _not_implemented()