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.
This commit is contained in:
Kovid Goyal 2024-05-18 08:34:01 +05:30
parent a9924d2ab7
commit c72ff568c5
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 8 additions and 13 deletions

View file

@ -2349,7 +2349,7 @@ def run_background_process(
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 @@ def run(stdin: Optional[int], stdout: Optional[int], stderr: Optional[int]) -> N
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))

View file

@ -125,14 +125,10 @@ def on_death(exit_status: int, err: Optional[Exception]) -> None:
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()

View file

@ -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 {