-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[FLINK-38559][python] Support unordered mode of async function in Python DataStream API #27145
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,77 @@ | ||
| ################################################################################ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| ################################################################################ | ||
| import inspect | ||
|
|
||
| from pyflink.common import Time, TypeInformation | ||
| from pyflink.datastream.data_stream import DataStream, _get_one_input_stream_operator | ||
| from pyflink.datastream.functions import AsyncFunctionDescriptor, AsyncFunction | ||
| from pyflink.java_gateway import get_gateway | ||
| from pyflink.util.java_utils import get_j_env_configuration | ||
|
|
||
|
|
||
| class AsyncDataStream(object): | ||
| """ | ||
| A helper class to apply :class:`~AsyncFunction` to a data stream. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def unordered_wait( | ||
| data_stream: DataStream, | ||
| async_function: AsyncFunction, | ||
| timeout: Time, | ||
| capacity: int = 100, | ||
| output_type: TypeInformation = None) -> 'DataStream': | ||
| """ | ||
| Adds an async function to the data stream. The order of output stream records may be | ||
| reordered. | ||
|
|
||
| :param data_stream: The input data stream. | ||
| :param async_function: The async function. | ||
| :param timeout: The timeout for the asynchronous operation to complete. | ||
| :param capacity: The max number of async i/o operation that can be triggered. | ||
| :param output_type: The output data type. | ||
| :return: The transformed DataStream. | ||
| """ | ||
| AsyncDataStream._validate(data_stream, async_function) | ||
|
|
||
| from pyflink.fn_execution import flink_fn_execution_pb2 | ||
| j_python_data_stream_function_operator, j_output_type_info = \ | ||
| _get_one_input_stream_operator( | ||
| data_stream, | ||
| AsyncFunctionDescriptor( | ||
| async_function, timeout, capacity, | ||
| AsyncFunctionDescriptor.OutputMode.UNORDERED), | ||
| flink_fn_execution_pb2.UserDefinedDataStreamFunction.PROCESS, # type: ignore | ||
| output_type) | ||
| return DataStream(data_stream._j_data_stream.transform( | ||
| "async wait operator", | ||
| j_output_type_info, | ||
| j_python_data_stream_function_operator)) | ||
|
|
||
| @staticmethod | ||
| def _validate(data_stream: DataStream, async_function: AsyncFunction) -> None: | ||
| if not inspect.iscoroutinefunction(async_function.async_invoke): | ||
| raise Exception("Method 'async_invoke' of class '%s' should be declared as 'async def'." | ||
| % type(async_function)) | ||
|
|
||
| gateway = get_gateway() | ||
| j_conf = get_j_env_configuration(data_stream._j_data_stream.getExecutionEnvironment()) | ||
| python_execution_mode = ( | ||
| j_conf.get(gateway.jvm.org.apache.flink.python.PythonOptions.PYTHON_EXECUTION_MODE)) | ||
| if python_execution_mode == 'thread': | ||
| raise Exception("AsyncFunction is still not supported for 'thread' mode.") | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think
type(function)will return a verbose result, maybe this change gives actual nameUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type(function)returns the full path of the class name andtype(async_function).__name__returns just the class name. My intention here is to print the full path which I think will be more helpful.