[Tutor] subprocess.Popen basics
Adam Jensen
hanzer at riseup.net
Tue Oct 28 16:31:13 CET 2014
Update:
On 10/27/2014 09:50 PM, Adam Jensen wrote:
> What's weird is that I have two different python3.4 installations on
> this CentOS-6.5 machine and both have the same behavior (script hangs
> until Ctrl+C).
>
> I built this one (/opt/bin/python3.4) from source:
...
> But this one (~/anaconda3/bin/python3.4) was a binary distribution (if I
> recall correctly):
This ^ undermines the "build problem" theory. I found a little blurb in
the documentation about bufsize:
-------------------------------------------------------------------------
bufsize will be supplied as the corresponding argument to the open()
function when creating the stdin/stdout/stderr pipe file objects:
* 0 means unbuffered (read and write are one system call and can return
short)
* 1 means line buffered (only usable if universal_newlines=True i.e., in
a text mode)
* any other positive value means use a buffer of approximately that size
* negative bufsize (the default) means the system default of
io.DEFAULT_BUFFER_SIZE will be used.
-------------------------------------------------------------------------
Some tinkering:
-------------------------------------------------------------------------
#!/usr/bin/env python3.4
import subprocess as sp
parrot = sp.Popen(['./parrot.sh'], bufsize=0,
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE,
universal_newlines=True, start_new_session=True)
parrot.stdin.write('Pushing up daisies.\n')
print('Parrot said: ', parrot.stdout.readline())
parrot.kill()
-------------------------------------------------------------------------
And I get these results (on CentOS-6.5-x86):
| bufsize | results |
|---------+---------|
| default | hangs |
| -1 | hangs |
| 0 | works |
| 1 | hangs |
| >=2 | works |
More information about the Tutor
mailing list