Skip to content

Commit be43f7c

Browse files
authored
Merge pull request #14 from Enapter/rnovatorov/dev
implement `Device.run_in_thread`
2 parents 0e86e53 + 7a20d04 commit be43f7c

10 files changed

Lines changed: 50 additions & 16 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
python: ["3.7", "3.8", "3.9", "3.10"]
15+
python: ["3.8", "3.9", "3.10", "3.11"]
1616
steps:
1717
- name: Checkout
1818
uses: actions/checkout@v2

enapter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.7.3"
1+
__version__ = "0.8.0"
22

33
from . import async_, log, mdns, mqtt, types, vucm
44

enapter/async_/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from .generator import generator
22
from .routine import Routine
3-
from .run_in_executor import run_in_executor
43

54
__all__ = [
65
"generator",
76
"Routine",
8-
"run_in_executor",
97
]

enapter/async_/run_in_executor.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

enapter/vucm/device.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import asyncio
2+
import concurrent
3+
import functools
24
import traceback
35
from typing import Optional, Set
46

@@ -7,12 +9,22 @@
79

810

911
class Device(async_.Routine):
10-
def __init__(self, channel, cmd_prefix="cmd_", task_prefix="task_") -> None:
12+
def __init__(
13+
self,
14+
channel,
15+
cmd_prefix="cmd_",
16+
task_prefix="task_",
17+
thread_pool_executor=None,
18+
) -> None:
1119
self.__channel = channel
1220

1321
self.__cmd_prefix = cmd_prefix
1422
self.__task_prefix = task_prefix
1523

24+
if thread_pool_executor is None:
25+
thread_pool_executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
26+
self.__thread_pool_executor = thread_pool_executor
27+
1628
self.log = Logger(channel=channel)
1729
self.alerts: Set[str] = set()
1830

@@ -34,7 +46,15 @@ async def send_properties(self, properties: Optional[types.JSON] = None) -> None
3446

3547
await self.__channel.publish_properties(properties)
3648

49+
async def run_in_thread(self, func, *args, **kwargs):
50+
loop = asyncio.get_running_loop()
51+
return await loop.run_in_executor(
52+
self.__thread_pool_executor, functools.partial(func, *args, **kwargs)
53+
)
54+
3755
async def _run(self):
56+
self._stack.enter_context(self.__thread_pool_executor)
57+
3858
tasks = set()
3959

4060
for name in dir(self):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
enapter==0.7.3
1+
enapter==0.8.0
22
psutil==5.9.4
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
enapter==0.7.3
1+
enapter==0.8.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
enapter==0.7.3
1+
enapter==0.8.0
22
python-weather==0.4.2
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
enapter==0.7.3
1+
enapter==0.8.0
22
python-miio==0.5.12

tests/unit/test_vucm.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import asyncio
2+
import contextlib
3+
4+
import enapter
5+
6+
7+
class TestDevice:
8+
async def test_run_in_thread(self, fake):
9+
async with enapter.vucm.Device(channel=MockChannel(fake)) as device:
10+
assert not device._Device__thread_pool_executor._shutdown
11+
assert await device.run_in_thread(lambda: 42) == 42
12+
assert device._Device__thread_pool_executor._shutdown
13+
14+
15+
class MockChannel:
16+
def __init__(self, fake):
17+
self.hardware_id = fake.hardware_id()
18+
self.channel_id = fake.channel_id()
19+
20+
@contextlib.asynccontextmanager
21+
async def subscribe_to_command_requests(self, *args, **kwargs):
22+
await asyncio.Event().wait()
23+
yield

0 commit comments

Comments
 (0)