Files
trycua--cua/libs/python/computer-server/examples/usage_example.py
T
wehub-resource-sync 91e75e620b
CI: cua-driver distro-compat matrix / Resolve release version (push) Waiting to run
CI: cua-driver distro-compat matrix / debian:12 (glibc 2.36) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / fedora:41 (glibc 2.40) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / rockylinux:9 (glibc 2.34) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / ubuntu:22.04 (glibc 2.35) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / ubuntu:24.04 (glibc 2.39) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / Distro compat summary (push) Blocked by required conditions
CI: Nix Linux Rust source / Nix / compositor build (push) Waiting to run
CI: Nix Linux Rust source / Nix / driver package (push) Waiting to run
CI: Nix Linux Rust source / Nix / Rust unit tests (push) Waiting to run
CI: Rust Linux unit / Rust Linux unit and compile (push) Waiting to run
CI: Rust Windows unit / Rust Windows unit and compile (push) Waiting to run
CI: SPDX Headers / Check SPDX headers (warn-only) (push) Waiting to run
CD: Docs MCP Server / build (linux/amd64) (push) Waiting to run
CD: Docs MCP Server / build (linux/arm64) (push) Waiting to run
CD: Docs MCP Server / merge (push) Blocked by required conditions
chore: import upstream snapshot with attribution
2026-07-13 13:03:19 +08:00

77 lines
2.0 KiB
Python

#!/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()