Skip to content

[Feature]: Lifecycle management for asyncstdlib-created iterators #165

@hongyiweiwu01

Description

@hongyiweiwu01

Describe the feature

This might be more of a request for usage guidance than one for new features. If I have an AsyncGenerator agen() and I pass it into one of the asyncstdlib iterators, say enumerate, my understanding is enumerate now ties the cleanup of agen to its own cleanup. But that doesn't mean enumerate(agen()) is deterministically cleaned-up -- for instance, in the following scenario:

async for i, item in asyncstdlib.enumerate(agen()):
    if i > 2:
        break

So would the correct solution in this case be to wrap the entire code block in async with asyncstdlib.scoped_iter(asyncstdlib.enumerate(agen())) as safe_enum_it and iterate on safe_enum_it instead?

A potentially related situation is with how asyncstdlib.list owns the internal iterable. If I just do a = await asyncstdlib.list(agen()), and for some reason (maybe an external exception) the execution is interrupted and agen() doesn't finish iterating, would it be correct to assume that the cleanup isn't guaranteed to happen, and we should ideally do something like

async with asyncstdlib.scoped_iter(agen()) as safe_agen_it:
   a = await asyncstdlib.list(safe_agen_it)

instead?

I really love this library, so would really appreciate your explanation on this. Thank you!

Request Assignment [Optional]

  • I already have a plan and want to submit an implementation.

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