2014-02-02 17:43:39 -04:00
|
|
|
.. currentmodule:: asyncio
|
|
|
|
|
2014-07-11 07:13:39 -03:00
|
|
|
.. _asyncio-subprocess:
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
============
|
|
|
|
Subprocesses
|
|
|
|
============
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2019-10-10 20:18:46 -03:00
|
|
|
**Source code:** :source:`Lib/asyncio/subprocess.py`,
|
|
|
|
:source:`Lib/asyncio/base_subprocess.py`
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
This section describes high-level async/await asyncio APIs to
|
|
|
|
create and manage subprocesses.
|
2017-07-25 20:03:51 -03:00
|
|
|
|
2018-09-14 19:11:24 -03:00
|
|
|
.. _asyncio_example_subprocess_shell:
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Here's an example of how asyncio can run a shell command and
|
2018-09-17 20:16:44 -03:00
|
|
|
obtain its result::
|
2014-07-08 18:42:38 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
import asyncio
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
async def run(cmd):
|
|
|
|
proc = await asyncio.create_subprocess_shell(
|
|
|
|
cmd,
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.PIPE)
|
2014-10-13 19:52:07 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
stdout, stderr = await proc.communicate()
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
print(f'[{cmd!r} exited with {proc.returncode}]')
|
|
|
|
if stdout:
|
|
|
|
print(f'[stdout]\n{stdout.decode()}')
|
|
|
|
if stderr:
|
|
|
|
print(f'[stderr]\n{stderr.decode()}')
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
asyncio.run(run('ls /zzz'))
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
will print::
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
['ls /zzz' exited with 1]
|
|
|
|
[stderr]
|
|
|
|
ls: /zzz: No such file or directory
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Because all asyncio subprocess functions are asynchronous and asyncio
|
|
|
|
provides many tools to work with such functions, it is easy to execute
|
|
|
|
and monitor multiple subprocesses in parallel. It is indeed trivial
|
2018-09-17 20:16:44 -03:00
|
|
|
to modify the above example to run several commands simultaneously::
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
async def main():
|
|
|
|
await asyncio.gather(
|
|
|
|
run('ls /zzz'),
|
|
|
|
run('sleep 1; echo "hello"'))
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
asyncio.run(main())
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
See also the `Examples`_ subsection.
|
2014-10-13 19:02:10 -03:00
|
|
|
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Creating Subprocesses
|
|
|
|
=====================
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2020-12-16 21:37:28 -04:00
|
|
|
.. coroutinefunction:: create_subprocess_exec(program, *args, stdin=None, \
|
|
|
|
stdout=None, stderr=None, limit=None, **kwds)
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Create a subprocess.
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
The *limit* argument sets the buffer limit for :class:`StreamReader`
|
|
|
|
wrappers for :attr:`Process.stdout` and :attr:`Process.stderr`
|
2023-07-21 06:34:30 -03:00
|
|
|
(if :const:`subprocess.PIPE` is passed to *stdout* and *stderr* arguments).
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Return a :class:`~asyncio.subprocess.Process` instance.
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
See the documentation of :meth:`loop.subprocess_exec` for other
|
|
|
|
parameters.
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2022-02-18 04:58:48 -04:00
|
|
|
.. versionchanged:: 3.10
|
|
|
|
Removed the *loop* parameter.
|
2021-05-26 18:59:34 -03:00
|
|
|
|
2019-09-12 09:40:40 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
.. coroutinefunction:: create_subprocess_shell(cmd, stdin=None, \
|
2020-12-16 21:37:28 -04:00
|
|
|
stdout=None, stderr=None, limit=None, **kwds)
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
Run the *cmd* shell command.
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
The *limit* argument sets the buffer limit for :class:`StreamReader`
|
|
|
|
wrappers for :attr:`Process.stdout` and :attr:`Process.stderr`
|
2023-07-21 06:34:30 -03:00
|
|
|
(if :const:`subprocess.PIPE` is passed to *stdout* and *stderr* arguments).
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Return a :class:`~asyncio.subprocess.Process` instance.
|
2014-10-13 19:02:10 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
See the documentation of :meth:`loop.subprocess_shell` for other
|
|
|
|
parameters.
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2020-05-26 01:08:40 -03:00
|
|
|
.. important::
|
|
|
|
|
|
|
|
It is the application's responsibility to ensure that all whitespace and
|
|
|
|
special characters are quoted appropriately to avoid `shell injection
|
|
|
|
<https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
|
|
|
|
vulnerabilities. The :func:`shlex.quote` function can be used to properly
|
|
|
|
escape whitespace and special shell characters in strings that are going
|
|
|
|
to be used to construct shell commands.
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2022-02-18 04:58:48 -04:00
|
|
|
.. versionchanged:: 3.10
|
|
|
|
Removed the *loop* parameter.
|
2021-05-26 18:59:34 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
.. note::
|
|
|
|
|
2020-10-19 19:08:34 -03:00
|
|
|
Subprocesses are available for Windows if a :class:`ProactorEventLoop` is
|
|
|
|
used. See :ref:`Subprocess Support on Windows <asyncio-windows-subprocess>`
|
2018-09-11 13:54:40 -03:00
|
|
|
for details.
|
2014-03-25 05:40:26 -03:00
|
|
|
|
2014-02-02 17:43:39 -04:00
|
|
|
.. seealso::
|
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
asyncio also has the following *low-level* APIs to work with subprocesses:
|
2018-09-11 13:54:40 -03:00
|
|
|
:meth:`loop.subprocess_exec`, :meth:`loop.subprocess_shell`,
|
|
|
|
:meth:`loop.connect_read_pipe`, :meth:`loop.connect_write_pipe`,
|
|
|
|
as well as the :ref:`Subprocess Transports <asyncio-subprocess-transports>`
|
|
|
|
and :ref:`Subprocess Protocols <asyncio-subprocess-protocols>`.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
Constants
|
2018-09-11 13:54:40 -03:00
|
|
|
=========
|
2014-02-02 17:43:39 -04:00
|
|
|
|
|
|
|
.. data:: asyncio.subprocess.PIPE
|
2022-05-19 12:34:58 -03:00
|
|
|
:module:
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Can be passed to the *stdin*, *stdout* or *stderr* parameters.
|
|
|
|
|
|
|
|
If *PIPE* is passed to *stdin* argument, the
|
|
|
|
:attr:`Process.stdin <asyncio.subprocess.Process.stdin>` attribute
|
|
|
|
will point to a :class:`StreamWriter` instance.
|
|
|
|
|
|
|
|
If *PIPE* is passed to *stdout* or *stderr* arguments, the
|
|
|
|
:attr:`Process.stdout <asyncio.subprocess.Process.stdout>` and
|
|
|
|
:attr:`Process.stderr <asyncio.subprocess.Process.stderr>`
|
|
|
|
attributes will point to :class:`StreamReader` instances.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
|
|
|
.. data:: asyncio.subprocess.STDOUT
|
2022-05-19 12:34:58 -03:00
|
|
|
:module:
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
Special value that can be used as the *stderr* argument and indicates
|
|
|
|
that standard error should be redirected into standard output.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
|
|
|
.. data:: asyncio.subprocess.DEVNULL
|
2022-05-19 12:34:58 -03:00
|
|
|
:module:
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
Special value that can be used as the *stdin*, *stdout* or *stderr* argument
|
|
|
|
to process creation functions. It indicates that the special file
|
|
|
|
:data:`os.devnull` will be used for the corresponding subprocess stream.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Interacting with Subprocesses
|
|
|
|
=============================
|
|
|
|
|
|
|
|
Both :func:`create_subprocess_exec` and :func:`create_subprocess_shell`
|
2018-09-17 20:16:44 -03:00
|
|
|
functions return instances of the *Process* class. *Process* is a high-level
|
|
|
|
wrapper that allows communicating with subprocesses and watching for
|
|
|
|
their completion.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
|
|
|
.. class:: asyncio.subprocess.Process
|
2022-05-19 12:34:58 -03:00
|
|
|
:module:
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
An object that wraps OS processes created by the
|
|
|
|
:func:`create_subprocess_exec` and :func:`create_subprocess_shell`
|
|
|
|
functions.
|
|
|
|
|
|
|
|
This class is designed to have a similar API to the
|
|
|
|
:class:`subprocess.Popen` class, but there are some
|
|
|
|
notable differences:
|
|
|
|
|
|
|
|
* unlike Popen, Process instances do not have an equivalent to
|
|
|
|
the :meth:`~subprocess.Popen.poll` method;
|
|
|
|
|
|
|
|
* the :meth:`~asyncio.subprocess.Process.communicate` and
|
2018-09-17 20:16:44 -03:00
|
|
|
:meth:`~asyncio.subprocess.Process.wait` methods don't have a
|
2022-11-08 12:26:06 -04:00
|
|
|
*timeout* parameter: use the :func:`~asyncio.wait_for` function;
|
2018-09-11 13:54:40 -03:00
|
|
|
|
|
|
|
* the :meth:`Process.wait() <asyncio.subprocess.Process.wait>` method
|
|
|
|
is asynchronous, whereas :meth:`subprocess.Popen.wait` method
|
|
|
|
is implemented as a blocking busy loop;
|
2014-02-03 18:08:14 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
* the *universal_newlines* parameter is not supported.
|
2014-02-03 18:08:14 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
This class is :ref:`not thread safe <asyncio-multithreading>`.
|
2014-02-03 18:08:14 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
See also the :ref:`Subprocess and Threads <asyncio-subprocess-threads>`
|
|
|
|
section.
|
2015-02-25 09:24:15 -04:00
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: wait()
|
2014-02-03 18:08:14 -04:00
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
Wait for the child process to terminate.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Set and return the :attr:`returncode` attribute.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2014-10-13 19:52:07 -03:00
|
|
|
.. note::
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
This method can deadlock when using ``stdout=PIPE`` or
|
|
|
|
``stderr=PIPE`` and the child process generates so much output
|
|
|
|
that it blocks waiting for the OS pipe buffer to accept
|
|
|
|
more data. Use the :meth:`communicate` method when using pipes
|
|
|
|
to avoid this condition.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: communicate(input=None)
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Interact with process:
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
1. send data to *stdin* (if *input* is not ``None``);
|
2023-04-27 21:30:26 -03:00
|
|
|
2. closes *stdin*;
|
|
|
|
3. read data from *stdout* and *stderr*, until EOF is reached;
|
|
|
|
4. wait for process to terminate.
|
2014-10-13 19:52:07 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
The optional *input* argument is the data (:class:`bytes` object)
|
|
|
|
that will be sent to the child process.
|
2014-07-17 07:25:27 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Return a tuple ``(stdout_data, stderr_data)``.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
If either :exc:`BrokenPipeError` or :exc:`ConnectionResetError`
|
|
|
|
exception is raised when writing *input* into *stdin*, the
|
|
|
|
exception is ignored. This condition occurs when the process
|
|
|
|
exits before all data are written into *stdin*.
|
2014-10-13 19:52:07 -03:00
|
|
|
|
2018-09-13 22:28:19 -03:00
|
|
|
If it is desired to send data to the process' *stdin*,
|
2018-09-11 13:54:40 -03:00
|
|
|
the process needs to be created with ``stdin=PIPE``. Similarly,
|
|
|
|
to get anything other than ``None`` in the result tuple, the
|
|
|
|
process has to be created with ``stdout=PIPE`` and/or
|
|
|
|
``stderr=PIPE`` arguments.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Note, that the data read is buffered in memory, so do not use
|
|
|
|
this method if the data size is large or unlimited.
|
2014-07-17 07:25:27 -03:00
|
|
|
|
2023-04-27 21:30:26 -03:00
|
|
|
.. versionchanged:: 3.12
|
|
|
|
|
|
|
|
*stdin* gets closed when `input=None` too.
|
|
|
|
|
2014-02-08 20:36:14 -04:00
|
|
|
.. method:: send_signal(signal)
|
2014-02-02 17:43:39 -04:00
|
|
|
|
|
|
|
Sends the signal *signal* to the child process.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
On Windows, :py:data:`SIGTERM` is an alias for :meth:`terminate`.
|
|
|
|
``CTRL_C_EVENT`` and ``CTRL_BREAK_EVENT`` can be sent to processes
|
|
|
|
started with a *creationflags* parameter which includes
|
|
|
|
``CREATE_NEW_PROCESS_GROUP``.
|
|
|
|
|
|
|
|
.. method:: terminate()
|
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
Stop the child process.
|
2018-09-11 13:54:40 -03:00
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
On POSIX systems this method sends :py:data:`signal.SIGTERM` to the
|
2018-09-11 13:54:40 -03:00
|
|
|
child process.
|
|
|
|
|
|
|
|
On Windows the Win32 API function :c:func:`TerminateProcess` is
|
|
|
|
called to stop the child process.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2014-10-13 19:52:07 -03:00
|
|
|
.. method:: kill()
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2021-04-26 01:38:16 -03:00
|
|
|
Kill the child process.
|
2018-09-11 13:54:40 -03:00
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
On POSIX systems this method sends :py:data:`SIGKILL` to the child
|
2018-09-11 13:54:40 -03:00
|
|
|
process.
|
|
|
|
|
|
|
|
On Windows this method is an alias for :meth:`terminate`.
|
2014-02-02 17:43:39 -04:00
|
|
|
|
2014-10-13 19:52:07 -03:00
|
|
|
.. attribute:: stdin
|
2014-03-16 17:29:31 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Standard input stream (:class:`StreamWriter`) or ``None``
|
|
|
|
if the process was created with ``stdin=None``.
|
2014-02-03 18:26:28 -04:00
|
|
|
|
2014-10-13 19:52:07 -03:00
|
|
|
.. attribute:: stdout
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Standard output stream (:class:`StreamReader`) or ``None``
|
|
|
|
if the process was created with ``stdout=None``.
|
2014-10-13 19:52:07 -03:00
|
|
|
|
|
|
|
.. attribute:: stderr
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Standard error stream (:class:`StreamReader`) or ``None``
|
|
|
|
if the process was created with ``stderr=None``.
|
2014-10-13 19:52:07 -03:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Use the :meth:`communicate` method rather than
|
|
|
|
:attr:`process.stdin.write() <stdin>`,
|
|
|
|
:attr:`await process.stdout.read() <stdout>` or
|
2022-05-02 12:01:52 -03:00
|
|
|
:attr:`await process.stderr.read() <stderr>`.
|
2018-09-13 22:28:19 -03:00
|
|
|
This avoids deadlocks due to streams pausing reading or writing
|
2018-09-11 13:54:40 -03:00
|
|
|
and blocking the child process.
|
2014-10-13 19:52:07 -03:00
|
|
|
|
|
|
|
.. attribute:: pid
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Process identification number (PID).
|
2014-10-13 19:52:07 -03:00
|
|
|
|
|
|
|
Note that for processes created by the :func:`create_subprocess_shell`
|
2018-09-11 13:54:40 -03:00
|
|
|
function, this attribute is the PID of the spawned shell.
|
2014-10-13 19:52:07 -03:00
|
|
|
|
|
|
|
.. attribute:: returncode
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Return code of the process when it exits.
|
|
|
|
|
|
|
|
A ``None`` value indicates that the process has not terminated yet.
|
2014-10-13 19:52:07 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
A negative value ``-N`` indicates that the child was terminated
|
2018-09-17 20:16:44 -03:00
|
|
|
by signal ``N`` (POSIX only).
|
2014-10-13 19:52:07 -03:00
|
|
|
|
|
|
|
|
2015-01-08 20:32:02 -04:00
|
|
|
.. _asyncio-subprocess-threads:
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Subprocess and Threads
|
2015-09-02 10:39:01 -03:00
|
|
|
----------------------
|
2015-01-08 20:32:02 -04:00
|
|
|
|
2019-06-30 06:54:59 -03:00
|
|
|
Standard asyncio event loop supports running subprocesses from different threads by
|
|
|
|
default.
|
2015-01-08 20:32:02 -04:00
|
|
|
|
2019-06-30 06:54:59 -03:00
|
|
|
On Windows subprocesses are provided by :class:`ProactorEventLoop` only (default),
|
|
|
|
:class:`SelectorEventLoop` has no subprocess support.
|
2015-01-08 20:32:02 -04:00
|
|
|
|
2019-06-30 06:54:59 -03:00
|
|
|
On UNIX *child watchers* are used for subprocess finish waiting, see
|
|
|
|
:ref:`asyncio-watchers` for more info.
|
2018-09-11 13:54:40 -03:00
|
|
|
|
2019-06-30 06:54:59 -03:00
|
|
|
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
|
|
|
UNIX switched to use :class:`ThreadedChildWatcher` for spawning subprocesses from
|
|
|
|
different threads without any limitation.
|
|
|
|
|
|
|
|
Spawning a subprocess with *inactive* current child watcher raises
|
|
|
|
:exc:`RuntimeError`.
|
|
|
|
|
|
|
|
Note that alternative event loop implementations might have own limitations;
|
|
|
|
please refer to their documentation.
|
2015-02-25 09:24:15 -04:00
|
|
|
|
2015-01-08 20:32:02 -04:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
The :ref:`Concurrency and multithreading in asyncio
|
|
|
|
<asyncio-multithreading>` section.
|
|
|
|
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
Examples
|
|
|
|
--------
|
2014-10-13 19:52:07 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
An example using the :class:`~asyncio.subprocess.Process` class to
|
|
|
|
control a subprocess and the :class:`StreamReader` class to read from
|
2018-09-17 20:16:44 -03:00
|
|
|
its standard output.
|
2014-10-13 19:52:07 -03:00
|
|
|
|
2018-09-17 16:35:24 -03:00
|
|
|
.. _asyncio_example_create_subprocess_exec:
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
The subprocess is created by the :func:`create_subprocess_exec`
|
2014-10-13 19:52:07 -03:00
|
|
|
function::
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
import asyncio
|
2014-10-13 19:52:07 -03:00
|
|
|
import sys
|
|
|
|
|
2018-08-07 17:29:06 -03:00
|
|
|
async def get_date():
|
2014-10-13 19:52:07 -03:00
|
|
|
code = 'import datetime; print(datetime.datetime.now())'
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
# Create the subprocess; redirect the standard output
|
|
|
|
# into a pipe.
|
2017-12-11 11:35:49 -04:00
|
|
|
proc = await asyncio.create_subprocess_exec(
|
|
|
|
sys.executable, '-c', code,
|
|
|
|
stdout=asyncio.subprocess.PIPE)
|
2014-10-13 19:52:07 -03:00
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
# Read one line of output.
|
2017-12-11 11:35:49 -04:00
|
|
|
data = await proc.stdout.readline()
|
2014-10-13 19:52:07 -03:00
|
|
|
line = data.decode('ascii').rstrip()
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
# Wait for the subprocess exit.
|
2017-12-11 11:35:49 -04:00
|
|
|
await proc.wait()
|
2014-10-13 19:52:07 -03:00
|
|
|
return line
|
|
|
|
|
2018-09-11 13:54:40 -03:00
|
|
|
date = asyncio.run(get_date())
|
|
|
|
print(f"Current date: {date}")
|
|
|
|
|
|
|
|
|
2018-09-17 16:35:24 -03:00
|
|
|
See also the :ref:`same example <asyncio_example_subprocess_proto>`
|
2018-09-11 13:54:40 -03:00
|
|
|
written using low-level APIs.
|