dispatchrun / dispatch-py

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

Optionally run a coroutine when using `dispatch.run_forever` #180

Closed chriso closed 2 months ago

chriso commented 2 months ago

The behavior of dispatch.run changed recently. Previously it would start a Dispatch endpoint server and serve forever. It optionally accepted a function to run once the server had been started, allowing users to dispatch an initial function call. Now it requires a coroutine as input, and sets up the Dispatch endpoint server only for the lifetime of the coroutine. Once the coroutine returns, dispatch.run returns its result after tearing down the endpoint server.

Users may need the ability to debug what happened with their function calls after they returned. However, if the Dispatch endpoint exits, the CLI will exit too.

There's a dispatch.run_forever variant that's closer to the original behavior of dispatch.run. It serves the endpoint forever, however it doesn't accept a coroutine as input nor a function to run when the server has been started.

This PR updates dispatch.run_forever to accept the same input as dispatch.run. If a coroutine is provided, it's run when the server has been started. Unlike dispatch.run, it doesn't return the result of the coroutine, instead keeping the server (and CLI, if applicable) running.