Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Doc/library/asyncio-eventloop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,14 @@ pool of processes). By default, an event loop uses a thread pool executor

.. method:: AbstractEventLoop.set_default_executor(executor)

Set the default executor used by :meth:`run_in_executor`.
Set *executor* as the default executor used by :meth:`run_in_executor`.
*executor* should be an instance of
:class:`~concurrent.futures.ThreadPoolExecutor`.

.. deprecated:: 3.8
Using an executor that is not an instance of
:class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
will trigger an error in Python 3.9.


Error Handling API
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ Deprecated
They will be removed in Python 3.9.
(Contributed by Serhiy Storchaka in :issue:`29209`.)

* Passing an object that is not an instance of
:class:`concurrent.futures.ThreadPoolExecutor` to
:meth:`asyncio.AbstractEventLoop.set_default_executor()` is
deprecated and will be prohibited in Python 3.9.
(Contributed by Elvis Pranskevichus in :issue:`34075`.)


Removed
=======
Expand Down
6 changes: 6 additions & 0 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,12 @@ def run_in_executor(self, executor, func, *args):
executor.submit(func, *args), loop=self)

def set_default_executor(self, executor):
if not isinstance(executor, concurrent.futures.ThreadPoolExecutor):
warnings.warn(
'Using the default executor that is not an instance of '
'ThreadPoolExecutor is deprecated and will be prohibited '
'in Python 3.9',
DeprecationWarning, 2)
self._default_executor = executor

def _getaddrinfo_debug(self, host, port, family, type, proto, flags):
Expand Down
14 changes: 13 additions & 1 deletion Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for base_events.py"""

import concurrent.futures
import errno
import logging
import math
Expand Down Expand Up @@ -211,10 +212,21 @@ def test__add_callback_cancelled_handle(self):
self.assertFalse(self.loop._ready)

def test_set_default_executor(self):
executor = mock.Mock()
class DummyExecutor(concurrent.futures.ThreadPoolExecutor):
def submit(self, fn, *args, **kwargs):
raise NotImplementedError(
'cannot submit into a dummy executor')

executor = DummyExecutor()
self.loop.set_default_executor(executor)
self.assertIs(executor, self.loop._default_executor)

def test_set_default_executor_deprecation_warnings(self):
executor = mock.Mock()

with self.assertWarns(DeprecationWarning):
self.loop.set_default_executor(executor)

def test_call_soon(self):
def cb():
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate passing non-ThreadPoolExecutor instances to
:meth:`AbstractEventLoop.set_default_executor`.