mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-11 18:32:12 +02:00
dont use a thread for I/O with kitten
This commit is contained in:
@@ -30,9 +30,6 @@ def as_json(indent: Optional[int] = None) -> str:
|
||||
return json.dumps(groups, indent=indent)
|
||||
|
||||
|
||||
exception_in_io_handler: Optional[Exception] = None
|
||||
|
||||
|
||||
def handle_io(from_kitten: BinaryIO, to_kitten: BinaryIO) -> None:
|
||||
import json
|
||||
global exception_in_io_handler
|
||||
@@ -42,22 +39,17 @@ def handle_io(from_kitten: BinaryIO, to_kitten: BinaryIO) -> None:
|
||||
to_kitten.write(b'\n')
|
||||
to_kitten.flush()
|
||||
|
||||
try:
|
||||
send_to_kitten(create_family_groups(add_variable_data=True))
|
||||
for line in from_kitten:
|
||||
cmd = json.loads(line)
|
||||
action = cmd['action']
|
||||
if action == 'ping':
|
||||
send_to_kitten({'action': 'pong'})
|
||||
except Exception as e:
|
||||
exception_in_io_handler = e
|
||||
send_to_kitten(create_family_groups(add_variable_data=True))
|
||||
for line in from_kitten:
|
||||
cmd = json.loads(line)
|
||||
action = cmd['action']
|
||||
if action == 'ping':
|
||||
send_to_kitten({'action': 'pong'})
|
||||
|
||||
|
||||
def main(argv: Sequence[str]) -> None:
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from threading import Thread
|
||||
|
||||
from kitty.constants import kitten_exe
|
||||
argv = list(argv)
|
||||
@@ -67,9 +59,12 @@ def main(argv: Sequence[str]) -> None:
|
||||
if os.environ.get('KITTY_STDIO_FORWARDED'):
|
||||
pass_fds.append(int(os.environ['KITTY_STDIO_FORWARDED']))
|
||||
p = subprocess.Popen([kitten_exe(), '__list_fonts__'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, pass_fds=pass_fds)
|
||||
Thread(target=handle_io, args=(p.stdout, p.stdin), daemon=True).start()
|
||||
try:
|
||||
raise SystemExit(p.wait())
|
||||
finally:
|
||||
if exception_in_io_handler is not None:
|
||||
print(exception_in_io_handler, file=sys.stderr)
|
||||
handle_io(p.stdout, p.stdin)
|
||||
except Exception:
|
||||
ret = p.wait()
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
else:
|
||||
ret = p.wait()
|
||||
raise SystemExit(ret)
|
||||
|
||||
@@ -22,6 +22,20 @@ func json_decode(v any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func to_kitty(v any) error {
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not encode message to kitty with error: %w", err)
|
||||
}
|
||||
if _, err = os.Stdout.Write(data); err != nil {
|
||||
return fmt.Errorf("Failed to send message to kitty with I/O error: %w", err)
|
||||
}
|
||||
if _, err = os.Stdout.WriteString("\n"); err != nil {
|
||||
return fmt.Errorf("Failed to send message to kitty with I/O error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() (rc int, err error) {
|
||||
json_decoder = json.NewDecoder(os.Stdin)
|
||||
lp, err := loop.New()
|
||||
|
||||
Reference in New Issue
Block a user