Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/art/local/backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import json
import math
import os
Expand Down Expand Up @@ -90,7 +91,26 @@ def __exit__(
exc: BaseException | None,
tb: TracebackType | None,
) -> None:
self._close()
try:
# If an event loop is running, force users to use the async context manager
asyncio.get_running_loop()
raise RuntimeError(
"LocalBackend used with 'with' inside a running event loop. Use 'async with LocalBackend()' instead."
)
except RuntimeError:
# No event loop running; safe to close synchronously
self._close()

async def __aenter__(self) -> Self:
return self

async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc: BaseException | None,
tb: TracebackType | None,
) -> None:
await self.close()

async def close(self) -> None:
"""
Expand Down