GithubHelp home page GithubHelp logo

Comments (2)

vxgmichel avatar vxgmichel commented on July 28, 2024

Hi @Joker-vD, thanks for creating an issue!

I can think of three different ways to implement the feature you're looking for:

import random
import asyncio
from aiostream import stream, pipe


async def do_something(x):
    print(f'Doing something for {x:.2f} s...')
    await asyncio.sleep(x)
    print(f'Done something for {x:.2f} s...')


async def produce_coros():
    for _ in range(5):
        await asyncio.sleep(random.random())
        yield do_something(random.random())


async def _await(arg):
    return await arg


async def main():
    # Version 1
    await stream.combine.amap(produce_coros(), lambda x: x)
    print()
    # Version 2
    await stream.map(produce_coros(), _await)
    print()
    # Version 3
    await stream.flatmap(produce_coros(), stream.just)
    print()


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

The last one is probably the simplest, although flatmap is meant to be an advanced operator. Maybe the library is missing an extra helper, but I can't really figure it out. Maybe something like asyncio.as_completed:

from aiostream import stream, pipe, operator

@operator(pipable=True)
def as_completed(source, task_limit=None):
    return stream.flatmap.raw(source, stream.just.raw, task_limit=task_limit)

async def main():
    results = await (as_completed(produce_coros()) | pipe.list())

from aiostream.

vxgmichel avatar vxgmichel commented on July 28, 2024

Now that async_ and await_ have been added to aiostream namespace (see PR #21), the simplest solution is:

from aiostream import stream, await_

async def main():
    await stream.map(produce_coros(), await_)

PR #21 will become available with version 0.3.1.

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.