import gi import subprocess gi.require_version('GObject', '2.0') from gi.repository import GObject def on_process_exit(pid, condition): print(f"Process {pid} exited with status {condition}") pid = subprocess.Popen(["echo", "Hello, World!"]).pid watch = GObject.child_watch_add(pid, on_process_exit)
import gi import subprocess gi.require_version('GObject', '2.0') from gi.repository import GObject, GLib def on_process_exit(pid, condition): print(f"Process {pid} exited with status {condition}") def start_child_process(command): pid = subprocess.Popen(command).pid GObject.child_watch_add(pid, on_process_exit) start_child_process(["echo", "Hello, World!"]) start_child_process(["sleep", "10"]) start_child_process(["echo", "Goodbye!"]) GLib.MainLoop().run()In this example, we define a `start_child_process` function that takes a command to run as its argument. It creates a child process using `subprocess` and sets up a watcher using `child_watch_add`. We then call this function for each process we want to run. Finally, we use `GLib.MainLoop().run()` to start the main event loop so the program will not exit until all child processes have completed.