2.4isms for bzr

John A Meinel john at arbash-meinel.com
Tue Jul 26 17:40:13 BST 2005


Aaron Bentley wrote:
> Here are a couple of patches for the Python 2.4 transition
> 1. Always invoke python-2.4, never 2.3 (more complete than Wouter's
> version).
> 2. patch.py now uses subprocess instead of popen
> 
> The rest of the code looks pretty clean, except for the unused functions
> bzrlib.osutils.extern_command() and bzrlib.osutils.uuid(), and a few
> calls to spawn*.
> 
> I'm kinda shocked that Popen.communicate() is a one-time function.  I
> thought you'd iterate through it like so:
> 
> output = []
> error = []
> for line in input:
>     out_chunk, err_chunk = process.communicate(input)
>     output.append(out_chunk)
>     error.append(err_chunk)
> 
> Is there no efficient way to run a command that accepts and produces
> large amounts of data without running the risk of deadlocks?  Hmm.  You
> could get away with blocking writes if you split your input into
> buffer-sized chunks, but you'd at least need non-blocking reads.
> 
> Aaron

Well, you probably can set the non-blocking flags on process.stdin and 
process.stdout

I agree that having communicate be one time is a little bit of a pain, 
but I think it is for simplicity, and the fact that a lot of times, that 
is all you need.

And it depends what you mean by "efficient", because your above code 
just buffers everything in ram, which is what communicate does.

You probably could just write some sort of select() wrapper around the 
popen object (just grab it's 3 file handles), and then you could have:

output = []
error = []
for line in input:
     out_chunk, err_chunk = iter_communicate(process, line)
     output.append(out_chunk)
     error.append(err_chunk)

Though probably it would be better to do it as:

output = []
error = []
for out_chunk, err_chunk in iter_communicate(process, input):
     output.append(out_chunk)
     error.append(err_chunk)

Heck, make iter_communicate support having input being a generator, and 
you could even have bi-directional communications with another process, 
while having it only really look like a loop.

John
=:->
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 253 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050726/560bc375/attachment.pgp 


More information about the bazaar mailing list