Added proc executor

This commit is contained in:
Qubasa
2023-12-19 14:36:37 +01:00
parent 62e8da8f41
commit c64420f191
5 changed files with 55 additions and 48 deletions

View File

@@ -1,6 +1,11 @@
import argparse
from .app import register_join_parser, register_overview_parser, show_overview
from .app import (
register_join_parser,
register_overview_parser,
register_run_parser,
show_overview,
)
def main() -> None:
@@ -16,6 +21,8 @@ def main() -> None:
register_overview_parser(subparser.add_parser("overview", help="overview screen"))
register_run_parser(subparser.add_parser("run", help="run a vm"))
# Executed when no command is given
parser.set_defaults(func=show_overview)
args = parser.parse_args()

View File

@@ -103,3 +103,48 @@ def show_overview(args: argparse.Namespace) -> None:
def register_overview_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=show_overview)
# Define a function that writes to the memfd
def dummy_f() -> None:
import sys
import time
c = 0
while True: # Simulate a long running process
print(f"out: Hello from process c={c}", file=sys.stdout)
print(f"err: Hello from process c={c}", file=sys.stderr)
user = input("Enter to continue: \n")
if user == "q":
raise Exception("User quit")
print(f"User entered {user}", file=sys.stdout)
print(f"User entered {user}", file=sys.stderr)
time.sleep(1) # Wait for 1 second
c += 1
def show_run_vm(parser: argparse.ArgumentParser) -> None:
import os
from .executor import spawn
print("Spawn process")
# proc = spawn(vms.run.run_vm, vm=vm)
proc = spawn(wait_stdin_connect=True, func=dummy_f)
pid = os.getpid()
gpid = os.getpgid(pid)
print(f"Main pid={pid} gpid={gpid}")
assert proc.proc.pid is not None
gpid = os.getpgid(proc.proc.pid)
print(f"Child pid={proc.proc.pid} gpid={gpid}")
def register_run_parser(parser: argparse.ArgumentParser) -> None:
# parser.add_argument(
# "command",
# type=str,
# help="command to run",
# choices=["join", "overview"],
# )
parser.set_defaults(func=show_run_vm)

View File

@@ -33,7 +33,6 @@ class MPProcess:
# def get_all_output(self) -> str:
# os.lseek(self.out_fd, 0, os.SEEK_SET)
# return os.read(self.out_fd, 1024).decode("utf-8")
# def write_all_input(self, input_str: str) -> None:
# os.lseek(self.in_fd, 0, os.SEEK_SET)
# os.write(self.in_fd, input_str.encode("utf-8"))
@@ -76,8 +75,8 @@ def spawn(*, wait_stdin_connect: bool, func: Callable, **kwargs: Any) -> MPProce
# rand_name = str(uuid.uuid4())
rand_name = "test"
proc_name = f"MPExecutor:{func.__name__}:{rand_name}"
out_file_name = f"{rand_name}_out.log"
in_file_name = f"{rand_name}_in.log"
out_file_name = f"{rand_name}_out.txt"
in_file_name = f"{rand_name}_in.fifo"
if os.path.exists(in_file_name):
os.unlink(in_file_name)

View File

@@ -13,24 +13,6 @@ from gi.repository import GdkPixbuf
from clan_vm_manager import assets
# Define a function that writes to the memfd
def dummy_f() -> None:
import sys
import time
c = 0
while True: # Simulate a long running process
print(f"out: Hello from process c={c}", file=sys.stdout)
print(f"err: Hello from process c={c}", file=sys.stderr)
user = input("Enter to continue: \n")
if user == "q":
raise Exception("User quit")
print(f"User entered {user}", file=sys.stdout)
print(f"User entered {user}", file=sys.stderr)
time.sleep(1) # Wait for 1 second
c += 1
@dataclass(frozen=True)
class VMBase:
icon: Path | GdkPixbuf.Pixbuf
@@ -66,20 +48,6 @@ class VMBase:
def run(self) -> None:
print(f"Running VM {self.name}")
# vm = vms.run.inspect_vm(flake_url=self.url, flake_attr="defaultVM")
import os
from .executor import spawn
# proc = spawn(vms.run.run_vm, vm=vm)
proc = spawn(wait_stdin_connect=True, func=dummy_f)
pid = os.getpid()
gpid = os.getpgid(pid)
print(f"Main pid={pid} gpid={gpid}")
assert proc.proc.pid is not None
gpid = os.getpgid(proc.proc.pid)
print(f"Child pid={proc.proc.pid} gpid={gpid}")
# os.killpg(gpid, signal.SIGKILL)
@dataclass(frozen=True)

View File

@@ -1,12 +0,0 @@
Waiting for stdin connection on file test_in.log
Executing function dummy_f now
out: Hello from process c=0
err: Hello from process c=0
Enter to continue:
Traceback (most recent call last):
File "/home/lhebendanz/Projects/clan-core/pkgs/clan-vm-manager/clan_vm_manager/executor.py", line 64, in init_proc
func(**kwargs)
File "/home/lhebendanz/Projects/clan-core/pkgs/clan-vm-manager/clan_vm_manager/models.py", line 26, in dummy_f
raise Exception("User quit")
Exception: User quit
Killing process group pid=547301 gpid=547301