Skip to content

Commit aba0b81

Browse files
committed
corrected examples
1 parent d8c8d73 commit aba0b81

4 files changed

Lines changed: 46 additions & 54 deletions

File tree

examples/async_devbox.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def demonstrate_basic_async():
2222
sdk = AsyncRunloopSDK()
2323

2424
# Create a devbox with async context manager
25-
async with sdk.devbox.create(name="async-example-devbox") as devbox:
25+
async with await sdk.devbox.create(name="async-example-devbox") as devbox:
2626
print(f"Created devbox: {devbox.id}")
2727

2828
# Execute command asynchronously
@@ -47,7 +47,7 @@ async def demonstrate_concurrent_commands():
4747

4848
sdk = AsyncRunloopSDK()
4949

50-
async with sdk.devbox.create(name="concurrent-commands-devbox") as devbox:
50+
async with await sdk.devbox.create(name="concurrent-commands-devbox") as devbox:
5151
print(f"Created devbox: {devbox.id}")
5252

5353
# Execute multiple commands concurrently
@@ -76,7 +76,7 @@ async def demonstrate_multiple_devboxes():
7676

7777
async def create_and_use_devbox(name: str, number: int):
7878
"""Create a devbox, run a command, and return the result."""
79-
async with sdk.devbox.create(name=name) as devbox:
79+
async with await sdk.devbox.create(name=name) as devbox:
8080
print(f"Devbox {number} ({devbox.id}): Created")
8181

8282
# Run a command
@@ -97,18 +97,18 @@ async def create_and_use_devbox(name: str, number: int):
9797

9898

9999
async def demonstrate_async_streaming():
100-
"""Demonstrate real-time command output streaming with async callbacks."""
100+
"""Demonstrate real-time command output streaming with synchronous callbacks."""
101101
print("=== Async Command Streaming ===")
102102

103103
sdk = AsyncRunloopSDK()
104104

105-
async with sdk.devbox.create(name="streaming-devbox") as devbox:
105+
async with await sdk.devbox.create(name="streaming-devbox") as devbox:
106106
print(f"Created devbox: {devbox.id}")
107107

108-
# Async callback to capture output
109-
output_lines = []
108+
# Synchronous callback to capture output (callbacks must be sync, not async)
109+
output_lines: list[str] = []
110110

111-
async def capture_output(line: str):
111+
def capture_output(line: str):
112112
print(f"[STREAM] {line.strip()}")
113113
output_lines.append(line)
114114

@@ -128,7 +128,7 @@ async def demonstrate_async_execution():
128128

129129
sdk = AsyncRunloopSDK()
130130

131-
async with sdk.devbox.create(name="async-exec-devbox") as devbox:
131+
async with await sdk.devbox.create(name="async-exec-devbox") as devbox:
132132
print(f"Created devbox: {devbox.id}")
133133

134134
# Start an async execution

examples/blueprint_example.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414

1515
from runloop_api_client import RunloopSDK
16+
from runloop_api_client.sdk import Blueprint
1617

1718

1819
def create_simple_blueprint(sdk: RunloopSDK):
@@ -119,7 +120,7 @@ def create_blueprint_from_base(sdk: RunloopSDK):
119120
return base_blueprint, derived_blueprint
120121

121122

122-
def use_blueprint_to_create_devbox(sdk: RunloopSDK, blueprint):
123+
def use_blueprint_to_create_devbox(blueprint: Blueprint):
123124
"""Create and use a devbox from a blueprint."""
124125
print("\n=== Creating Devbox from Blueprint ===")
125126

@@ -157,7 +158,7 @@ def list_blueprints(sdk: RunloopSDK):
157158
print(f" - {info.name} ({bp.id}): {info.status}")
158159

159160

160-
def cleanup_blueprints(sdk: RunloopSDK, blueprints):
161+
def cleanup_blueprints(blueprints: list[Blueprint]):
161162
"""Delete blueprints to clean up."""
162163
print("\n=== Cleaning Up Blueprints ===")
163164

@@ -176,7 +177,7 @@ def main():
176177
sdk = RunloopSDK()
177178
print("Initialized Runloop SDK\n")
178179

179-
created_blueprints = []
180+
created_blueprints: list[Blueprint] = []
180181

181182
try:
182183
# Create simple blueprint
@@ -192,15 +193,15 @@ def main():
192193
created_blueprints.extend([base_bp, derived_bp])
193194

194195
# Use a blueprint to create a devbox
195-
use_blueprint_to_create_devbox(sdk, simple_bp)
196+
use_blueprint_to_create_devbox(simple_bp)
196197

197198
# List all blueprints
198199
list_blueprints(sdk)
199200

200201
finally:
201202
# Cleanup all created blueprints
202203
if created_blueprints:
203-
cleanup_blueprints(sdk, created_blueprints)
204+
cleanup_blueprints(created_blueprints)
204205

205206
print("\nBlueprint example completed!")
206207

examples/storage_example.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pathlib import Path
1515

1616
from runloop_api_client import RunloopSDK
17+
from runloop_api_client.sdk import StorageObject
1718

1819

1920
def demonstrate_text_upload(sdk: RunloopSDK):
@@ -90,7 +91,8 @@ def demonstrate_file_upload(sdk: RunloopSDK):
9091
info = obj.refresh()
9192
print(f"Object name: {info.name}")
9293
print(f"Content type: {info.content_type}")
93-
print(f"Metadata: {info.metadata}")
94+
# TODO: Add metadata to the object
95+
# print(f"Metadata: {info.metadata}")
9496

