Code Monkey home page Code Monkey logo

Comments (2)

vxgmichel avatar vxgmichel commented on August 12, 2024 6

Thanks for creating this issue, maybe it's something that should be better documented.

Consider the following asynchronous generator:

async def count_to_ten():
    try:
        for i in range(10):
            yield i
            await asyncio.sleep(1)
    finally:
        print('cleaning up')

Now, say you're only interested in the first three values:

async def main():
    xs = stream.enumerate(agen())
    async for i, x in xs:
        if i == 3:
            break
        print(x)
    print('done!')

And you run the main coroutine using the standard boiler-plate code:

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()

Let's check the output:

[...]: UserWarning: AsyncIteratorContext is iterated outside of its context
  "AsyncIteratorContext is iterated outside of its context")
0
1
2
done!

And that's it, no clean up! According to PEP 525, that's because you're actually supposed to add the following line before closing the loop:

loop.run_until_complete(loop.shutdown_asyncgens())

Notice however that 'cleaning up' will appear after 'done!' which is usually not what you want. By using the stream method, the clean up gets to run before leaving the corresponding context:

async def main():
    xs = stream.enumerate(agen())
    async with xs.stream() as streamer:
        async for i, x in streamer:
            if i == 3:
                break
            print(x)
    print('done!')

Note that if you use await instead of the asynchronous iteration, the streaming context is not necessary:

async def main():
    xs = stream.iterate(agen())
    print(await xs[:3])
    print('done!')

Now if you can think of a more intuitive interface, I'd be happy to look into it!

from aiostream.

andersea avatar andersea commented on August 12, 2024 3

Looks like I stepped into a minefield here.

dabeaz/curio#176

https://vorpus.org/blog/some-thoughts-on-asynchronous-api-design-in-a-post-asyncawait-world/

This is actually broken in python IMHO.. If DB can't come up with any good solution, I am not even going to try. Closing.

from aiostream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.