2025-05-23 13:53:44 +02:00
|
|
|
import asyncio
|
|
|
|
|
|
2025-06-11 12:29:47 +00:00
|
|
|
from daytona import Daytona, SessionExecuteRequest
|
2025-05-23 13:53:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
daytona = Daytona()
|
|
|
|
|
sandbox = daytona.create()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
session_id = "exec-session-1"
|
|
|
|
|
sandbox.process.create_session(session_id)
|
|
|
|
|
|
|
|
|
|
command = sandbox.process.execute_session_command(
|
|
|
|
|
session_id,
|
|
|
|
|
SessionExecuteRequest(
|
2025-09-08 16:30:25 +02:00
|
|
|
command=(
|
2026-01-30 09:52:44 +01:00
|
|
|
'printf "Enter your name: \\n" && read name && printf "Hello, %s\\n" "$name"; '
|
|
|
|
|
'counter=1; while (( counter <= 3 )); do echo "Count: $counter"; '
|
2025-09-08 16:30:25 +02:00
|
|
|
"((counter++)); sleep 2; done; non-existent-command"
|
|
|
|
|
),
|
2025-05-23 13:53:44 +02:00
|
|
|
run_async=True,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logs_task = asyncio.create_task(
|
|
|
|
|
sandbox.process.get_session_command_logs_async(
|
2025-09-08 16:30:25 +02:00
|
|
|
session_id,
|
|
|
|
|
command.cmd_id,
|
|
|
|
|
lambda log: print(f"[STDOUT]: {log}"),
|
|
|
|
|
lambda log: print(f"[STDERR]: {log}"),
|
2025-05-23 13:53:44 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print("Continuing execution while logs are streaming...")
|
|
|
|
|
await asyncio.sleep(1)
|
2026-01-30 09:52:44 +01:00
|
|
|
print("Sending input to the command")
|
|
|
|
|
sandbox.process.send_session_command_input(session_id, command.cmd_id, "Alice")
|
|
|
|
|
print("Input sent to the command")
|
2025-05-23 13:53:44 +02:00
|
|
|
print("Other operations completed!")
|
|
|
|
|
|
|
|
|
|
print("Now waiting for logs to complete...")
|
|
|
|
|
await logs_task
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error: {e}")
|
|
|
|
|
finally:
|
|
|
|
|
print("Cleaning up sandbox...")
|
|
|
|
|
daytona.delete(sandbox)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
asyncio.run(main())
|