PEP 3145 – Asynchronous I/O For subprocess.Popen
- Author:
- Eric Pruitt, Charles R. McCreary, Josiah Carlson
- Status:
- Withdrawn
- Type:
- Standards Track
- Created:
- 04-Aug-2009
- Python-Version:
- 3.2
- Post-History:
Table of Contents
Abstract
In its present form, thesubprocess.Popen
implementation is prone to
dead-locking and blocking of the parent Python script while waiting on data
from the child process. This PEP proposes to make
subprocess.Popen
more asynchronous to help alleviate these
problems.
PEP Deferral
Further exploration of the concepts covered in this PEP has been deferred at least until afterPEP 3156has been resolved.
PEP Withdrawal
This can be dealt with in the bug tracker. A specific proposal is attached to[11].
Motivation
A search for “Python asynchronous subprocess” will turn up numerous
accounts of people wanting to execute a child process and communicate with
it from time to time reading only the data that is available instead of
blocking to wait for the program to produce data[1][2][3].The current
behavior of thesubprocess
module is that when a user sends or receives
data via the stdin, stderr and stdout file objects, dead locks are common
and documented[4][5].While communicate can be used to alleviate some of
the buffering issues, it will still cause the parent process to block while
attempting to read data when none is available to be read from the child
process.
Rationale
There is a documented need for asynchronous, non-blocking functionality in
subprocess.Popen
[6][7][2][3].Inclusion of the code would improve the
utility of the Python standard library that can be used on Unix based and
Windows builds of Python. Practically every I/O object in Python has a
file-like wrapper of some sort. Sockets already act as such and for
strings there isStringIO
.Popen can be made to act like a file by simply
using the methods attached to thesubprocess.Popen.stderr
,stdout and
stdin file-like objects. But when using the read and write methods of
those options, you do not have the benefit of asynchronous I/O. In the
proposed solution the wrapper wraps the asynchronous methods to mimic a
file object.
Reference Implementation
I have been maintaining a Google Code repository that contains all of my changes including tests and documentation[9]as well as blog detailing the problems I have come across in the development process[10].
I have been working on implementing non-blocking asynchronous I/O in the
subprocess
module as well as a wrapper class forsubprocess.Popen
that makes it so that an executed process can take the place of a file by
duplicating all of the methods and attributes that file objects have.
There are two base functions that have been added to thesubprocess.Popen
class:Popen.send
andPopen._recv
,each with two separate implementations,
one for Windows and one for Unix-based systems. The Windows
implementation uses ctypes to access the functions needed to control pipes
in the kernel 32 DLL in an asynchronous manner. On Unix based systems,
the Python interface for file control serves the same purpose. The
different implementations ofPopen.send
andPopen._recv
have identical
arguments to make code that uses these functions work across multiple
platforms.
When calling thePopen._recv
function, it requires the pipe name be
passed as an argument so there exists thePopen.recv
function that passes
selects stdout as the pipe forPopen._recv
by default.Popen.recv_err
selects stderr as the pipe by default.Popen.recv
andPopen.recv_err
are much easier to read and understand thanPopen._recv('stdout'...)
and
Popen._recv('stderr'...)
respectively.
Since thePopen._recv
function does not wait on data to be produced
before returning a value, it may return empty bytes.Popen.asyncread
handles this issue by returning all data read over a given time
interval.
TheProcessIOWrapper
class uses theasyncread
andasyncwrite
functions to
allow a process to act like a file so that there are no blocking issues
that can arise from using the stdout and stdin file objects produced from
asubprocess.Popen
call.
References
[8] subprocess.rst - subprocdev - Project Hosting on Google Code https://web.archive.org/web/20130306074135/http://code.google /p/subprocdev/source/browse/doc/subprocess.rst?spec=svn2c925e935cad0166d5da85e37c742d8e7f609de5&r=2c925e935cad0166d5da85e37c742d8e7f609de5
Copyright
This P.E.P. is licensed under the Open Publication License; http:// opencontent.org/openpub/.
Source:https://github / Python /peps/blob/main/peps/pep-3145.rst
Last modified:2023-09-09 17:39:29 GMT