simbiot-harness/venv/lib/python3.12/site-packages/strands/_async.py
Simbiot Harness b60439152b Simbiot Harness v0.1: тактовая лестница на Strands + CQRS/ES-спина
- harness/es.py: append-only event store + проекции (образец — кооп-портал)
- harness/levels.py: уровни 1с/1м/1ч/1д, контекст ∝ масштабу
- harness/scheduler.py: уровневые потоки, идемпотентные тики, absorbed-стабы
- harness/isolated_runner.py + _llm_worker.py: LLM-тик в сабпроцессе
  (глобальный asyncio-рантайм Strands не переживает 2+ create_harness)
- cli.py: init/task/tasks/status/events + requeue (написан самим минутным агентом)
- 9router: отдельный ключ sk-simbiot, модель auto (glm-5.3 первым)
2026-09-29 15:28:50 +00:00

45 lines
1.3 KiB
Python

"""Private async execution utilities."""
import asyncio
import contextvars
from collections.abc import Awaitable, Callable
from concurrent.futures import ThreadPoolExecutor
from typing import TypeVar
T = TypeVar("T")
_RUN_ASYNC_BRIDGE = contextvars.ContextVar("strands_run_async_bridge", default=False)
def is_run_async_bridge() -> bool:
"""Return whether execution is inside the temporary sync bridge loop."""
return _RUN_ASYNC_BRIDGE.get()
def run_async(async_func: Callable[[], Awaitable[T]]) -> T:
"""Run an async function in a separate thread to avoid event loop conflicts.
This utility handles the common pattern of running async code from sync contexts
by using ThreadPoolExecutor to isolate the async execution.
Args:
async_func: A callable that returns an awaitable
Returns:
The result of the async function
"""
async def execute_async() -> T:
return await async_func()
def execute() -> T:
token = _RUN_ASYNC_BRIDGE.set(True)
try:
return asyncio.run(execute_async())
finally:
_RUN_ASYNC_BRIDGE.reset(token)
with ThreadPoolExecutor() as executor:
context = contextvars.copy_context()
future = executor.submit(context.run, execute)
return future.result()