-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
GH-37093: [Python] Add async Flight client with GetFlightInfo #36986
Merged
pitrou
merged 7 commits into
apache:main
from
lidavidm:flight-async-py-client-getflightinfo
Aug 23, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
adab985
GH-37093: [FlightRPC][Python] Add async GetFlightInfo
lidavidm 9b286d5
Bind to future instead of dealing with callbacks
pitrou 8571b9f
Add async.h
lidavidm 4154e4f
Try to avoid type-unsafe `void*`
pitrou 5c7d665
Simplify AsyncioCall using a asyncio.Future
pitrou 58a938a
Forward async-support error message to Python, for informative test s…
pitrou 07193d4
Mark override
pitrou 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 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 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 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 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 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 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 | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -837,6 +837,12 @@ cdef class FlightInfo(_Weakrefable): | |||||||||||||||||||||||||||||||||||||
cdef: | ||||||||||||||||||||||||||||||||||||||
unique_ptr[CFlightInfo] info | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@staticmethod | ||||||||||||||||||||||||||||||||||||||
cdef wrap(CFlightInfo c_info): | ||||||||||||||||||||||||||||||||||||||
cdef FlightInfo obj = FlightInfo.__new__(FlightInfo) | ||||||||||||||||||||||||||||||||||||||
obj.info.reset(new CFlightInfo(move(c_info))) | ||||||||||||||||||||||||||||||||||||||
return obj | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def __init__(self, Schema schema, FlightDescriptor descriptor, endpoints, | ||||||||||||||||||||||||||||||||||||||
total_records, total_bytes): | ||||||||||||||||||||||||||||||||||||||
"""Create a FlightInfo object from a schema, descriptor, and endpoints. | ||||||||||||||||||||||||||||||||||||||
|
@@ -1219,6 +1225,66 @@ cdef class FlightMetadataWriter(_Weakrefable): | |||||||||||||||||||||||||||||||||||||
check_flight_status(self.writer.get().WriteMetadata(deref(buf))) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
class AsyncioCall: | ||||||||||||||||||||||||||||||||||||||
"""State for an async RPC using asyncio.""" | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def __init__(self) -> None: | ||||||||||||||||||||||||||||||||||||||
import asyncio | ||||||||||||||||||||||||||||||||||||||
self._future = asyncio.get_running_loop().create_future() | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def as_awaitable(self) -> object: | ||||||||||||||||||||||||||||||||||||||
return self._future | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def wakeup(self, result_or_exception) -> None: | ||||||||||||||||||||||||||||||||||||||
# Mark the Future done from within its loop (asyncio | ||||||||||||||||||||||||||||||||||||||
# objects are generally not thread-safe) | ||||||||||||||||||||||||||||||||||||||
loop = self._future.get_loop() | ||||||||||||||||||||||||||||||||||||||
if isinstance(result_or_exception, BaseException): | ||||||||||||||||||||||||||||||||||||||
loop.call_soon_threadsafe( | ||||||||||||||||||||||||||||||||||||||
self._future.set_exception, result_or_exception) | ||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||
loop.call_soon_threadsafe( | ||||||||||||||||||||||||||||||||||||||
self._future.set_result, result_or_exception) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
cdef class AsyncioFlightClient: | ||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||
A FlightClient with an asyncio-based async interface. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
This interface is EXPERIMENTAL. | ||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
cdef: | ||||||||||||||||||||||||||||||||||||||
FlightClient _client | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def __init__(self, FlightClient client) -> None: | ||||||||||||||||||||||||||||||||||||||
self._client = client | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
async def get_flight_info( | ||||||||||||||||||||||||||||||||||||||
self, | ||||||||||||||||||||||||||||||||||||||
descriptor: FlightDescriptor, | ||||||||||||||||||||||||||||||||||||||
*, | ||||||||||||||||||||||||||||||||||||||
options: FlightCallOptions = None, | ||||||||||||||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||||||||||||||
call = AsyncioCall() | ||||||||||||||||||||||||||||||||||||||
self._get_flight_info(call, descriptor, options) | ||||||||||||||||||||||||||||||||||||||
return await call.as_awaitable() | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+1263
to
+1271
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could just be:
Suggested change
but I've kept the explicit |
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
cdef _get_flight_info(self, call, descriptor, options): | ||||||||||||||||||||||||||||||||||||||
cdef: | ||||||||||||||||||||||||||||||||||||||
CFlightCallOptions* c_options = \ | ||||||||||||||||||||||||||||||||||||||
FlightCallOptions.unwrap(options) | ||||||||||||||||||||||||||||||||||||||
CFlightDescriptor c_descriptor = \ | ||||||||||||||||||||||||||||||||||||||
FlightDescriptor.unwrap(descriptor) | ||||||||||||||||||||||||||||||||||||||
CFuture[CFlightInfo] c_future | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
with nogil: | ||||||||||||||||||||||||||||||||||||||
c_future = self._client.client.get().GetFlightInfoAsync( | ||||||||||||||||||||||||||||||||||||||
deref(c_options), c_descriptor) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
BindFuture(move(c_future), call.wakeup, FlightInfo.wrap) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
cdef class FlightClient(_Weakrefable): | ||||||||||||||||||||||||||||||||||||||
"""A client to a Flight service. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
@@ -1320,6 +1386,14 @@ cdef class FlightClient(_Weakrefable): | |||||||||||||||||||||||||||||||||||||
check_flight_status(CFlightClient.Connect(c_location, c_options | ||||||||||||||||||||||||||||||||||||||
).Value(&self.client)) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@property | ||||||||||||||||||||||||||||||||||||||
def supports_async(self): | ||||||||||||||||||||||||||||||||||||||
return self.client.get().supports_async() | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def as_async(self) -> None: | ||||||||||||||||||||||||||||||||||||||
check_status(self.client.get().CheckAsyncSupport()) | ||||||||||||||||||||||||||||||||||||||
return AsyncioFlightClient(self) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def wait_for_available(self, timeout=5): | ||||||||||||||||||||||||||||||||||||||
"""Block until the server can be contacted. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
This file contains 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 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 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 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 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.
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.
@lidavidm Does this API look ok?
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.
LGTM, though I would be happy to replace
supports_async
entirely with this.