/Doc/library/popen2.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 199 lines · 128 code · 71 blank · 0 comment · 0 complexity · 0d820d09d11625c51641358d7280430c MD5 · raw file

  1. :mod:`popen2` --- Subprocesses with accessible I/O streams
  2. ==========================================================
  3. .. module:: popen2
  4. :synopsis: Subprocesses with accessible standard I/O streams.
  5. :deprecated:
  6. .. sectionauthor:: Drew Csillag <drew_csillag@geocities.com>
  7. .. deprecated:: 2.6
  8. This module is obsolete. Use the :mod:`subprocess` module. Check
  9. especially the :ref:`subprocess-replacements` section.
  10. This module allows you to spawn processes and connect to their
  11. input/output/error pipes and obtain their return codes under Unix and Windows.
  12. The :mod:`subprocess` module provides more powerful facilities for spawning new
  13. processes and retrieving their results. Using the :mod:`subprocess` module is
  14. preferable to using the :mod:`popen2` module.
  15. The primary interface offered by this module is a trio of factory functions.
  16. For each of these, if *bufsize* is specified, it specifies the buffer size for
  17. the I/O pipes. *mode*, if provided, should be the string ``'b'`` or ``'t'``; on
  18. Windows this is needed to determine whether the file objects should be opened in
  19. binary or text mode. The default value for *mode* is ``'t'``.
  20. On Unix, *cmd* may be a sequence, in which case arguments will be passed
  21. directly to the program without shell intervention (as with :func:`os.spawnv`).
  22. If *cmd* is a string it will be passed to the shell (as with :func:`os.system`).
  23. The only way to retrieve the return codes for the child processes is by using
  24. the :meth:`poll` or :meth:`wait` methods on the :class:`Popen3` and
  25. :class:`Popen4` classes; these are only available on Unix. This information is
  26. not available when using the :func:`popen2`, :func:`popen3`, and :func:`popen4`
  27. functions, or the equivalent functions in the :mod:`os` module. (Note that the
  28. tuples returned by the :mod:`os` module's functions are in a different order
  29. from the ones returned by the :mod:`popen2` module.)
  30. .. function:: popen2(cmd[, bufsize[, mode]])
  31. Executes *cmd* as a sub-process. Returns the file objects ``(child_stdout,
  32. child_stdin)``.
  33. .. function:: popen3(cmd[, bufsize[, mode]])
  34. Executes *cmd* as a sub-process. Returns the file objects ``(child_stdout,
  35. child_stdin, child_stderr)``.
  36. .. function:: popen4(cmd[, bufsize[, mode]])
  37. Executes *cmd* as a sub-process. Returns the file objects
  38. ``(child_stdout_and_stderr, child_stdin)``.
  39. .. versionadded:: 2.0
  40. On Unix, a class defining the objects returned by the factory functions is also
  41. available. These are not used for the Windows implementation, and are not
  42. available on that platform.
  43. .. class:: Popen3(cmd[, capturestderr[, bufsize]])
  44. This class represents a child process. Normally, :class:`Popen3` instances are
  45. created using the :func:`popen2` and :func:`popen3` factory functions described
  46. above.
  47. If not using one of the helper functions to create :class:`Popen3` objects, the
  48. parameter *cmd* is the shell command to execute in a sub-process. The
  49. *capturestderr* flag, if true, specifies that the object should capture standard
  50. error output of the child process. The default is false. If the *bufsize*
  51. parameter is specified, it specifies the size of the I/O buffers to/from the
  52. child process.
  53. .. class:: Popen4(cmd[, bufsize])
  54. Similar to :class:`Popen3`, but always captures standard error into the same
  55. file object as standard output. These are typically created using
  56. :func:`popen4`.
  57. .. versionadded:: 2.0
  58. .. _popen3-objects:
  59. Popen3 and Popen4 Objects
  60. -------------------------
  61. Instances of the :class:`Popen3` and :class:`Popen4` classes have the following
  62. methods:
  63. .. method:: Popen3.poll()
  64. Returns ``-1`` if child process hasn't completed yet, or its status code
  65. (see :meth:`wait`) otherwise.
  66. .. method:: Popen3.wait()
  67. Waits for and returns the status code of the child process. The status code
  68. encodes both the return code of the process and information about whether it
  69. exited using the :cfunc:`exit` system call or died due to a signal. Functions
  70. to help interpret the status code are defined in the :mod:`os` module; see
  71. section :ref:`os-process` for the :func:`W\*` family of functions.
  72. The following attributes are also available:
  73. .. attribute:: Popen3.fromchild
  74. A file object that provides output from the child process. For :class:`Popen4`
  75. instances, this will provide both the standard output and standard error
  76. streams.
  77. .. attribute:: Popen3.tochild
  78. A file object that provides input to the child process.
  79. .. attribute:: Popen3.childerr
  80. A file object that provides error output from the child process, if
  81. *capturestderr* was true for the constructor, otherwise ``None``. This will
  82. always be ``None`` for :class:`Popen4` instances.
  83. .. attribute:: Popen3.pid
  84. The process ID of the child process.
  85. .. _popen2-flow-control:
  86. Flow Control Issues
  87. -------------------
  88. Any time you are working with any form of inter-process communication, control
  89. flow needs to be carefully thought out. This remains the case with the file
  90. objects provided by this module (or the :mod:`os` module equivalents).
  91. When reading output from a child process that writes a lot of data to standard
  92. error while the parent is reading from the child's standard output, a deadlock
  93. can occur. A similar situation can occur with other combinations of reads and
  94. writes. The essential factors are that more than :const:`_PC_PIPE_BUF` bytes
  95. are being written by one process in a blocking fashion, while the other process
  96. is reading from the first process, also in a blocking fashion.
  97. .. Example explanation and suggested work-arounds substantially stolen
  98. from Martin von Lรถwis:
  99. http://mail.python.org/pipermail/python-dev/2000-September/009460.html
  100. There are several ways to deal with this situation.
  101. The simplest application change, in many cases, will be to follow this model in
  102. the parent process::
  103. import popen2
  104. r, w, e = popen2.popen3('python slave.py')
  105. e.readlines()
  106. r.readlines()
  107. r.close()
  108. e.close()
  109. w.close()
  110. with code like this in the child::
  111. import os
  112. import sys
  113. # note that each of these print statements
  114. # writes a single long string
  115. print >>sys.stderr, 400 * 'this is a test\n'
  116. os.close(sys.stderr.fileno())
  117. print >>sys.stdout, 400 * 'this is another test\n'
  118. In particular, note that ``sys.stderr`` must be closed after writing all data,
  119. or :meth:`readlines` won't return. Also note that :func:`os.close` must be
  120. used, as ``sys.stderr.close()`` won't close ``stderr`` (otherwise assigning to
  121. ``sys.stderr`` will silently close it, so no further errors can be printed).
  122. Applications which need to support a more general approach should integrate I/O
  123. over pipes with their :func:`select` loops, or use separate threads to read each
  124. of the individual files provided by whichever :func:`popen\*` function or
  125. :class:`Popen\*` class was used.
  126. .. seealso::
  127. Module :mod:`subprocess`
  128. Module for spawning and managing subprocesses.