Mirko Džaja 7fc2cde59e
feat: separate stdout/stderr for session command logs (#2241)
Signed-off-by: MDzaja <mirkodzaja0@gmail.com>
2025-09-08 16:30:25 +02:00

58 lines
2.1 KiB
Python

import asyncio
from daytona import AsyncDaytona, SessionExecuteRequest
async def main():
async with AsyncDaytona() as daytona:
sandbox = await daytona.create()
exec_session_id = "exec-session-1"
await sandbox.process.create_session(exec_session_id)
# Get the session details any time
session = await sandbox.process.get_session(exec_session_id)
print(session)
# Execute a first command in the session
exec_command1 = await sandbox.process.execute_session_command(
exec_session_id, SessionExecuteRequest(command="export FOO=BAR")
)
if exec_command1.exit_code != 0:
print(f"Error: {exec_command1.exit_code} {exec_command1.stderr}")
# Get the session details again to see the command has been executed
session = await sandbox.process.get_session(exec_session_id)
print(session)
# Get the command details
session_command = await sandbox.process.get_session_command(exec_session_id, exec_command1.cmd_id)
print(session_command)
# Execute a second command in the session and see that the environment variable is set
exec_command2 = await sandbox.process.execute_session_command(
exec_session_id, SessionExecuteRequest(command="echo $FOO")
)
if exec_command2.exit_code != 0:
print(f"Error: {exec_command2.exit_code} {exec_command2.stderr}")
else:
print(exec_command2.stdout)
print("Now getting logs for the second command")
logs = await sandbox.process.get_session_command_logs(exec_session_id, exec_command2.cmd_id)
print(f"[STDOUT]: {logs.stdout}")
print(f"[STDERR]: {logs.stderr}")
# You can also list all active sessions
sessions = await sandbox.process.list_sessions()
print(sessions)
# And of course you can delete the session at any time
await sandbox.process.delete_session(exec_session_id)
await daytona.delete(sandbox)
if __name__ == "__main__":
asyncio.run(main())