2024-04-18 19:45:20 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
2024-04-20 02:09:58 +00:00
|
|
|
from enum import Enum
|
2024-04-18 19:45:20 +00:00
|
|
|
from typing import TYPE_CHECKING, Any, Callable
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2024-05-23 14:46:23 +00:00
|
|
|
from cassandra.cluster import ResponseFuture, Session
|
2024-04-18 19:45:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def wrapped_response_future(
|
|
|
|
func: Callable[..., ResponseFuture], *args: Any, **kwargs: Any
|
|
|
|
) -> Any:
|
2024-04-29 21:40:23 +00:00
|
|
|
"""Wrap a Cassandra response future in an asyncio future.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
func: The Cassandra function to call.
|
|
|
|
*args: The arguments to pass to the Cassandra function.
|
|
|
|
**kwargs: The keyword arguments to pass to the Cassandra function.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The result of the Cassandra function.
|
|
|
|
"""
|
2024-04-18 19:45:20 +00:00
|
|
|
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
|
2024-04-20 02:09:58 +00:00
|
|
|
|
|
|
|
|
2024-05-23 14:46:23 +00:00
|
|
|
async def aexecute_cql(session: Session, query: str, **kwargs: Any) -> Any:
|
|
|
|
return await wrapped_response_future(session.execute_async, query, **kwargs)
|
|
|
|
|
|
|
|
|
2024-04-20 02:09:58 +00:00
|
|
|
class SetupMode(Enum):
|
|
|
|
SYNC = 1
|
|
|
|
ASYNC = 2
|
|
|
|
OFF = 3
|