Skip to content

Commit

Permalink
Update to 1.2.1
Browse files Browse the repository at this point in the history
- Fixes concurrency issue caused by an unresolved bug in Python
- Increases max message payload size to 16MB (from 8)
- Removes some unneeded code
  • Loading branch information
tt2468 committed Sep 30, 2021
1 parent 62e6f7a commit f34cb6a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name="simpleobsws",
version="1.2.0",
version="1.2.1",
author="tt2468",
author_email="[email protected]",
description="A simple obs-websocket library in async Python for people who just want JSON output.",
Expand Down
26 changes: 13 additions & 13 deletions simpleobsws.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ class EventRegistrationError(Exception):
class NotIdentifiedError(Exception):
pass

async def _wait_cond(cond):
async with cond:
await cond.wait()

async def _wait_for_cond(cond, func):
async with cond:
await cond.wait_for(func)

class WebSocketClient:
def __init__(self,
url: str = "ws://localhost:4444",
Expand Down Expand Up @@ -92,7 +100,7 @@ async def connect(self):
self.recv_task = None
self.identified = False
self.hello_message = None
self.ws = await websockets.connect(self.url, max_size=2**23)
self.ws = await websockets.connect(self.url, max_size=2**24)
self.recv_task = self.loop.create_task(self._ws_recv_task())
return True

Expand All @@ -101,12 +109,9 @@ async def wait_until_identified(self, timeout: int = 10):
log.debug('WebSocket session is not open. Returning early.')
return False
try:
async with self.cond:
await asyncio.wait_for(self.cond.wait_for(self.is_identified), timeout=timeout)
await asyncio.wait_for(_wait_for_cond(self.cond, self.is_identified), timeout=timeout)
return True
except asyncio.TimeoutError:
#if not self.ws.open:
# log.debug('WebSocket session is no longer open. Returning early.')
return False


Expand Down Expand Up @@ -141,8 +146,7 @@ async def call(self, request: Request, timeout: int = 15):
try:
self.waiters[request_id] = waiter
await self.ws.send(json.dumps(request_payload))
async with waiter.cond:
await asyncio.wait_for(waiter.cond.wait(), timeout=timeout)
await asyncio.wait_for(_wait_cond(waiter.cond), timeout=timeout)
except asyncio.TimeoutError:
raise MessageTimeout('The request with type {} timed out after {} seconds.'.format(request.requestType, timeout))
finally:
Expand Down Expand Up @@ -191,8 +195,7 @@ async def call_batch(self, requests: list, timeout: int = 15, halt_on_failure: b
try:
self.waiters[request_batch_id] = waiter
await self.ws.send(json.dumps(request_batch_payload))
async with waiter.cond:
await asyncio.wait_for(waiter.cond.wait(), timeout=timeout)
await asyncio.wait_for(_wait_cond(waiter.cond), timeout=timeout)
except asyncio.TimeoutError:
raise MessageTimeout('The request batch timed out after {} seconds.'.format(timeout))
finally:
Expand Down Expand Up @@ -272,7 +275,7 @@ async def _ws_recv_task(self):
while self.ws.open:
message = ''
try:
message = await asyncio.wait_for(self.ws.recv(), timeout=5)
message = await self.ws.recv()
if not message:
continue
incoming_payload = json.loads(message)
Expand Down Expand Up @@ -318,7 +321,4 @@ async def _ws_recv_task(self):
break
except json.JSONDecodeError:
continue
except asyncio.TimeoutError:
async with self.cond:
self.cond.notify_all()
self.identified = False

0 comments on commit f34cb6a

Please sign in to comment.