This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rough POC that we can do async without touching function graph
@Stefan this is what I meant Basically we pass the coroutine the entire way through. When a function depends on other data, we create a coroutine that awaits everything. Finally, we do a gather at the end. I *think* this is optimal but will need to dig in a bit/look into things. Otherwise, nifty POC with *very* rough edges.
- Loading branch information
1 parent
9ecf72c
commit 081db80
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import asyncio | ||
import aiohttp | ||
import fastapi | ||
|
||
from hamilton import driver, ad_hoc_utils | ||
|
||
|
||
async def request_raw(request: fastapi.Request) -> dict: | ||
return await request.json() | ||
|
||
|
||
def foo(request_raw: fastapi.Request) -> str: | ||
return request_raw.get('foo', 'far') | ||
|
||
|
||
def bar(request_raw: fastapi.Request) -> str: | ||
return request_raw.get('bar', 'baz') | ||
|
||
|
||
async def computation1(foo: str, some_data: dict) -> bool: | ||
await asyncio.sleep(1) | ||
return False | ||
|
||
|
||
async def some_data() -> dict: | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get('http://httpbin.org/get') as resp: | ||
return await resp.json() | ||
|
||
|
||
async def computation2(bar: str) -> bool: | ||
await asyncio.sleep(1) | ||
return True | ||
|
||
|
||
async def pipeline(computation1: bool, computation2: bool) -> dict: | ||
await asyncio.sleep(1) | ||
return {'computation1': computation1, 'computation2': computation2} | ||
|
||
|
||
# Some logic similar to computation1 | ||
|
||
|
||
app = fastapi.FastAPI() | ||
|
||
|
||
@app.post('/execute') | ||
async def call( | ||
request: fastapi.Request | ||
) -> dict: | ||
"""Handler for pipeline call""" | ||
dr = driver.AsyncDriver( | ||
{}, | ||
ad_hoc_utils.create_temporary_module( | ||
pipeline, | ||
computation1, | ||
foo, | ||
bar, | ||
some_data, | ||
computation2)) | ||
input_data = {'request_raw': request} | ||
return await dr.raw_execute(['pipeline'], inputs=input_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters