Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying amap usage #18

Closed
florimondmanca opened this issue May 17, 2020 · 3 comments
Closed

Simplifying amap usage #18

florimondmanca opened this issue May 17, 2020 · 3 comments

Comments

@florimondmanca
Copy link
Owner

Reading through the docs it came to my mind that the async with usage of amap doesn’t “look necessary” from a UX perspective.

When using it we always have to do two things:

  • Enter a context with async with (looks like an implementation detail)
  • Iterate over results with async for

As a user really only care about the second operation.

So what if we moved async with inside the implementation of amap, so that users can just do...

results = [result async for result in amap(process, items)]
@graingert
Copy link
Contributor

graingert commented Jul 4, 2021

you can do this but you'd need to pass the TaskGroup into the amap, and wrap the returned AsyncGenerator with aclosing:

async with anyio.create_task_group() as tg:
    async with aclosing(amap(process, items, tg)) as aiter:
        results = [result async for result in aiter]

@florimondmanca
Copy link
Owner Author

@graingert Well indeed, good point. The actual correct comparison would then be this…

(Current situation)

async with aiometer.amap(process, items) as aiter:
    results = [result async for result in aiter]

Compared to this…

(Correct implementation of proposed idea)

async with anyio.create_task_group() as tg:
    async with aclosing(aiometer.amap(process, items, tg)) as aiter:
        results = [result async for result in aiter]

Which instantaneously seems like we don't want amap to change, and the async with (which ensures structured concurrency constraints) + async for is just fine (users would have to add it not to shoot themselves in the foot; and it's likely that an implementation would require the correct implementation anyway, which means there's not even a semi-viable "just async for" alternative).

I think we can close this then?

@graingert
Copy link
Contributor

I think we can aclose this then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants