from threading import Lock from .io_buffer import IOBuffer class WebIOBuffer(IOBuffer): """ Thread-safe WebIOBuffer that maintains the synchronous IOBuffer interface. Uses threading primitives instead of asyncio for synchronization. """ def __init__(self): self._stdin_buffer = "" self._stdout_buffer = "" self._lock = Lock() def append_stdin(self, content: str) -> None: """Thread-safe append to stdin buffer.""" if not content: return with self._lock: self._stdin_buffer += content def read(self) -> str: """Thread-safe read from stdin buffer.""" with self._lock: content = self._stdin_buffer self._stdin_buffer = "" return content def write(self, content: str) -> None: """Thread-safe write to stdout buffer.""" if not content: return with self._lock: self._stdout_buffer += content def buffer_length(self) -> int: """ Get the current length of the stdin buffer. Returns: int: Number of characters in the stdin buffer """ return len(self._stdin_buffer) def append_stdin(self, content: str) -> None: """ Append content to the stdin buffer. Args: content: String content to append to the stdin buffer """ if content: self._stdin_buffer += content def get_stdout(self) -> str: """Thread-safe get stdout content.""" with self._lock: return self._stdout_buffer def clear_stdout(self) -> None: """Thread-safe clear stdout buffer.""" with self._lock: self._stdout_buffer = "" def buffer_length(self) -> int: """Thread-safe get stdin buffer length.""" with self._lock: return len(self._stdin_buffer)