dispatchrun / dispatch-py

Python package to develop applications with Dispatch.
https://pypi.org/project/dispatch-py/
Apache License 2.0
56 stars 3 forks source link

interoperability with asyncio (part 2): integration with aiohttp #175

Closed achille-roussel closed 5 months ago

achille-roussel commented 5 months ago

Based on #122, this PR adds a new integration with the aiohttp framework, to enable dispatch applications that run on aiohttp servers, or use aiohttp clients to integrate with the asyncio facilities end-to-end.

This change is also a prerequisite to more modifications I want to bring to the SDK, that in my mind are other prerequisites for a sync mode where we can wait on dispatch calls in retrieve their results.

Thoughts on next steps

What I would like to get to is an API that matches asyncio more closely, for example:

import dispatch

@dispatch.function
async def job():
    ...

dispatch.run(job())

This would cause a call to job to be dispatched to the scheduler, then waited on by the dispatch.run event loop, with the result returned.

This could also be composed with other asyncio operations:

import dispatch

@dispatch.function
async def job():
    ...

async def main():
    # dispatch.sdk.v1.DispatchServer/Dispatch because not in the context
    # of a dispatch function, then poll for the result
    return await job() 

dispatch.run(main())

This would provide a unified model for both durable and volatile contexts, which would pave the road to simple testing constructs, examples, and integrations with RPC handlers etc...

For blocking world that doesn't use asyncio, the .dispatch() method can still be used, but we'd modify its return value to a form of future-like value where the result can be retrieved in a blocking fashion.

Let me know what you think!