-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve console chat launch speed by defer unnecessary package loading (
#396)
- Loading branch information
Showing
41 changed files
with
554 additions
and
213 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[pytest] | ||
markers = | ||
app_config: mark a test that requires the app config | ||
testpaths = | ||
tests |
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,15 +1,21 @@ | ||
from typing import Literal | ||
|
||
from taskweaver.ces.common import Manager | ||
from taskweaver.ces.environment import Environment, EnvMode | ||
from taskweaver.ces.manager.defer import DeferredManager | ||
from taskweaver.ces.manager.sub_proc import SubProcessManager | ||
|
||
|
||
def code_execution_service_factory( | ||
env_dir: str, | ||
kernel_mode: Literal["local", "container"] = "local", | ||
) -> Manager: | ||
return SubProcessManager( | ||
env_dir=env_dir, | ||
def sub_proc_manager_factory() -> SubProcessManager: | ||
return SubProcessManager( | ||
env_dir=env_dir, | ||
kernel_mode=kernel_mode, | ||
) | ||
|
||
return DeferredManager( | ||
kernel_mode=kernel_mode, | ||
manager_factory=sub_proc_manager_factory, | ||
) |
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,74 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Callable, Dict, Optional | ||
|
||
from taskweaver.ces.common import Client, ExecutionResult, KernelModeType, Manager | ||
|
||
|
||
class DeferredClient(Client): | ||
def __init__(self, client_factory: Callable[[], Client]) -> None: | ||
self.client_factory = client_factory | ||
self.proxy_client: Optional[Client] = None | ||
|
||
def start(self) -> None: | ||
# defer the start to the proxy client | ||
pass | ||
|
||
def stop(self) -> None: | ||
if self.proxy_client is not None: | ||
self.proxy_client.stop() | ||
|
||
def load_plugin(self, plugin_name: str, plugin_code: str, plugin_config: Dict[str, str]) -> None: | ||
self._get_proxy_client().load_plugin(plugin_name, plugin_code, plugin_config) | ||
|
||
def test_plugin(self, plugin_name: str) -> None: | ||
self._get_proxy_client().test_plugin(plugin_name) | ||
|
||
def update_session_var(self, session_var_dict: Dict[str, str]) -> None: | ||
self._get_proxy_client().update_session_var(session_var_dict) | ||
|
||
def execute_code(self, exec_id: str, code: str) -> ExecutionResult: | ||
return self._get_proxy_client().execute_code(exec_id, code) | ||
|
||
def _get_proxy_client(self) -> Client: | ||
if self.proxy_client is None: | ||
self.proxy_client = self.client_factory() | ||
self.proxy_client.start() | ||
return self.proxy_client | ||
|
||
|
||
class DeferredManager(Manager): | ||
def __init__(self, kernel_mode: KernelModeType, manager_factory: Callable[[], Manager]) -> None: | ||
super().__init__() | ||
self.kernel_mode: KernelModeType = kernel_mode | ||
self.manager_factory = manager_factory | ||
self.proxy_manager: Optional[Manager] = None | ||
|
||
def initialize(self) -> None: | ||
# defer the initialization to the proxy manager | ||
pass | ||
|
||
def clean_up(self) -> None: | ||
if self.proxy_manager is not None: | ||
self.proxy_manager.clean_up() | ||
|
||
def get_session_client( | ||
self, | ||
session_id: str, | ||
env_id: Optional[str] = None, | ||
session_dir: Optional[str] = None, | ||
cwd: Optional[str] = None, | ||
) -> DeferredClient: | ||
def client_factory() -> Client: | ||
return self._get_proxy_manager().get_session_client(session_id, env_id, session_dir, cwd) | ||
|
||
return DeferredClient(client_factory) | ||
|
||
def get_kernel_mode(self) -> KernelModeType: | ||
return self.kernel_mode | ||
|
||
def _get_proxy_manager(self) -> Manager: | ||
if self.proxy_manager is None: | ||
self.proxy_manager = self.manager_factory() | ||
self.proxy_manager.initialize() | ||
return self.proxy_manager |
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
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.