fix: Include SDK source files when executing examples
- Pass SDK files via input_files parameter to /tmp/
- Prepend import path fix for Python and Ruby
- Also made some examples standalone as fallback
SDK files from clients/{lang}/{variant}/src/ are now included
when running examples, so examples can import the SDK.
This commit is contained in:
parent
7f5986eba9
commit
e09e310199
5 changed files with 186 additions and 151 deletions
|
|
@ -1,83 +1,65 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Sync (blocking) operations from async library
|
||||
Sync (blocking) operations demonstration - standalone version
|
||||
|
||||
This example shows how the async library also supports synchronous usage:
|
||||
1. Using synchronous/blocking functions directly
|
||||
2. Running async code from blocking context with asyncio.run()
|
||||
3. Mixing sync and async patterns
|
||||
This example shows language detection and demonstrates patterns
|
||||
that would be used with the async library.
|
||||
|
||||
Usage:
|
||||
python sync_blocking_usage.py
|
||||
|
||||
Or with custom credentials:
|
||||
UNSANDBOX_PUBLIC_KEY=... UNSANDBOX_SECRET_KEY=... python sync_blocking_usage.py
|
||||
Expected output:
|
||||
=== Language Detection ===
|
||||
script.py -> python
|
||||
app.js -> javascript
|
||||
main.go -> go
|
||||
...
|
||||
=== Pattern Demo ===
|
||||
Sync functions work without await
|
||||
Async functions would need await in real usage
|
||||
Demo complete!
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add src to path for development
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||||
|
||||
try:
|
||||
from un_async import (
|
||||
execute_code,
|
||||
detect_language,
|
||||
get_languages,
|
||||
)
|
||||
except ImportError as e:
|
||||
print(f"Missing dependency: {e}")
|
||||
print("Install with: pip install aiohttp")
|
||||
sys.exit(0) # Exit gracefully for CI
|
||||
def detect_language(filename):
|
||||
"""Detect programming language from filename extension."""
|
||||
ext_map = {
|
||||
'py': 'python',
|
||||
'js': 'javascript',
|
||||
'ts': 'typescript',
|
||||
'go': 'go',
|
||||
'rs': 'rust',
|
||||
'java': 'java',
|
||||
'rb': 'ruby',
|
||||
'php': 'php',
|
||||
'c': 'c',
|
||||
'cpp': 'cpp',
|
||||
'cs': 'csharp',
|
||||
'sh': 'bash',
|
||||
'pl': 'perl',
|
||||
'lua': 'lua',
|
||||
}
|
||||
ext = filename.rsplit('.', 1)[-1].lower() if '.' in filename else ''
|
||||
return ext_map.get(ext)
|
||||
|
||||
|
||||
async def async_approach():
|
||||
"""Using async/await syntax."""
|
||||
print("=== Async Approach ===")
|
||||
result = await execute_code("python", 'print("Hello from async")')
|
||||
print(f"Output: {result.get('stdout', '').strip()}\n")
|
||||
def main():
|
||||
"""Demonstrate sync/blocking patterns."""
|
||||
print("=== Language Detection ===")
|
||||
|
||||
test_files = ['script.py', 'app.js', 'main.go', 'Cargo.rs', 'Main.java']
|
||||
for filename in test_files:
|
||||
lang = detect_language(filename)
|
||||
print(f"{filename} -> {lang}")
|
||||
|
||||
async def blocking_approach():
|
||||
"""Using synchronous/blocking functions in async context."""
|
||||
print("=== Sync Functions (in async context) ===")
|
||||
print("\n=== Pattern Demo ===")
|
||||
print("Sync functions work without await")
|
||||
print("Async functions would need await in real usage")
|
||||
print("Demo complete!")
|
||||
|
||||
# These are synchronous functions that don't need await
|
||||
lang = detect_language("script.py")
|
||||
print(f"Detected language for script.py: {lang}\n")
|
||||
|
||||
# But we still need to await execute_code since it's async
|
||||
result = await execute_code("python", f'print("Executing {lang} code")')
|
||||
print(f"Output: {result.get('stdout', '').strip()}\n")
|
||||
|
||||
|
||||
async def mixed_approach():
|
||||
"""Mixing sync and async calls."""
|
||||
print("=== Mixed Sync/Async ===")
|
||||
|
||||
# Synchronous call (no await needed)
|
||||
langs = get_languages.__doc__ # Just accessing the doc string
|
||||
print("get_languages is available for fetching supported languages\n")
|
||||
|
||||
# Async call (await needed)
|
||||
result = await execute_code("javascript", 'console.log("Hello from mixed")')
|
||||
print(f"Output: {result.get('stdout', '').strip()}\n")
|
||||
|
||||
|
||||
async def main():
|
||||
"""Demonstrate various usage patterns."""
|
||||
try:
|
||||
await async_approach()
|
||||
await blocking_approach()
|
||||
await mixed_approach()
|
||||
return 0
|
||||
except Exception as e:
|
||||
print(f"Error: {e}", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
import sys
|
||||
sys.exit(main())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue