Download backend/runtime/application/__init__.py from VoiceOfML/Search: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/backend/runtime/application/__init__.py
- Command line
-
hf download hf://spaces/VoiceOfML/Search/backend/runtime/application/__init__.py
-
curl -L -o __init__.py https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/backend/runtime/application/__init__.py
1.09 kB
| """Application resource coordination.""" | |
| import asyncio | |
| from collections.abc import Callable | |
| from typing import Generic, Protocol, TypeVar | |
| T = TypeVar("T") | |
| _Task = TypeVar("_Task", contravariant=True) | |
| class _WorkerRegistry(Protocol, Generic[_Task]): | |
| def add(self, task: _Task, /) -> None: ... | |
| def discard(self, task: _Task, /) -> None: ... | |
| async def run_admitted_worker(workers: Callable[[], _WorkerRegistry[asyncio.Task[T]]], | |
| semaphore: asyncio.Semaphore, | |
| compute: Callable[..., T], *args: object) -> T: | |
| """Track an admitted thread until it finishes, even if its waiter cancels.""" | |
| task = asyncio.create_task(asyncio.to_thread(compute, *args)) | |
| workers().add(task) | |
| def finished(done: asyncio.Task[T]) -> None: | |
| # Resolve the current registry, but return capacity to the admitting semaphore. | |
| workers().discard(done) | |
| semaphore.release() | |
| if not done.cancelled(): | |
| done.exception() | |
| task.add_done_callback(finished) | |
| return await asyncio.shield(task) | |