-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Synchronous and asynchronous compatibility within Prefect #15008
Labels
enhancement
An improvement of an existing feature
Comments
This was referenced Aug 20, 2024
This was referenced Sep 9, 2024
1 task
This was referenced Nov 19, 2024
This was referenced Dec 2, 2024
This was referenced Dec 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the current behavior
Currently, Prefect supports many user interfaces that we attempt to maintain as compatible with both synchronous execution as well as asynchronous execution. A popular example of this is
Block.load
. The way this is achieved is through an internal utility that "magically" - and more critically: quietly - attempts to decide on the user's behalf whether the user will be able to await the coroutine or not.This works well in certain situations such as methods / utilities called within Prefect tasks and flows - this is because we explicitly track whether that task or flow is synchronous or asynchronous.
However, this can turn out very poorly in situations where something about the runtime environment changes between local development and production. A few examples to make the point:
sync_compatible
decorator returning coroutines unexpectedly when running a flow #14625Prefect 3.0 exposes a new keyword argument on these special methods / functions for explicitly setting the behavior a user wants: to continue with the block loading example, users can now specify
Block.load(**kwargs, _sync=True)
for enforcing synchronous execution andawait Block.load(**kwargs, _sync=False)
for enforcing asynchronous execution. This is useful as an escape hatch, but under the hood it still engages with complex event loop and threading logic that risks performance degradation and more difficult to inspect failure modes.Clearly there is room for improvement here along a few dimensions:
_sync=True/False
behavior in a more first class way for discoverabilityDescribe the proposed behavior
To achieve the goals outlined above, I propose first expanding the
sync_compatible
interface in two ways:sync_compatible(sync_version=sync_method)
that explicitly provides an alternative synchronous implementation to dispatch between; note this does mean expanding the codebase into both synchronous and asynchronous implementations. The decorator and our current "magic" will allow us to take a strangler fig approach and incrementally add these duplicate implementations as we developasync_compatible(async_version=async_method)
(the need for this will become clear below when I discuss naming conventions)For any decorated function / method that has a dual implementation, Prefect can begin issuing a warning whenever the user relies on behavior for which that function / method is dispatching to another implementation. For example:
To make sure this part is not glossed over: this will ultimately result in Prefect maintaining two implementations for a large class of user interfaces (primarily those that interact with the Prefect client / API).
Naming Convention
To achieve this, we will rely on the following naming conventions:
Async
; e.g.,PrefectClient
vs.AsyncPrefectClient
a
prefix: e.g.,Block.load
vs.Block.aload
a
prefix: e.g.,run_deployment
vsarun_deployment
There is one edge case with this, which is
.wait
methods on Prefect futures; for this we will make the corresponding class awaitable for async waits.Additional context
Feel free to comment and discuss.
The text was updated successfully, but these errors were encountered: