summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/asyncio/__init__.py2
-rw-r--r--Lib/asyncio/threads.py21
-rw-r--r--Lib/test/test_asyncio/test_threads.py79
3 files changed, 102 insertions, 0 deletions
diff --git a/Lib/asyncio/__init__.py b/Lib/asyncio/__init__.py
index 28c2e2c..eb84bfb 100644
--- a/Lib/asyncio/__init__.py
+++ b/Lib/asyncio/__init__.py
@@ -17,6 +17,7 @@ from .queues import *
from .streams import *
from .subprocess import *
from .tasks import *
+from .threads import *
from .transports import *
# Exposed for _asynciomodule.c to implement now deprecated
@@ -35,6 +36,7 @@ __all__ = (base_events.__all__ +
streams.__all__ +
subprocess.__all__ +
tasks.__all__ +
+ threads.__all__ +
transports.__all__)
if sys.platform == 'win32': # pragma: no cover
diff --git a/Lib/asyncio/threads.py b/Lib/asyncio/threads.py
new file mode 100644
index 0000000..2f40467
--- /dev/null
+++ b/Lib/asyncio/threads.py
@@ -0,0 +1,21 @@
+"""High-level support for working with threads in asyncio"""
+
+import functools
+
+from . import events
+
+
+__all__ = "to_thread",
+
+
+async def to_thread(func, /, *args, **kwargs):
+ """Asynchronously run function *func* in a separate thread.
+
+ Any *args and **kwargs supplied for this function are directly passed
+ to *func*.
+
+ Return an asyncio.Future which represents the eventual result of *func*.
+ """
+ loop = events.get_running_loop()
+ func_call = functools.partial(func, *args, **kwargs)
+ return await loop.run_in_executor(None, func_call)
diff --git a/Lib/test/test_asyncio/test_threads.py b/Lib/test/test_asyncio/test_threads.py
new file mode 100644
index 0000000..99a00f2
--- /dev/null
+++ b/Lib/test/test_asyncio/test_threads.py
@@ -0,0 +1,79 @@
+"""Tests for asyncio/threads.py"""
+
+import asyncio
+import unittest
+
+from unittest import mock
+from test.test_asyncio import utils as test_utils
+
+
+def tearDownModule():
+ asyncio.set_event_loop_policy(None)
+
+
+class ToThreadTests(test_utils.TestCase):
+ def setUp(self):
+ super().setUp()
+ self.loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(self.loop)
+
+ def tearDown(self):
+ self.loop.run_until_complete(
+ self.loop.shutdown_default_executor())
+ self.loop.close()
+ asyncio.set_event_loop(None)
+ self.loop = None
+ super().tearDown()
+
+ def test_to_thread(self):
+ async def main():
+ return await asyncio.to_thread(sum, [40, 2])
+
+ result = self.loop.run_until_complete(main())
+ self.assertEqual(result, 42)
+
+ def test_to_thread_exception(self):
+ def raise_runtime():
+ raise RuntimeError("test")
+
+ async def main():
+ await asyncio.to_thread(raise_runtime)
+
+ with self.assertRaisesRegex(RuntimeError, "test"):
+ self.loop.run_until_complete(main())
+
+ def test_to_thread_once(self):
+ func = mock.Mock()
+
+ async def main():
+ await asyncio.to_thread(func)
+
+ self.loop.run_until_complete(main())
+ func.assert_called_once()
+
+ def test_to_thread_concurrent(self):
+ func = mock.Mock()
+
+ async def main():
+ futs = []
+ for _ in range(10):
+ fut = asyncio.to_thread(func)
+ futs.append(fut)
+ await asyncio.gather(*futs)
+
+ self.loop.run_until_complete(main())
+ self.assertEqual(func.call_count, 10)
+
+ def test_to_thread_args_kwargs(self):
+ # Unlike run_in_executor(), to_thread() should directly accept kwargs.
+ func = mock.Mock()
+
+ async def main():
+ await asyncio.to_thread(func, 'test', something=True)
+
+ self.loop.run_until_complete(main())
+ func.assert_called_once_with('test', something=True)
+
+
+if __name__ == "__main__":
+ unittest.main()