From c72ff568c502a8e7feaca41f6426a853618d26d1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 18 May 2024 08:34:01 +0530 Subject: [PATCH] Micro-optimization Start the background process immediately and use a zero timer only if executing the process fails, thereby guaranteeing ordering without imposing a delay on process start. --- kitty/boss.py | 8 +++----- kitty/rc/run.py | 12 ++++-------- tools/cmd/at/socket_io.go | 1 + 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 414056559..37d2d6e88 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -2349,7 +2349,7 @@ class Boss: cwd_from: Optional[CwdRequest] = None, allow_remote_control: bool = False, remote_control_passwords: Optional[Dict[str, Sequence[str]]] = None, - notify_on_death: Optional[Callable[[int, Optional[Exception]], None]] = None, + notify_on_death: Optional[Callable[[int, Optional[Exception]], None]] = None, # guaranteed to be called only after event loop tick stdout: Optional[int] = None, stderr: Optional[int] = None, ) -> None: import subprocess @@ -2407,11 +2407,9 @@ class Boss: with suppress(OSError): os.close(fd) if notify_on_death: - try: + def callback(err: Exception, timer_id: Optional[int]) -> None: notify_on_death(-1, err) - except Exception: - import traceback - traceback.print_exc() + add_timer(partial(callback, err), 0, False) else: self.show_error(_('Failed to run background process'), _('Failed to run background process with error: {}').format(err)) diff --git a/kitty/rc/run.py b/kitty/rc/run.py index a5492a3c9..41eb26e00 100644 --- a/kitty/rc/run.py +++ b/kitty/rc/run.py @@ -125,14 +125,10 @@ The executed program will have privileges to run remote control commands in kitt for k, v in parse_env(x, env): env[k] = v - def callback(timer_id: Optional[int]) -> None: - boss.run_background_process( - cmdline, env=env, stdin=stdin_data, stdout=stdout.fileno(), stderr=stderr.fileno(), - notify_on_death=on_death, remote_control_passwords=rcp, allow_remote_control=allow_remote_control - ) - # Ensure that AsyncResponse is queued before the background process response - from kitty.fast_data_types import add_timer - add_timer(callback, 0, False) + boss.run_background_process( + cmdline, env=env, stdin=stdin_data, stdout=stdout.fileno(), stderr=stderr.fileno(), + notify_on_death=on_death, remote_control_passwords=rcp, allow_remote_control=allow_remote_control + ) return AsyncResponse() diff --git a/tools/cmd/at/socket_io.go b/tools/cmd/at/socket_io.go index fcc86394b..fadfe50ec 100644 --- a/tools/cmd/at/socket_io.go +++ b/tools/cmd/at/socket_io.go @@ -119,6 +119,7 @@ func run_stdin_echo_loop(conn *net.Conn, io_data *rc_io_data) (err error) { func simple_socket_io(conn *net.Conn, io_data *rc_io_data) (serialized_response []byte, err error) { r := response_reader{} + r.pending_responses = make([][]byte, 0, 2) // we read at most two responses first_escape_code_sent := false wants_streaming := io_data.rc.Stream for {