Skip to content
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

Cleanup in concurrency module and typing #1173

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cognite/client/_api/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ def _download_file_to_path(self, download_link: str, path: Path, chunk_size: int
) as r:
r = cast("Response", r)
with path.open("wb") as f:
for chunk in r.iter_content(chunk_size=chunk_size): # type: ignore
for chunk in r.iter_content(chunk_size=chunk_size):
if chunk: # filter out keep-alive new chunks
f.write(chunk)

Expand Down
47 changes: 15 additions & 32 deletions cognite/client/utils/_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,13 @@ def result(self) -> T_Result:
...


class SyncFuture(TaskFuture):
def __init__(self, fn: Callable[..., T_Result], *args: Any, **kwargs: Any):
self.__fn = fn
self.__args = args
self.__kwargs = kwargs

def result(self) -> T_Result: # type: ignore
return self.__fn(*self.__args, **self.__kwargs)


class MainThreadExecutor(TaskExecutor):
"""
In order to support executing sdk methods in the browser using pyodide (a port of CPython to webassembly),
we need to be able to turn off the usage of threading. So we have this executor which implements the Executor
protocol but just executes everything serially in the main thread.
"""

def submit(self, fn: Callable, *args: Any, **kwargs: Any) -> SyncFuture:
return SyncFuture(fn, *args, **kwargs)


class ExtendedSyncFuture(TaskFuture):
class SyncFuture(TaskFuture[T_Result]):
def __init__(self, fn: Callable[..., T_Result], *args: Any, **kwargs: Any):
self._task = functools.partial(fn, *args, **kwargs)
self._result: Optional[T_Result] = None
self._is_cancelled = False

def result(self) -> T_Result: # type: ignore
def result(self) -> T_Result:
if self._is_cancelled:
raise CancelledError
if self._result is None:
Expand All @@ -182,23 +161,27 @@ def cancel(self) -> None:
self._is_cancelled = True


class ExtendedMainThreadExecutor(TaskExecutor):
__doc__ = MainThreadExecutor.__doc__
class MainThreadExecutor(TaskExecutor):
"""
In order to support executing sdk methods in the browser using pyodide (a port of CPython to webassembly),
we need to be able to turn off the usage of threading. So we have this executor which implements the Executor
protocol but just executes everything serially in the main thread.
"""

def submit(self, fn: Callable[..., T_Result], *args: Any, **kwargs: Any) -> ExtendedSyncFuture:
def submit(self, fn: Callable[..., T_Result], *args: Any, **kwargs: Any) -> SyncFuture:
if "priority" in inspect.signature(fn).parameters:
raise TypeError(f"Given function {fn} cannot accept reserved parameter name `priority`")
kwargs.pop("priority", None)
return ExtendedSyncFuture(fn, *args, **kwargs)
return SyncFuture(fn, *args, **kwargs)

def shutdown(self, wait: bool = False) -> None:
return None

@staticmethod
def as_completed(it: Iterable[ExtendedSyncFuture]) -> Iterator[ExtendedSyncFuture]:
def as_completed(it: Iterable[SyncFuture]) -> Iterator[SyncFuture]:
return iter(copy(it))

def __enter__(self) -> ExtendedMainThreadExecutor:
def __enter__(self) -> MainThreadExecutor:
return self

def __exit__(
Expand Down Expand Up @@ -227,10 +210,10 @@ def get_executor(max_workers: int) -> TaskExecutor:

if ConcurrencySettings.executor_type == "threadpool":
try:
executor: TaskExecutor = _THREAD_POOL_EXECUTOR_SINGLETON # type: ignore
executor: TaskExecutor = _THREAD_POOL_EXECUTOR_SINGLETON
except NameError:
# TPE has not been initialized
executor = _THREAD_POOL_EXECUTOR_SINGLETON = ThreadPoolExecutor(max_workers) # type: ignore
executor = _THREAD_POOL_EXECUTOR_SINGLETON = ThreadPoolExecutor(max_workers)
elif ConcurrencySettings.executor_type == "mainthread":
executor = _MAIN_THREAD_EXECUTOR_SINGLETON
else:
Expand All @@ -245,7 +228,7 @@ def get_priority_executor(max_workers: int) -> PriorityThreadPoolExecutor:
if ConcurrencySettings.priority_executor_type == "priority_threadpool":
return PriorityThreadPoolExecutor(max_workers)
elif ConcurrencySettings.priority_executor_type == "mainthread":
return ExtendedMainThreadExecutor() # type: ignore [return-value]
return MainThreadExecutor() # type: ignore [return-value]

raise RuntimeError(f"Invalid priority-queue executor type '{ConcurrencySettings.priority_executor_type}'")

Expand Down