@@ -541,84 +541,6 @@ Thus, to read *n* bytes from a pipe *p* created with :func:`os.popen`, you need
541541use ` ` p.read(n)` ` .
542542
543543
544- .. XXX update to use subprocess. See the :ref:` subprocess-replacements` section.
545-
546- How do I run a subprocess with pipes connected to both input and output?
547- ------------------------------------------------------------------------
548-
549- Use the :mod:` popen2` module. For example::
550-
551- import popen2
552- fromchild, tochild = popen2.popen2(" command" )
553- tochild.write(" input\n " )
554- tochild.flush()
555- output = fromchild.readline()
556-
557- Warning: in general it is unwise to do this because you can easily cause a
558- deadlock where your process is blocked waiting for output from the child
559- while the child is blocked waiting for input from you. This can be caused
560- by the parent expecting the child to output more text than it does or
561- by data being stuck in stdio buffers due to lack of flushing.
562- The Python parent can of course explicitly flush the data it sends to the
563- child before it reads any output, but if the child is a naive C program it
564- may have been written to never explicitly flush its output, even if it is
565- interactive, since flushing is normally automatic.
566-
567- Note that a deadlock is also possible if you use :func:` popen3` to read
568- stdout and stderr. If one of the two is too large for the internal buffer
569- (increasing the buffer size does not help) and you ` ` read()` ` the other one
570- first, there is a deadlock, too.
571-
572- Note on a bug in popen2: unless your program calls ` ` wait()` ` or
573- ` ` waitpid()` ` , finished child processes are never removed, and eventually
574- calls to popen2 will fail because of a limit on the number of child
575- processes. Calling :func:` os.waitpid` with the :const:` os.WNOHANG` option can
576- prevent this; a good place to insert such a call would be before calling
577- ` ` popen2` ` again.
578-
579- In many cases, all you really need is to run some data through a command and
580- get the result back. Unless the amount of data is very large, the easiest
581- way to do this is to write it to a temporary file and run the command with
582- that temporary file as input. The standard module :mod:` tempfile` exports a
583- :func:` ~tempfile.mktemp` function to generate unique temporary file names. ::
584-
585- import tempfile
586- import os
587-
588- class Popen3:
589- " " "
590- This is a deadlock-safe version of popen that returns
591- an object with errorlevel, out (a string) and err (a string).
592- (capturestderr may not work under windows.)
593- Example: print(Popen3(' grep spam' ,' \n\nhere spam\n\n' ).out)
594- " " "
595- def __init__(self,command,input=None,capturestderr=None):
596- outfile=tempfile.mktemp()
597- command=" ( %s ) > %s" % (command,outfile)
598- if input:
599- infile=tempfile.mktemp()
600- open(infile," w" ).write(input)
601- command=command+" < " +infile
602- if capturestderr:
603- errfile=tempfile.mktemp()
604- command=command+" 2> " +errfile
605- self.errorlevel=os.system(command) >> 8
606- self.out=open(outfile," r" ).read()
607- os.remove(outfile)
608- if input:
609- os.remove(infile)
610- if capturestderr:
611- self.err=open(errfile," r" ).read()
612- os.remove(errfile)
613-
614- Note that many interactive programs (e.g. vi) don't work well with pipes
615- substituted for standard input and output. You will have to use pseudo ttys
616- (" ptys" ) instead of pipes. Or you can use a Python interface to Don Libes'
617- " expect" library. A Python extension that interfaces to expect is called
618- " expy" and available from https://expectpy.sourceforge.net. A pure Python
619- solution that works like expect is :pypi:` pexpect` .
620-
621-
622544How do I access the serial (RS232) port?
623545----------------------------------------
624546
0 commit comments