Skip to content

Feat: Enable switching between UI Thread and Multi Process modes #1

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
32 changes: 27 additions & 5 deletions app/src/androidMain/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import time
from model.config import ChatHistory
from repl import REPLConfig, send_server_launch_intent, kernel
from repl import REPLConfig, send_server_launch_intent, send_server_stop_intent, kernel, UIThreadKernelService


config = REPLConfig(manager_class=kernel.UIThreadKernelManager)
Expand Down Expand Up @@ -49,12 +49,34 @@ def compose(cls):

cls.user_prompt = remember_saveable("안녕하세요!")

cls.target_manager = remember_saveable(config.manager.split(".")[-1])

def get_target_manager_class():
if cls.target_manager.getValue() == "UIThreadKernelManager":
return kernel.UIThreadKernelManager
else:
return kernel.InAppKernelManager

def change_mode():
current_manager = cls.target_manager.getValue()
new_value = "InAppKernelManager" if current_manager == "UIThreadKernelManager" else "UIThreadKernelManager"
cls.target_manager.setValue(new_value)

def run_jupyter():
browser_intent = Intent(Intent.ACTION_VIEW, Uri.parse(config.uri))
new_config = REPLConfig(manager_class=get_target_manager_class())
browser_intent = Intent(Intent.ACTION_VIEW, Uri.parse(new_config.uri))

def runner():
send_server_launch_intent(context, config)
kernel_service_class = UIThreadKernelService.getClass()
intent = Intent(context, kernel_service_class)
context.stopService(intent)

send_server_stop_intent(context)
time.sleep(1)

send_server_launch_intent(context, new_config)
time.sleep(1)

context.startActivity(browser_intent)

cls.scope.launch(runner)
Expand All @@ -72,10 +94,10 @@ def View():
Text("")

Button(
onclick=lambda: None,
onclick=change_mode,
color=0xFF000000,
content=lambda: {
Text(f"실행 모드 변경 (현재 옵션: UI 스레드 동기화 모드)", color=0xFFFFFFFF)
Text(f"실행 모드 변경 (현재 옵션: {cls.target_manager.getValue()[:-7]} 모드)", color=0xFFFFFFFF)
}
)
Button(
Expand Down
4 changes: 2 additions & 2 deletions app/src/androidMain/python/repl/kernel/kernel_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def process_info(self):
:return: The process info if the kernel is running, None otherwise
"""
activity_manager = app.getSystemService(app.ACTIVITY_SERVICE)
for p_info in activity_manager.getRunningAppProcesses(Integer.MAX_VALUE).toArray():
for p_info in activity_manager.getRunningAppProcesses().toArray():
if ":".join(["", *p_info.processName.split(":")[1:]]) == f":kernel{self.process_name}":
return p_info
return None
Expand Down Expand Up @@ -252,4 +252,4 @@ async def launch_kernel(self, cmd: List[str], **kwargs: Any) -> KernelConnection
return self.connection_info


#provisioning.LocalProvisioner = InAppLocalPrivateProvisioner
provisioning.LocalProvisioner = InAppLocalPrivateProvisioner
2 changes: 1 addition & 1 deletion app/src/commonMain/python/repl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .config import REPLConfig
from .server import run_lab_server, send_server_launch_intent
from .server import run_lab_server, send_server_launch_intent, send_server_stop_intent

import signal

Expand Down
4 changes: 4 additions & 0 deletions app/src/commonMain/python/repl/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ def send_server_launch_intent(context, config: REPLConfig):
for key, value in config.dict.items():
intent.putExtra(key, value)
context.startService(intent)

def send_server_stop_intent(context):
intent = Intent(context, InAppLabServerService.getClass())
context.stopService(intent)