-
Notifications
You must be signed in to change notification settings - Fork 15.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
community[minor]: Add async methods to CassandraLoader (#20609)
Co-authored-by: Eugene Yurtsev <[email protected]>
- Loading branch information
Showing
3 changed files
with
54 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from typing import TYPE_CHECKING, Any, Callable | ||
|
||
if TYPE_CHECKING: | ||
from cassandra.cluster import ResponseFuture | ||
|
||
|
||
async def wrapped_response_future( | ||
func: Callable[..., ResponseFuture], *args: Any, **kwargs: Any | ||
) -> Any: | ||
loop = asyncio.get_event_loop() | ||
asyncio_future = loop.create_future() | ||
response_future = func(*args, **kwargs) | ||
|
||
def success_handler(_: Any) -> None: | ||
loop.call_soon_threadsafe(asyncio_future.set_result, response_future.result()) | ||
|
||
def error_handler(exc: BaseException) -> None: | ||
loop.call_soon_threadsafe(asyncio_future.set_exception, exc) | ||
|
||
response_future.add_callbacks(success_handler, error_handler) | ||
return await asyncio_future |
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