diff --git a/examples/13_background_processes.py b/examples/13_background_processes.py index a68c32f8..2f263570 100755 --- a/examples/13_background_processes.py +++ b/examples/13_background_processes.py @@ -35,9 +35,10 @@ def main(): ) print(f"Launched process 1: {process_id_1}") - # Launch another process with a different command + # Launch another process with a different command; it outlives the + # round trips below, so it is still running when it gets killed process_id_2 = sandbox.launch_process( - "python3 -c 'import time; [print(f\"Process 2: {i}\") or time.sleep(1) for i in range(5)]'" + "python3 -c 'import time; [print(f\"Process 2: {i}\") or time.sleep(1) for i in range(60)]'" ) print(f"Launched process 2: {process_id_2}") @@ -75,8 +76,8 @@ def main(): print() # Launch a few more processes - process_id_3 = sandbox.launch_process("sleep 5") - process_id_4 = sandbox.launch_process("sleep 5") + process_id_3 = sandbox.launch_process("sleep 60") + process_id_4 = sandbox.launch_process("sleep 60") print(f"Launched processes 3 and 4: {process_id_3}, {process_id_4}") # Wait a bit diff --git a/examples/19_entrypoint_and_command.py b/examples/19_entrypoint_and_command.py index cbb6b6e2..09f04523 100644 --- a/examples/19_entrypoint_and_command.py +++ b/examples/19_entrypoint_and_command.py @@ -17,6 +17,11 @@ def main(): suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=8)) + # A ready sandbox has a running executor; the image's own command may not + # have run yet. Wait up to 10 s for the file it writes before reading it. + def cat_when_present(path): + return f"for i in $(seq 50); do [ -f {path} ] && break; sleep 0.2; done; cat {path}" + # Example 1: Custom command with args print("=== Example 1: Custom command ===") sandbox = None @@ -32,7 +37,9 @@ def main(): env={"LOG_LEVEL": "DEBUG"}, timeout=300, ) - result = sandbox.exec("cat /tmp/command-was-here && echo 'File exists'") + result = sandbox.exec( + f"{cat_when_present('/tmp/command-was-here')} && echo 'File exists'" + ) print(f" {result.stdout.strip()}") assert result.exit_code == 0, "Expected /tmp/command-was-here to exist" print(" OK: custom command created the file") @@ -62,7 +69,7 @@ def main(): timeout=300, ) # The file was created by python3 (the entrypoint), not bash - result = sandbox.exec("cat /tmp/started-by-python") + result = sandbox.exec(cat_when_present("/tmp/started-by-python")) content = result.stdout.strip() print(f" Marker content: {content}") assert content == "yes", f"Expected 'yes', got '{content}'"