-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
48 lines (42 loc) · 1.48 KB
/
Copy pathProgram.cs
File metadata and controls
48 lines (42 loc) · 1.48 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
using System.Text;
using CreateOS.Sandbox;
const string script = """
import time
for number in range(1, 6):
print(f"result {number}", flush=True)
time.sleep(1)
""";
using var shutdown = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) => { eventArgs.Cancel = true; shutdown.Cancel(); };
var client = new SandboxClient();
var sandbox = await client.CreateSandboxAsync(new CreateSandboxRequest
{
Shape = "s-1vcpu-1gb",
RootFileSystem = "devbox:1"
}, cancellationToken: shutdown.Token);
Console.WriteLine($"created: {sandbox.Id}");
try
{
await using var source = new MemoryStream(Encoding.UTF8.GetBytes(script));
await sandbox.Files.UploadAsync("/tmp/script.py", source, token: shutdown.Token);
Console.WriteLine("--- streaming output ---");
await foreach (var item in sandbox.StreamCommandAsync(new RunCommandRequest
{
Command = "python3",
Arguments = ["/tmp/script.py"]
}, token: shutdown.Token))
{
switch (item.Type)
{
case ExecStreamEventType.Stdout: Console.Write(item.Data); break;
case ExecStreamEventType.Stderr: Console.Error.Write(item.Data); break;
case ExecStreamEventType.Error: Console.Error.WriteLine($"agent error: {item.ErrorMessage}"); break;
case ExecStreamEventType.Exit: Console.WriteLine($"(exited {item.ExitCode})"); break;
}
}
}
finally
{
await sandbox.DestroyAsync(CancellationToken.None);
Console.WriteLine("destroyed");
}