This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add docstrings, remove old classes/functions, fix future wrapper
- Use Numpy docstrings - Remove completable_future wrapper since Python deprecated getting the event loop if there is no current one, and made the future wrapper not use a coroutine so it does not raise a warning if it is not awaited - Remove classes/functions that were either replaced by another one or deprecated.
- Loading branch information
1 parent
5b65cf7
commit e629ae5
Showing
30 changed files
with
3,194 additions
and
1,014 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
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 |
---|---|---|
@@ -1,42 +1,30 @@ | ||
from ._jpype_type_conversions import PythonBiFunction | ||
from typing import Awaitable, TypeVar, TYPE_CHECKING | ||
from asyncio import Future, get_event_loop, CancelledError | ||
|
||
if TYPE_CHECKING: | ||
from java.util.concurrent import (Future as JavaFuture, | ||
CompletableFuture as JavaCompletableFuture) | ||
from java.util.concurrent import Future as JavaFuture | ||
|
||
|
||
Result = TypeVar('Result') | ||
|
||
|
||
def wrap_future(future: 'JavaFuture[Result]') -> Awaitable[Result]: | ||
async def get_result() -> Result: | ||
nonlocal future | ||
return future.get() | ||
class JavaFutureAwaitable(Awaitable[Result]): | ||
_future: 'JavaFuture[Result]' | ||
|
||
return get_result() | ||
def __init__(self, future: 'JavaFuture[Result]') -> None: | ||
self._future = future | ||
|
||
def __await__(self) -> Result: | ||
return self | ||
|
||
def wrap_completable_future(future: 'JavaCompletableFuture[Result]') -> Future[Result]: | ||
loop = get_event_loop() | ||
out = loop.create_future() | ||
def __iter__(self): | ||
return self | ||
|
||
def result_handler(result, error): | ||
nonlocal out | ||
if error is not None: | ||
out.set_exception(error) | ||
else: | ||
out.set_result(result) | ||
def __next__(self): | ||
raise StopIteration(self._future.get()) | ||
|
||
def cancel_handler(python_future: Future): | ||
nonlocal future | ||
if isinstance(python_future.exception(), CancelledError): | ||
future.cancel(True) | ||
|
||
future.handle(PythonBiFunction(result_handler)) | ||
out.add_done_callback(cancel_handler) | ||
return out | ||
def wrap_future(future: 'JavaFuture[Result]') -> Awaitable[Result]: | ||
return JavaFutureAwaitable(future) | ||
|
||
|
||
__all__ = ['wrap_future', 'wrap_completable_future'] | ||
__all__ = ['wrap_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
Oops, something went wrong.