Code Monkey home page Code Monkey logo

Comments (2)

vxgmichel avatar vxgmichel commented on September 16, 2024

Hi @t-mart, thanks for your report :)

I tried to reproduce your problem, and I did get a proper exception out of it. For instance, consider the following code:

import asyncio
from aiostream import operator


@operator(pipable=True)
async def some_pipable_operator(source, arg):
    await asyncio.sleep(1)


async def main():
    some_pipable_operator("test")

if __name__ == "__main__":
    asyncio.run(main())

Just like in your snippet, regenerate is incorrectly instantiated:

  • the first argument is not an async iterable
  • the second argument is missing

In this case it's the first error that is reported first:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    asyncio.run(main())
  File "/home/vinmic/miniconda/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/home/vinmic/miniconda/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "test.py", line 12, in main
    some_pipable_operator("test")
  File "/home/vinmic/aiostream/aiostream/core.py", line 284, in init
    assert_async_iterable(args[0])
  File "/home/vinmic/aiostream/aiostream/aiter_utils.py", line 61, in assert_async_iterable
    f"{type(obj).__name__!r} object is not async iterable")
TypeError: 'str' object is not async iterable

For your information, the proper usage of user-defined pipable operator is:

import asyncio
from aiostream import stream, operator


@operator(pipable=True)
async def some_pipable_operator(source, arg):
    assert await source == "a"
    yield arg


async def main():
    result = await (stream.just("a") | some_pipable_operator.pipe("b"))
    assert result == "b"

if __name__ == "__main__":
    asyncio.run(main())

However, it seems to me that you don't need a user-defined pipable operator. Instead, you may simply use stream.map:

from functools import partial
from aiostream import stream, pipe

async def watch(app: web.Application) -> None:
    await (
        stream.merge(*[awatch(p) for p in watch_paths]) 
        | pipe.map(partial(regenerate, app=app), task_limit=1)
    )

async def regenerate(
    changedset: Set[Tuple[Change, str]], app: web.Application
) -> None:
    [...]

Hope that helps :)

from aiostream.

t-mart avatar t-mart commented on September 16, 2024

Fantastic. Thank you for your help.

I wasn't getting the exception because, while I did create_task all this code, I wasn't awaiting it, and that maybe caused the exception to not bubble up properly. (It's amazing it was running at all. Maybe aiohttp application.on_startup was helping me in some way I don't understand.) EDIT: Or it was because I didn't debug=True the loop...no matter. It's not important. For many Python programmers, thinking asynchronously will take some getting-used-to.

Sorry to stuff up your issues with a non-issue. I'd definitely be in favor of a help channel of some kind, whether that be gitter or IRC or discord etc.

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.