from beam import Image, Sandbox
# Create a sandbox and make some changes to the filesystem
sandbox = Sandbox(cpu=1).create()
sandbox.expose_port(8000)
p = sandbox.process.exec("python", "-c", "import http.server; http.server.HTTPServer(('', 8000), http.server.SimpleHTTPRequestHandler).serve_forever()")
# Wait until the server is accepting connections before capturing it.
ready = sandbox.process.run_code("""
import socket
import time
for _ in range(100):
try:
with socket.create_connection(("127.0.0.1", 8000), timeout=1):
break
except OSError:
time.sleep(0.1)
else:
raise RuntimeError("server not ready")
""")
if ready.exit_code != 0:
sandbox.terminate()
raise RuntimeError("server did not become ready; snapshot cancelled")
# Generate a memory snapshot and terminate the sandbox
snapshot_id = sandbox.snapshot_memory()
print(sandbox.list_urls())
sandbox.terminate()