-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03_basic_commands_async.py
More file actions
55 lines (43 loc) · 1.25 KB
/
Copy path03_basic_commands_async.py
File metadata and controls
55 lines (43 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
"""Basic command execution (async variant)"""
import asyncio
import os
import sys
import random
import string
from koyeb import AsyncSandbox
async def main():
api_token = os.getenv("KOYEB_API_TOKEN")
if not api_token:
print("Error: KOYEB_API_TOKEN not set")
return 1
sandbox = None
suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
try:
sandbox = await AsyncSandbox.create(
image="koyeb/sandbox:slim",
name=f"basic-commands-{suffix}",
wait_ready=True,
api_token=api_token,
)
# Simple command
result = await sandbox.exec("echo 'Hello World'")
print(result.stdout.strip())
# Python command
result = await sandbox.exec("python3 -c 'print(2 + 2)'")
print(result.stdout.strip())
# Multi-line Python script
result = await sandbox.exec(
'''python3 -c "
import sys
print(f'Python version: {sys.version.split()[0]}')
print(f'Platform: {sys.platform}')
"'''
)
print(result.stdout.strip())
return 0
finally:
if sandbox:
await sandbox.delete()
if __name__ == "__main__":
sys.exit(asyncio.run(main()))