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

@pipe_output, @pipe_input and @mutate for async functons #1193

Closed
elijahbenizzy opened this issue Oct 17, 2024 · 2 comments
Closed

@pipe_output, @pipe_input and @mutate for async functons #1193

elijahbenizzy opened this issue Oct 17, 2024 · 2 comments

Comments

@elijahbenizzy
Copy link
Collaborator

Is your feature request related to a problem? Please describe.
These don't work:

  1. Async functions decorated with @pipe_output/@pipe_input/@mutate (have not tested all configurations of these)
  2. Async transformations covered by step

Describe the solution you'd like
These should work. See the way we do other decorators. E.G.

async def async_function(**kwargs):

Describe alternatives you've considered
We can use a workaround in some cases. E.G. an identity node. See the first comment for it.

Additional context
https://hamilton-opensource.slack.com/archives/C03M33QB4M8/p1729156532663429

@elijahbenizzy
Copy link
Collaborator Author

Workaround for @pipe_output

import asyncio

import pandas as pd

from hamilton import async_driver
from hamilton.function_modifiers import pipe_output, step, hamilton_exclude


async def data_input() -> pd.DataFrame:
    await asyncio.sleep(0.0001)
    return pd.DataFrame({
        "a": [1, 2, 3],
        "b": [4, 5, 6]
    })


def _groupby_a(d: pd.DataFrame) -> pd.DataFrame:
    return d.groupby("a").sum().reset_index()


def _groupby_b(d: pd.DataFrame) -> pd.DataFrame:
    return d.groupby("b").sum().reset_index()


@pipe_output(
    step(_groupby_a).when(groupby="a"),
    step(_groupby_b).when_not(groupby="a"),
)
def data(data_input: pd.DataFrame) -> pd.DataFrame:
    return data_input


@hamilton_exclude
async def main():
    import __main__
    dr = (await async_driver.Builder().with_modules(__main__).with_config(dict(groupby="b")).build())
    results = await dr.execute(["data"])
    print(results)


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

@zilto
Copy link
Collaborator

zilto commented Jan 18, 2025

Closed with #1223

@zilto zilto closed this as completed Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants