Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
102 captures
11 Oct 2008 - 24 Mar 2024
SepOCTNov
15
201120122013
success
fail
COLLECTED BY
Organization:Internet Archive
The Internet Archive discovers and captures web pages through many different web crawls.At any given time several distinct crawls are running, some for months, and some every day or longer.View the web archive through theWayback Machine.
Web wide crawl with initial seedlist and crawler configuration from September 2012.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20121015110053/http://docs.python.org/library/popen2.html

Navigation

17.5.popen2 — Subprocesses with accessible I/O streams

Deprecated since version 2.6:This module is obsolete. Use thesubprocess module. Checkespecially theReplacing Older Functions with the subprocess Module section.

This module allows you to spawn processes and connect to theirinput/output/error pipes and obtain their return codes under Unix and Windows.

Thesubprocess module provides more powerful facilities for spawning newprocesses and retrieving their results. Using thesubprocess module ispreferable to using thepopen2 module.

The primary interface offered by this module is a trio of factory functions.For each of these, ifbufsize is specified, it specifies the buffer size forthe I/O pipes.mode, if provided, should be the string'b' or't'; onWindows this is needed to determine whether the file objects should be opened inbinary or text mode. The default value formode is't'.

On Unix,cmd may be a sequence, in which case arguments will be passeddirectly to the program without shell intervention (as withos.spawnv()).Ifcmd is a string it will be passed to the shell (as withos.system()).

The only way to retrieve the return codes for the child processes is by usingthepoll() orwait() methods on thePopen3 andPopen4 classes; these are only available on Unix. This information isnot available when using thepopen2(),popen3(), andpopen4()functions, or the equivalent functions in theos module. (Note that thetuples returned by theos module’s functions are in a different orderfrom the ones returned by thepopen2 module.)

popen2.popen2(cmd[,bufsize[,mode]])

Executescmd as a sub-process. Returns the file objects(child_stdout,child_stdin).

popen2.popen3(cmd[,bufsize[,mode]])

Executescmd as a sub-process. Returns the file objects(child_stdout,child_stdin,child_stderr).

popen2.popen4(cmd[,bufsize[,mode]])

Executescmd as a sub-process. Returns the file objects(child_stdout_and_stderr,child_stdin).

New in version 2.0.

On Unix, a class defining the objects returned by the factory functions is alsoavailable. These are not used for the Windows implementation, and are notavailable on that platform.

classpopen2.Popen3(cmd[,capturestderr[,bufsize]])

This class represents a child process. Normally,Popen3 instances arecreated using thepopen2() andpopen3() factory functions describedabove.

If not using one of the helper functions to createPopen3 objects, theparametercmd is the shell command to execute in a sub-process. Thecapturestderr flag, if true, specifies that the object should capture standarderror output of the child process. The default is false. If thebufsizeparameter is specified, it specifies the size of the I/O buffers to/from thechild process.

classpopen2.Popen4(cmd[,bufsize])

Similar toPopen3, but always captures standard error into the samefile object as standard output. These are typically created usingpopen4().

New in version 2.0.

17.5.1. Popen3 and Popen4 Objects

Instances of thePopen3 andPopen4 classes have the followingmethods:

Popen3.poll()

Returns-1 if child process hasn’t completed yet, or its status code(seewait()) otherwise.

Popen3.wait()

Waits for and returns the status code of the child process. The status codeencodes both the return code of the process and information about whether itexited using theexit() system call or died due to a signal. Functionsto help interpret the status code are defined in theos module; seesectionProcess Management for theW*() family of functions.

The following attributes are also available:

Popen3.fromchild

A file object that provides output from the child process. ForPopen4instances, this will provide both the standard output and standard errorstreams.

Popen3.tochild

A file object that provides input to the child process.

Popen3.childerr

A file object that provides error output from the child process, ifcapturestderr was true for the constructor, otherwiseNone. This willalways beNone forPopen4 instances.

Popen3.pid

The process ID of the child process.

17.5.2. Flow Control Issues

Any time you are working with any form of inter-process communication, controlflow needs to be carefully thought out. This remains the case with the fileobjects provided by this module (or theos module equivalents).

When reading output from a child process that writes a lot of data to standarderror while the parent is reading from the child’s standard output, a deadlockcan occur. A similar situation can occur with other combinations of reads andwrites. The essential factors are that more than_PC_PIPE_BUF bytesare being written by one process in a blocking fashion, while the other processis reading from the first process, also in a blocking fashion.

There are several ways to deal with this situation.

The simplest application change, in many cases, will be to follow this model inthe parent process:

importpopen2r,w,e=popen2.popen3('python slave.py')e.readlines()r.readlines()r.close()e.close()w.close()

with code like this in the child:

importosimportsys# note that each of these print statements# writes a single long stringprint>>sys.stderr,400*'this is a test\n'os.close(sys.stderr.fileno())print>>sys.stdout,400*'this is another test\n'

In particular, note thatsys.stderr must be closed after writing all data,orreadlines() won’t return. Also note thatos.close() must beused, assys.stderr.close() won’t closestderr (otherwise assigning tosys.stderr will silently close it, so no further errors can be printed).

Applications which need to support a more general approach should integrate I/Oover pipes with theirselect() loops, or use separate threads to read eachof the individual files provided by whicheverpopen*() function orPopen* class was used.

See also

Modulesubprocess
Module for spawning and managing subprocesses.

Table Of Contents

Previous topic

17.4.signal — Set handlers for asynchronous events

Next topic

17.6.asyncore — Asynchronous socket handler

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2012, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Oct 15, 2012.Found a bug?
Created usingSphinx 1.0.7.

[8]ページ先頭

©2009-2025 Movatter.jp