Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/13_background_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions examples/19_entrypoint_and_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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}'"
Expand Down
Loading