Code Monkey home page Code Monkey logo

Comments (3)

vxgmichel avatar vxgmichel commented on August 12, 2024

Hi @tiptop96,

The perserve operator doesn't not preserve the content of the stream but prevents the stream from being closed when exiting the streaming context. In order to preserve the content, you can add the data to a list and later chain this list with the preserved iterator:

async def main():
    async with aiohttp.ClientSession() as sesh:
        async with sesh.get("https://jsonplaceholder.typicode.com/todos/1") as resp:
            async with stream.preserve(resp.content).stream() as streamer:
                stack = []
                async for line in streamer:
                    stack.append(line)
                    await asyncio.sleep(1)
                    # Some criteria in the middle of content
                    if b"title" in line:
                        break
            preserved_content = stream.iterate(stack) + resp.content
            async with preserved_content.stream() as streamer:
                async for line in streamer:
                    print(line)
                    await asyncio.sleep(1)

You can also factorize this logic into a dedicated operator:

@operator(pipable=True)
async def preserve_content(source, items):
    for item in items:
        yield item
    async with stream.preserve(source).stream() as streamer:
        async for item in streamer:
            items.append(item)
            yield item


async def main():
    async with aiohttp.ClientSession() as sesh:
        async with sesh.get("https://jsonplaceholder.typicode.com/todos/1") as resp:
            items = []
            preserved = preserve_content(resp.content, items)
            async with preserved.stream() as streamer:
                async for line in streamer:
                    await asyncio.sleep(1)
                    # Some criteria in the middle of content
                    if b"title" in line:
                        break
            async with preserved.stream() as streamer:
                async for line in streamer:
                    print(line)
                    await asyncio.sleep(1)

As a side note, I also receive a warning as follows: [...]

The stream operators are classes that are created dynamically, so I'm not surprised that linters might struggle to detect their interface. There might be a way to help them figure out the right info, I'll try to look into it later.

Hope it helps :)

from aiostream.

tiptop96 avatar tiptop96 commented on August 12, 2024

Ahh my bad for missunderstanding the operator! But this is awesome, thanks so much for the thorough response and the awesome lib! 🔥

from aiostream.

vxgmichel avatar vxgmichel commented on August 12, 2024

No problem :)

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.