bpo-34075: Deprecate non-ThreadPoolExecutor in loop.set_default_execu… · python/cpython@22d2508 (original) (raw)

5 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -906,7 +906,14 @@ pool of processes). By default, an event loop uses a thread pool executor
906 906
907 907 .. method:: AbstractEventLoop.set_default_executor(executor)
908 908
909 - Set the default executor used by :meth:`run_in_executor`.
909 + Set *executor* as the default executor used by :meth:`run_in_executor`.
910 + *executor* should be an instance of
911 +:class:`~concurrent.futures.ThreadPoolExecutor`.
912 +
913 + .. deprecated:: 3.8
914 + Using an executor that is not an instance of
915 +:class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
916 + will trigger an error in Python 3.9.
910 917
911 918
912 919 Error Handling API
Original file line number Diff line number Diff line change
@@ -164,6 +164,12 @@ Deprecated
164 164 They will be removed in Python 3.9.
165 165 (Contributed by Serhiy Storchaka in :issue:`29209`.)
166 166
167 +* Passing an object that is not an instance of
168 +:class:`concurrent.futures.ThreadPoolExecutor` to
169 +:meth:`asyncio.AbstractEventLoop.set_default_executor()` is
170 + deprecated and will be prohibited in Python 3.9.
171 + (Contributed by Elvis Pranskevichus in :issue:`34075`.)
172 +
167 173
168 174 Removed
169 175 =======
Original file line number Diff line number Diff line change
@@ -741,6 +741,12 @@ def run_in_executor(self, executor, func, *args):
741 741 executor.submit(func, *args), loop=self)
742 742
743 743 def set_default_executor(self, executor):
744 +if not isinstance(executor, concurrent.futures.ThreadPoolExecutor):
745 +warnings.warn(
746 +'Using the default executor that is not an instance of '
747 +'ThreadPoolExecutor is deprecated and will be prohibited '
748 +'in Python 3.9',
749 +DeprecationWarning, 2)
744 750 self._default_executor = executor
745 751
746 752 def _getaddrinfo_debug(self, host, port, family, type, proto, flags):
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1 1 """Tests for base_events.py"""
2 2
3 +import concurrent.futures
3 4 import errno
4 5 import logging
5 6 import math
@@ -211,10 +212,21 @@ def test__add_callback_cancelled_handle(self):
211 212 self.assertFalse(self.loop._ready)
212 213
213 214 def test_set_default_executor(self):
214 -executor = mock.Mock()
215 +class DummyExecutor(concurrent.futures.ThreadPoolExecutor):
216 +def submit(self, fn, *args, **kwargs):
217 +raise NotImplementedError(
218 +'cannot submit into a dummy executor')
219 +
220 +executor = DummyExecutor()
215 221 self.loop.set_default_executor(executor)
216 222 self.assertIs(executor, self.loop._default_executor)
217 223
224 +def test_set_default_executor_deprecation_warnings(self):
225 +executor = mock.Mock()
226 +
227 +with self.assertWarns(DeprecationWarning):
228 +self.loop.set_default_executor(executor)
229 +
218 230 def test_call_soon(self):
219 231 def cb():
220 232 pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +Deprecate passing non-ThreadPoolExecutor instances to
2 +:meth:`AbstractEventLoop.set_default_executor`.