Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22d2508

Browse files
elpransvstinner
authored andcommitted
bpo-34075: Deprecate non-ThreadPoolExecutor in loop.set_default_executor() (GH-8533)
Various asyncio internals expect that the default executor is a `ThreadPoolExecutor`, so deprecate passing anything else to `loop.set_default_executor()`.
1 parent 4e11c46 commit 22d2508

5 files changed

Lines changed: 35 additions & 2 deletions

File tree

Doc/library/asyncio-eventloop.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,14 @@ pool of processes). By default, an event loop uses a thread pool executor
906906

907907
.. method:: AbstractEventLoop.set_default_executor(executor)
908908

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.
910917

911918

912919
Error Handling API

Doc/whatsnew/3.8.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ Deprecated
164164
They will be removed in Python 3.9.
165165
(Contributed by Serhiy Storchaka in :issue:`29209`.)
166166

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+
167173

168174
Removed
169175
=======

Lib/asyncio/base_events.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,12 @@ def run_in_executor(self, executor, func, *args):
741741
executor.submit(func, *args), loop=self)
742742

743743
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)
744750
self._default_executor = executor
745751

746752
def _getaddrinfo_debug(self, host, port, family, type, proto, flags):

Lib/test/test_asyncio/test_base_events.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for base_events.py"""
22

3+
import concurrent.futures
34
import errno
45
import logging
56
import math
@@ -211,10 +212,21 @@ def test__add_callback_cancelled_handle(self):
211212
self.assertFalse(self.loop._ready)
212213

213214
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()
215221
self.loop.set_default_executor(executor)
216222
self.assertIs(executor, self.loop._default_executor)
217223

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+
218230
def test_call_soon(self):
219231
def cb():
220232
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate passing non-ThreadPoolExecutor instances to
2+
:meth:`AbstractEventLoop.set_default_executor`.

0 commit comments

Comments
 (0)