Skip to content

Commit

Permalink
Add run_in_executor function to aiomisc/thread_pool.py
Browse files Browse the repository at this point in the history
  • Loading branch information
leenr committed Oct 16, 2018
1 parent b5524fa commit 0be744f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions aiomisc/thread_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def __del__(self):
self.shutdown()


async def run_in_executor(func, *args, **kwargs):
loop = asyncio.get_event_loop()

return await loop.run_in_executor(
None, partial(func, *args, **kwargs)
)


def threaded(func):
@wraps(func)
async def wrap(*args, **kwargs):
Expand Down
19 changes: 18 additions & 1 deletion tests/test_thread_pool.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
from contextlib import suppress
from functools import partial

import pytest
import time

from aiomisc.thread_pool import ThreadPoolExecutor, threaded
from aiomisc.thread_pool import ThreadPoolExecutor, run_in_executor, threaded


@pytest.fixture
Expand Down Expand Up @@ -36,6 +37,22 @@ async def test_threaded(executor: ThreadPoolExecutor, timer):
)


@pytest.mark.asyncio
async def test_run_in_executor(executor: ThreadPoolExecutor, timer):
assert executor

sleep = partial(run_in_executor, time.sleep)

with timer(1):
await asyncio.gather(
sleep(1),
sleep(1),
sleep(1),
sleep(1),
sleep(1),
)


@pytest.mark.asyncio
async def test_threaded_exc(executor: ThreadPoolExecutor):
assert executor
Expand Down

0 comments on commit 0be744f

Please sign in to comment.