Skip to content

bpo-36373: Deprecate explicit loop in task and subprocess API #16033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 12, 2019
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
8 changes: 8 additions & 0 deletions Doc/library/asyncio-subprocess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Creating Subprocesses
See the documentation of :meth:`loop.subprocess_exec` for other
parameters.

.. deprecated-removed:: 3.8 3.10

The *loop* parameter.

.. coroutinefunction:: create_subprocess_shell(cmd, stdin=None, \
stdout=None, stderr=None, loop=None, \
limit=None, \*\*kwds)
Expand All @@ -95,6 +99,10 @@ Creating Subprocesses
escape whitespace and special shell characters in strings that are going
to be used to construct shell commands.

.. deprecated-removed:: 3.8 3.10

The *loop* parameter.

.. note::

The default asyncio event loop implementation on **Windows** does not
Expand Down
33 changes: 23 additions & 10 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ Running Tasks Concurrently
cancellation of one submitted Task/Future to cause other
Tasks/Futures to be cancelled.

.. deprecated-removed:: 3.8 3.10
The *loop* parameter.

.. _asyncio_example_gather:

Example::
Expand Down Expand Up @@ -411,6 +414,9 @@ Shielding From Cancellation
except CancelledError:
res = None

.. deprecated-removed:: 3.8 3.10
The *loop* parameter.


Timeouts
========
Expand Down Expand Up @@ -478,22 +484,12 @@ Waiting Primitives
set concurrently and block until the condition specified
by *return_when*.

.. deprecated:: 3.8

If any awaitable in *aws* is a coroutine, it is automatically
scheduled as a Task. Passing coroutines objects to
``wait()`` directly is deprecated as it leads to
:ref:`confusing behavior <asyncio_example_wait_coroutine>`.

Returns two sets of Tasks/Futures: ``(done, pending)``.

Usage::

done, pending = await asyncio.wait(aws)

.. deprecated-removed:: 3.8 3.10
The *loop* parameter.

*timeout* (a float or int), if specified, can be used to control
the maximum number of seconds to wait before returning.

Expand Down Expand Up @@ -525,6 +521,17 @@ Waiting Primitives
Unlike :func:`~asyncio.wait_for`, ``wait()`` does not cancel the
futures when a timeout occurs.

.. deprecated:: 3.8

If any awaitable in *aws* is a coroutine, it is automatically
scheduled as a Task. Passing coroutines objects to
``wait()`` directly is deprecated as it leads to
:ref:`confusing behavior <asyncio_example_wait_coroutine>`.

.. deprecated-removed:: 3.8 3.10

The *loop* parameter.

.. _asyncio_example_wait_coroutine:
.. note::

Expand Down Expand Up @@ -568,6 +575,9 @@ Waiting Primitives
Raises :exc:`asyncio.TimeoutError` if the timeout occurs before
all Futures are done.

.. deprecated-removed:: 3.8 3.10
The *loop* parameter.

Example::

for f in as_completed(aws):
Expand Down Expand Up @@ -694,6 +704,9 @@ Task Object
.. versionchanged:: 3.8
Added the ``name`` parameter.

.. deprecated-removed:: 3.8 3.10
The *loop* parameter.

.. method:: cancel()

Request the Task to be cancelled.
Expand Down
13 changes: 13 additions & 0 deletions Lib/asyncio/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ async def create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None,
**kwds):
if loop is None:
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8 "
"and scheduled for removal in Python 3.10.",
DeprecationWarning,
stacklevel=2
)

protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
loop=loop,
_asyncio_internal=True)
Expand All @@ -239,6 +246,12 @@ async def create_subprocess_exec(program, *args, stdin=None, stdout=None,
limit=streams._DEFAULT_LIMIT, **kwds):
if loop is None:
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8 "
"and scheduled for removal in Python 3.10.",
DeprecationWarning,
stacklevel=2
)
protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
loop=loop,
_asyncio_internal=True)
Expand Down
19 changes: 17 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,17 @@ def as_completed(fs, *, loop=None, timeout=None):
"""
if futures.isfuture(fs) or coroutines.iscoroutine(fs):
raise TypeError(f"expect a list of futures, not {type(fs).__name__}")
loop = loop if loop is not None else events.get_event_loop()
todo = {ensure_future(f, loop=loop) for f in set(fs)}

from .queues import Queue # Import here to avoid circular import problem.
done = Queue(loop=loop)

if loop is None:
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
todo = {ensure_future(f, loop=loop) for f in set(fs)}
timeout_handle = None

def _on_timeout():
Expand Down Expand Up @@ -733,6 +740,10 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
if not coros_or_futures:
if loop is None:
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
outer = loop.create_future()
outer.set_result([])
return outer
Expand Down Expand Up @@ -842,6 +853,10 @@ def shield(arg, *, loop=None):
except CancelledError:
res = None
"""
if loop is not None:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
inner = ensure_future(arg, loop=loop)
if inner.done():
# Shortcut.
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,9 +855,10 @@ def test_read_all_from_pipe_reader(self):
watcher.attach_loop(self.loop)
try:
asyncio.set_child_watcher(watcher)
create = asyncio.create_subprocess_exec(*args,
pass_fds={wfd},
loop=self.loop)
create = asyncio.create_subprocess_exec(
*args,
pass_fds={wfd},
)
proc = self.loop.run_until_complete(create)
self.loop.run_until_complete(proc.wait())
finally:
Expand Down
Loading