Skip to content

Commit

Permalink
Merge pull request #198 from flacjacket/robust_no_async
Browse files Browse the repository at this point in the history
Allow amcrest camera to be setup without async running
  • Loading branch information
flacjacket authored Sep 17, 2021
2 parents 0098be1 + 1b175a7 commit 57ca4de
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/amcrest/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def __init__(
timeout_protocol: TimeoutT = None,
) -> None:
self._token_lock = threading.Lock()
self._async_token_lock = asyncio.Lock()
try:
self._async_token_lock: Optional[asyncio.Lock] = asyncio.Lock()
except RuntimeError:
self._async_token_lock = None
self._cmd_id_lock = threading.Lock()
self._cmd_id = 0
self._host = clean_url(host)
Expand Down Expand Up @@ -219,6 +222,8 @@ def command(self, *args, **kwargs) -> requests.Response:
return self._command(*args, **kwargs)

async def async_command(self, *args, **kwargs) -> httpx.Response:
if self._async_token_lock is None:
raise RuntimeError("Camera not setup with async loop running")
async with self._async_token_lock:
if not self._async_token:
await self._async_generate_token()
Expand All @@ -228,6 +233,8 @@ async def async_command(self, *args, **kwargs) -> httpx.Response:
async def async_stream_command(
self, *args, **kwargs
) -> AsyncIterator[httpx.Response]:
if self._async_token_lock is None:
raise RuntimeError("Camera not setup with async loop running")
async with self._async_token_lock:
if not self._async_token:
await self._async_generate_token()
Expand Down

0 comments on commit 57ca4de

Please sign in to comment.