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

interoperability with asyncio (part 3): refactor with aiohttp #176

Merged
merged 35 commits into from
Jun 20, 2024

Conversation

achille-roussel
Copy link
Contributor

Follow up to #175, this PR refactors the dispatch-py internals to use asyncio across the board, and only convert between blocking and async modes at interface boundaries.

Overall, this PR does not modify the public API much, but mostly enables to key changes:

  • it creates a path forward to enable applications to write tests for dispatch functions
  • it defines a model for waiting on dispatched function results
    (these were previously discussed in part 2)

Waiting on dispatched function results

I have added a temporary disaptch.sdk.v1.DispatchService/Wait endpoint -- which is very much experimental at this point and only used in tests -- to allow retrieving the result of dispatched function calls. Along with this change, the SDK is now detecting whether a dispatch function is called (and awaited) from an external client or within the a dispatch execution context. When the former is true, the /Wait endpoint is used to wait for the result of the function call.

I want to emphasize that it's still very much experimental, I want to make sure the model works well end-to-end before committing to an API in https://github.com/dispatchrun/dispatch-proto.

Testing dispatch functions

This PR does not yet deliver on this front, but it makes great progress by adding a generic dispatch.test.TestCase test suite that is used to exercise testing of all integrations with FastAPI, Flask, aiohttp, http.server, etc...

One blocker to finishing this work is the inability to pickle cell objects, which are implicitly generated by Python when local variables are referenced from multiple scopes, and happens in constructs like the following:

@aiotest
async def test_call_nested_function_with_result(self):
    @self.dispatch.function
    def echo(name: str) -> str:
        return name

    @self.dispatch.function
    async def echo2(name: str) -> str:
        return await echo(name)

    self.assertEqual(await echo2("hello"), "hello")

(echo exists in the local scopes of test_call_nested_function_with_result and echo2)

These blockers prevent finishing the rewrite of tests in test_fastapi.py::TestCoroutine into dispatch.test.TestCase, which also prevents removing the grpcio and httpx dependencies, and deleting all the gRPC codegen.

@achille-roussel achille-roussel added enhancement New feature or request breaking change Breaking change that will be disruptive to programs that depended on them labels Jun 12, 2024
@achille-roussel achille-roussel self-assigned this Jun 12, 2024
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
…hon 3.8

Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
…s are skipped on 3.8/3.9

Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
support both asyncio and blocking modes with different abstractions
Copy link
Contributor

@chriso chriso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 👏🏻

@achille-roussel achille-roussel merged commit bb6ec79 into main Jun 20, 2024
7 checks passed
@achille-roussel achille-roussel deleted the aiohttp-2 branch June 20, 2024 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Breaking change that will be disruptive to programs that depended on them enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants