Пример #1
0
def detect_running_asynclib() -> Optional[str]:
    if 'trio' in sys.modules:
        from trio.hazmat import current_trio_token
        try:
            current_trio_token()
        except RuntimeError:
            pass
        else:
            return 'trio'

    if 'curio' in sys.modules:
        from curio.meta import curio_running
        if curio_running():
            return 'curio'

    if 'asyncio' in sys.modules:
        from ._backends.asyncio import get_running_loop
        try:
            get_running_loop()
        except RuntimeError:
            pass
        else:
            return 'asyncio'

    return None
Пример #2
0
def _detect_running_asynclib() -> Optional[str]:
    # This function can be removed once https://github.com/python-trio/sniffio/pull/5 has been
    # merged
    try:
        return sniffio.current_async_library()
    except sniffio.AsyncLibraryNotFoundError:
        if 'curio' in sys.modules:
            from curio.meta import curio_running
            if curio_running():
                return 'curio'

        return None
Пример #3
0
def current_async_library() -> str:
    """Detect which async library is currently running.

    The following libraries are currently supported:

    ================   ===========  ============================
    Library             Requires     Magic string
    ================   ===========  ============================
    **Trio**            Trio v0.6+   ``"trio"``
    **Curio**           -            ``"curio"``
    **asyncio**                      ``"asyncio"``
    **Trio-asyncio**    v0.8.2+     ``"trio"`` or ``"asyncio"``,
                                    depending on current mode
    ================   ===========  ============================

    Returns:
      A string like ``"trio"``.

    Raises:
      AsyncLibraryNotFoundError: if called from synchronous context,
        or if the current async library was not recognized.

    Examples:

        .. code-block:: python3

           from sniffio import current_async_library

           async def generic_sleep(seconds):
               library = current_async_library()
               if library == "trio":
                   import trio
                   await trio.sleep(seconds)
               elif library == "asyncio":
                   import asyncio
                   await asyncio.sleep(seconds)
               # ... and so on ...
               else:
                   raise RuntimeError(f"Unsupported library {library!r}")

    """
    value = current_async_library_cvar.get()
    if value is not None:
        return value

    # Sniff for curio (for now)
    if 'curio' in sys.modules:
        from curio.meta import curio_running
        if curio_running():
            return 'curio'

    # Need to sniff for asyncio
    if "asyncio" in sys.modules:
        import asyncio
        try:
            current_task = asyncio.current_task  # type: ignore[attr-defined]
        except AttributeError:
            current_task = asyncio.Task.current_task  # type: ignore[attr-defined]
        try:
            if current_task() is not None:
                if (3, 7) <= sys.version_info:
                    # asyncio has contextvars support, and we're in a task, so
                    # we can safely cache the sniffed value
                    current_async_library_cvar.set("asyncio")
                return "asyncio"
        except RuntimeError:
            pass
    raise AsyncLibraryNotFoundError(
        "unknown async library, or not in async context")
Пример #4
0
 def wrapped(*args, **kwargs):
     if _from_coroutine() or curio_running():
         return asyncfunc(*args, **kwargs)
     else:
         return get_kernel().run(asyncfunc(*args, **kwargs))
Пример #5
0
 def wrapped(*args, **kwargs):
     if _from_coroutine() or curio_running():
         return asyncfunc(*args, **kwargs)
     else:
         return _gen(*args, **kwargs)