9597
return obj
9698
finally:
@@ -110,7 +112,7 @@ def demonstrate_manual_upload(sdk: RunloopSDK):
110112
)
111113

112114
print(f"Created storage object: {obj.id}")
113-
print(f"Upload URL: {obj.upload_url[:50]}...")
115+
print(f"Upload URL: {obj.upload_url[:50] if obj.upload_url is not None else 'None'}...")
114116

115117
# Step 2: Upload content to the presigned URL
116118
content = b"This content was uploaded manually using the upload flow."
@@ -242,7 +244,7 @@ def list_storage_objects(sdk: RunloopSDK):
242244
print(f" - {info.name} ({obj.id}): {info.content_type}")
243245

244246

245-
def cleanup_storage_objects(sdk: RunloopSDK, objects):
247+
def cleanup_storage_objects(objects: list[StorageObject]):
246248
"""Delete storage objects to clean up."""
247249
print("\n=== Cleaning Up Storage Objects ===")
248250

@@ -261,7 +263,7 @@ def main():
261263
sdk = RunloopSDK()
262264
print("Initialized Runloop SDK\n")
263265

264-
created_objects = []
266+
created_objects: list[StorageObject] = []
265267

266268
try:
267269
# Demonstrate different upload methods
@@ -290,7 +292,7 @@ def main():
290292
finally:
291293
# Cleanup all created objects
292294
if created_objects:
293-
cleanup_storage_objects(sdk, created_objects)
295+
cleanup_storage_objects(created_objects)
294296

295297
print("\nStorage object example completed!")
296298

examples/streaming_output.py

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- Streaming stderr
88
- Streaming combined output
99
- Processing output line-by-line
10-
- Async streaming callbacks
10+
- Using synchronous callbacks (async callbacks not supported)
1111
"""
1212

1313
import os
@@ -76,7 +76,7 @@ def demonstrate_combined_streaming(sdk: RunloopSDK):
7676
print(f"Created devbox: {devbox.id}\n")
7777

7878
# Track all output
79-
all_output = []
79+
all_output: list[tuple[str, str]] = []
8080

8181
def capture_all(line: str):
8282
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
@@ -159,7 +159,7 @@ def demonstrate_long_running_stream(sdk: RunloopSDK):
159159
with sdk.devbox.create(name="streaming-longrun-devbox") as devbox:
160160
print(f"Created devbox: {devbox.id}\n")
161161

162-
progress_items = []
162+
progress_items: list[str] = []
163163

164164
def track_progress(line: str):
165165
line = line.rstrip()
@@ -188,46 +188,35 @@ def track_progress(line: str):
188188

189189

190190
async def demonstrate_async_streaming():
191-
"""Demonstrate async streaming with async callbacks."""
192-
print("\n=== Async Streaming ===")
191+
"""Demonstrate async devbox with synchronous callbacks.
192+
193+
Note: Callbacks must be synchronous functions, not async.
194+
Use thread-safe queues if you need to process output asynchronously.
195+
"""
196+
print("\n=== Async Devbox with Synchronous Callbacks ===")
193197

194198
sdk = AsyncRunloopSDK()
195199

196-
async with sdk.devbox.create(name="async-streaming-devbox") as devbox:
200+
async with await sdk.devbox.create(name="async-streaming-devbox") as devbox:
197201
print(f"Created devbox: {devbox.id}\n")
198202

199-
# Async callback with async operations
200-
output_queue = asyncio.Queue()
201-
202-
async def async_capture(line: str):
203-
# Simulate async processing (e.g., writing to a database)
204-
await asyncio.sleep(0.01)
205-
await output_queue.put(line.rstrip())
206-
print(f"[ASYNC] {line.rstrip()}")
207-
208-
# Start processing task
209-
async def process_queue():
210-
processed = []
211-
while True:
212-
try:
213-
line = await asyncio.wait_for(output_queue.get(), timeout=2.0)
214-
processed.append(line)
215-
except asyncio.TimeoutError:
216-
break
217-
return processed
218-
219-
processor = asyncio.create_task(process_queue())
220-
221-
# Execute with async streaming
222-
print("Streaming with async callbacks:")
203+
# Synchronous callback (callbacks must be sync, not async)
204+
output_lines: list[str] = []
205+
206+
def capture_output(line: str):
207+
# Callback must be synchronous
208+
output_lines.append(line.rstrip())
209+
print(f"[CAPTURED] {line.rstrip()}")
210+
211+
# Execute with synchronous callback
212+
print("Streaming with synchronous callbacks:")
223213
await devbox.cmd.exec(
224-
'for i in 1 2 3 4 5; do echo "Async line $i"; sleep 0.2; done',
225-
stdout=async_capture,
214+
'for i in 1 2 3 4 5; do echo "Line $i"; sleep 0.2; done',
215+
stdout=capture_output,
226216
)
227217

228-
# Wait for queue processing
229-
processed = await processor
230-
print(f"\nProcessed {len(processed)} lines asynchronously")
218+
print(f"\nCaptured {len(output_lines)} lines")
219+
print(f"Output: {output_lines}")
231220

232221

233222
def main():

0 commit comments

Comments
 (0)