Skip to content

Commit

Permalink
Switch usage of asyncio.wait_for to async_timeout (#337)
Browse files Browse the repository at this point in the history
* Switch usage of asyncio.wait_for to async_timeout

`asyncio.wait_for` creates another tasks which leads to some race conditions in cancelation and a performance hit

cpython 3.12 will change the underlying implementation of `asyncio.wait_for` to use `asyncio.wait` but that is still a long way off for many people:

python/cpython#98518

* adjust ci
  • Loading branch information
bdraco authored Sep 1, 2023
1 parent 0994182 commit a7b1e51
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
if python --version 2>&1 | grep -q "Python 2"; then pip install mock rsa==4.0; fi
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install .
if python --version 2>&1 | grep -q "Python 3.7" || python --version 2>&1 | grep -q "Python 3.8" || python --version 2>&1 | grep -q "Python 3.9" || python --version 2>&1 | grep -q "Python 3.10"; then pip install aiofiles adb-shell[usb]; fi
if python --version 2>&1 | grep -q "Python 3.7" || python --version 2>&1 | grep -q "Python 3.8" || python --version 2>&1 | grep -q "Python 3.9" || python --version 2>&1 | grep -q "Python 3.10"; then pip install aiofiles async_timeout adb-shell[usb]; fi
- name: Check formatting with black
run: |
if python --version 2>&1 | grep -q "Python 3.7" || python --version 2>&1 | grep -q "Python 3.8" || python --version 2>&1 | grep -q "Python 3.9" || python --version 2>&1 | grep -q "Python 3.10"; then pip install black && make lint-black; fi
Expand Down
4 changes: 3 additions & 1 deletion androidtv/adb_manager/adb_manager_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from adb_shell.auth.sign_pythonrsa import PythonRSASigner
from adb_shell.constants import DEFAULT_PUSH_MODE, DEFAULT_READ_TIMEOUT_S
import aiofiles
import async_timeout
from ppadb.client import Client

from ..constants import (
Expand Down Expand Up @@ -164,7 +165,8 @@ async def _acquire(lock, timeout=DEFAULT_LOCK_TIMEOUT_S):
try:
acquired = False
try:
acquired = await asyncio.wait_for(lock.acquire(), timeout)
async with async_timeout.timeout(timeout):
acquired = await lock.acquire()
if not acquired:
raise LockNotAcquiredException
yield acquired
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
author_email="[email protected]",
packages=["androidtv", "androidtv.adb_manager", "androidtv.basetv", "androidtv.androidtv", "androidtv.firetv"],
install_requires=["adb-shell>=0.4.0", "pure-python-adb>=0.3.0.dev0"],
extras_require={"async": ["aiofiles>=0.4.0"], "usb": ["adb-shell[usb]>=0.4.0"]},
extras_require={"async": ["aiofiles>=0.4.0", "async_timeout>=3.0.0"], "usb": ["adb-shell[usb]>=0.4.0"]},
classifiers=[
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
Expand Down

0 comments on commit a7b1e51

Please sign in to comment.