Skip to content

Commit

Permalink
handle timeout property
Browse files Browse the repository at this point in the history
  • Loading branch information
fate0 committed Sep 19, 2017
1 parent e2f11b9 commit 6130592
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aiochrome/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from .tab import Tab
from .exceptions import *

__version__ = '0.0.1'
__version__ = '0.0.2'
25 changes: 22 additions & 3 deletions aiochrome/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,34 @@ async def _send(self, message, timeout=None):
if self.debug: # pragma: no cover
print("SEND ► %s" % message_json)

if not isinstance(timeout, (int, float)) or timeout > 1:
q_timeout = 1
else:
q_timeout = timeout / 2.0

try:
queue = asyncio.Queue()
self.method_results[message['id']] = queue

# just raise the exception to user
await self._ws.send(message_json)
return await asyncio.wait_for(queue.get(), timeout)
except asyncio.TimeoutError:
raise TimeoutException("Calling {} timeout".format(message['method']))

while not self._stopped.is_set():
try:
if isinstance(timeout, (int, float)):
if timeout < q_timeout:
q_timeout = timeout

timeout -= q_timeout

return await asyncio.wait_for(queue.get(), q_timeout)
except asyncio.TimeoutError:
if isinstance(timeout, (int, float)) and timeout <= 0:
raise TimeoutException("Calling %s timeout" % message['method'])

continue

raise UserAbortException("User abort, call stop() when calling %s" % message['method'])
finally:
self.method_results.pop(message['id'])

Expand Down

0 comments on commit 6130592

Please sign in to comment.