Skip to content

Need a way to distinguish between nest-asyncio and nest-asyncio2 patch #1

@dagardner-nv

Description

@dagardner-nv

Currently I find myself taking care to ensure that our thirds-party dependencies which will apply the nest-asyncio patch on import, are lazily imported, allowing my own library to apply the nest_asyncio2 patch instead.

I find myself writing code like this:

    # Make sure someone in our stack didn't already apply the older nest-asyncio patch
    assert not hasattr(asyncio, "_nest_patched")
    import nest_asyncio2
    nest_asyncio2.apply()

The main issue here is that I have no way to determine if nest_asyncio or nest_asyncio2 was applied.

Ideas:

  1. In addition to the _nest_patched attribute, add a _nest2_patched attribute. If both are set then we know the nest_asyncio2 patch was applied, if only _nest_patched was set then nest_asyncio was applied.

  2. Alternately since nest-asyncio only checks if the attribute exists, the value could be changed to 2 allowing for code like:

patched = getattr(asyncio, "_nest_patched", None)
if patched is None:
    # apply patch
elif patched == 2:
    # nest-asyncio2 is applied, we're good
elif patched is True:
    logger.warn("Attempted to apply nest_asyncio2, but nest_asyncio was already applied")
  1. Add an optional argument to raise an exception on apply if nest-asyncio was applied first something like: def apply(loop=None, *, run_close_loop: bool = False, error_on_v1_patched: bool = False):

Edit by maintainer: related PR: NVIDIA/NeMo-Agent-Toolkit#1190

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions