mirror of
https://github.com/trycua/computer.git
synced 2026-01-04 04:19:57 -06:00
Reorganize lib folder w/typescript and python roots, initialize core library.
This commit is contained in:
3
libs/python/computer-server/examples/__init__.py
Normal file
3
libs/python/computer-server/examples/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Examples package for the CUA Computer API.
|
||||
"""
|
||||
76
libs/python/computer-server/examples/usage_example.py
Normal file
76
libs/python/computer-server/examples/usage_example.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Example showing how to use the CUA Computer API as an imported package.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
# For type checking only
|
||||
if TYPE_CHECKING:
|
||||
from computer_api import Server
|
||||
|
||||
# Setup logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Example 1: Synchronous usage (blocks until server is stopped)
|
||||
def example_sync():
|
||||
"""
|
||||
Example of synchronous server usage. This will block until interrupted.
|
||||
Run with: python3 -m examples.usage_example sync
|
||||
"""
|
||||
# Import directly to avoid any confusion
|
||||
from computer_api.server import Server
|
||||
|
||||
server = Server(port=8080)
|
||||
print("Server started at http://localhost:8080")
|
||||
print("Press Ctrl+C to stop the server")
|
||||
|
||||
try:
|
||||
server.start() # This will block until the server is stopped
|
||||
except KeyboardInterrupt:
|
||||
print("Server stopped by user")
|
||||
|
||||
|
||||
# Example 2: Asynchronous usage
|
||||
async def example_async():
|
||||
"""
|
||||
Example of asynchronous server usage. This will start the server in the background
|
||||
and allow other operations to run concurrently.
|
||||
Run with: python3 -m examples.usage_example async
|
||||
"""
|
||||
# Import directly to avoid any confusion
|
||||
from computer_api.server import Server
|
||||
|
||||
server = Server(port=8080)
|
||||
|
||||
# Start the server in the background
|
||||
await server.start_async()
|
||||
|
||||
print("Server is running in the background")
|
||||
print("Performing other tasks...")
|
||||
|
||||
# Do other things while the server is running
|
||||
for i in range(5):
|
||||
print(f"Doing work iteration {i+1}/5...")
|
||||
await asyncio.sleep(2)
|
||||
|
||||
print("Work complete, stopping server...")
|
||||
|
||||
# Stop the server when done
|
||||
await server.stop()
|
||||
print("Server stopped")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "async":
|
||||
asyncio.run(example_async())
|
||||
else:
|
||||
example_sync()
|
||||
Reference in New Issue
Block a user