99- Async command streaming
1010"""
1111
12- import asyncio
1312import os
13+ import asyncio
14+
1415from runloop_api_client import AsyncRunloopSDK
1516
1617
1718async def demonstrate_basic_async ():
1819 """Demonstrate basic async devbox operations."""
1920 print ("=== Basic Async Operations ===" )
20-
21+
2122 sdk = AsyncRunloopSDK ()
22-
23+
2324 # Create a devbox with async context manager
2425 async with sdk .devbox .create (name = "async-example-devbox" ) as devbox :
2526 print (f"Created devbox: { devbox .id } " )
26-
27+
2728 # Execute command asynchronously
2829 result = await devbox .cmd .exec ("echo 'Hello from async devbox!'" )
2930 output = await result .stdout ()
3031 print (f"Command output: { output .strip ()} " )
31-
32+
3233 # File operations
3334 await devbox .file .write (
3435 path = "/home/user/async_test.txt" ,
3536 contents = "Hello from async operations!\n " ,
3637 )
3738 content = await devbox .file .read (path = "/home/user/async_test.txt" )
3839 print (f"File content: { content .strip ()} " )
39-
40+
4041 print ("Devbox automatically shutdown\n " )
4142
4243
4344async def demonstrate_concurrent_commands ():
4445 """Execute multiple commands concurrently on the same devbox."""
4546 print ("=== Concurrent Command Execution ===" )
46-
47+
4748 sdk = AsyncRunloopSDK ()
48-
49+
4950 async with sdk .devbox .create (name = "concurrent-commands-devbox" ) as devbox :
5051 print (f"Created devbox: { devbox .id } " )
51-
52+
5253 # Execute multiple commands concurrently
5354 async def run_command (cmd : str , label : str ):
5455 print (f"Starting: { label } " )
5556 result = await devbox .cmd .exec (cmd )
5657 output = await result .stdout ()
5758 print (f"{ label } completed: { output .strip ()} " )
5859 return output
59-
60+
6061 # Run multiple commands in parallel
6162 results = await asyncio .gather (
6263 run_command ("echo 'Task 1' && sleep 1" , "Task 1" ),
6364 run_command ("echo 'Task 2' && sleep 1" , "Task 2" ),
6465 run_command ("echo 'Task 3' && sleep 1" , "Task 3" ),
6566 )
66-
67+
6768 print (f"All { len (results )} tasks completed\n " )
6869
6970
7071async def demonstrate_multiple_devboxes ():
7172 """Create and manage multiple devboxes concurrently."""
7273 print ("=== Managing Multiple Devboxes ===" )
73-
74+
7475 sdk = AsyncRunloopSDK ()
75-
76+
7677 async def create_and_use_devbox (name : str , number : int ):
7778 """Create a devbox, run a command, and return the result."""
7879 async with sdk .devbox .create (name = name ) as devbox :
7980 print (f"Devbox { number } ({ devbox .id } ): Created" )
80-
81+
8182 # Run a command
8283 result = await devbox .cmd .exec (f"echo 'Hello from devbox { number } '" )
8384 output = await result .stdout ()
8485 print (f"Devbox { number } : { output .strip ()} " )
85-
86+
8687 return output
87-
88+
8889 # Create and use multiple devboxes concurrently
8990 results = await asyncio .gather (
9091 create_and_use_devbox ("multi-devbox-1" , 1 ),
9192 create_and_use_devbox ("multi-devbox-2" , 2 ),
9293 create_and_use_devbox ("multi-devbox-3" , 3 ),
9394 )
94-
95+
9596 print (f"All { len (results )} devboxes completed and shutdown\n " )
9697
9798
9899async def demonstrate_async_streaming ():
99100 """Demonstrate real-time command output streaming with async callbacks."""
100101 print ("=== Async Command Streaming ===" )
101-
102+
102103 sdk = AsyncRunloopSDK ()
103-
104+
104105 async with sdk .devbox .create (name = "streaming-devbox" ) as devbox :
105106 print (f"Created devbox: { devbox .id } " )
106-
107+
107108 # Async callback to capture output
108109 output_lines = []
109-
110+
110111 async def capture_output (line : str ):
111112 print (f"[STREAM] { line .strip ()} " )
112113 output_lines .append (line )
113-
114+
114115 # Execute command with streaming output
115116 print ("\n Streaming command output:" )
116117 await devbox .cmd .exec (
117- " for i in 1 2 3 4 5; do echo \ " Line $i\ " ; sleep 0.2; done" ,
118+ ' for i in 1 2 3 4 5; do echo "Line $i"; sleep 0.2; done' ,
118119 stdout = capture_output ,
119120 )
120-
121+
121122 print (f"\n Captured { len (output_lines )} lines of output\n " )
122123
123124
124125async def demonstrate_async_execution ():
125126 """Demonstrate async execution management."""
126127 print ("=== Async Execution Management ===" )
127-
128+
128129 sdk = AsyncRunloopSDK ()
129-
130+
130131 async with sdk .devbox .create (name = "async-exec-devbox" ) as devbox :
131132 print (f"Created devbox: { devbox .id } " )
132-
133+
133134 # Start an async execution
134- execution = await devbox .cmd .exec_async (
135- "echo 'Starting...'; sleep 2; echo 'Finished!'"
136- )
135+ execution = await devbox .cmd .exec_async ("echo 'Starting...'; sleep 2; echo 'Finished!'" )
137136 print (f"Started execution: { execution .execution_id } " )
138-
137+
139138 # Poll execution state
140139 state = await execution .get_state ()
141140 print (f"Initial status: { state .status } " )
142-
141+
143142 # Wait for completion
144143 print ("Waiting for completion..." )
145144 result = await execution .result ()
146145 print (f"Exit code: { result .exit_code } " )
147146 output = await result .stdout ()
148147 print (f"Output:\n { output } " )
149-
148+
150149 # Start another execution and kill it
151150 print ("\n Starting long-running process..." )
152151 long_execution = await devbox .cmd .exec_async ("sleep 30" )
153152 print (f"Execution ID: { long_execution .execution_id } " )
154-
153+
155154 # Wait a bit then kill it
156155 await asyncio .sleep (1 )
157156 print ("Killing execution..." )
@@ -162,14 +161,14 @@ async def demonstrate_async_execution():
162161async def main ():
163162 """Run all async demonstrations."""
164163 print ("Initialized Async Runloop SDK\n " )
165-
164+
166165 # Run demonstrations
167166 await demonstrate_basic_async ()
168167 await demonstrate_concurrent_commands ()
169168 await demonstrate_multiple_devboxes ()
170169 await demonstrate_async_streaming ()
171170 await demonstrate_async_execution ()
172-
171+
173172 print ("All async demonstrations completed!" )
174173
175174
@@ -180,10 +179,9 @@ async def main():
180179 print ("Please set it to your Runloop API key:" )
181180 print (" export RUNLOOP_API_KEY=your-api-key" )
182181 exit (1 )
183-
182+
184183 try :
185184 asyncio .run (main ())
186185 except Exception as e :
187186 print (f"\n Error: { e } " )
188187 raise
189-
0 commit comments