IPC

Jp Calderone exarkun at divmod.com
Tue Jul 27 16:46:43 EDT 2004


Donn Cave wrote:
> In article <mailman.856.1090955408.5135.python-list at python.org>,
>  <brianc at temple.edu> wrote:
> 
>>There's two modules that I like to use for this kind of stuff:
>>http://starship.python.net/crew/tmick/ (process.py)
>>http://www.lysator.liu.se/~astrand/popen5/ (also process.py
>>just to be confusing)
> 
> 
> And worse yet, neither does much processing per se.
> 
> 
>>I recently did a quick hack using select and popen2, but I
>>doubt it's fast or even safe, but it works.
>>
>>#yadda yadda
>>r, w, e = popen2.popen3(cmd)
>>cmd = "?\n"
>>w.write(cmd)
>>w.flush()
>>#continuely test for output to read
>>while select.select([e],[],[],1)[0]:#timeout guessing game
>>    line=e.readline()
>>    #do something with line
>>while select.select([r],[],[],1)[0]:#timeout guessing game
>>    line=r.readline()
>>    #do something with line
>>
>>If you're output is line buffered you probably shouldn't have
>>many problems with this approach if you can be assured your
>>program will respond within the 1 second alloted to it. If
>>you're positive you'll get output, just remove the timeout.
> 
> 
> Two points spring to mind:
> 
> 1.  Select's fundamental purpose, the reason you must
>     write select([file], ... instead of select(file, ...
>     is to handle multiple sources of input.
> 
> 2.  The file object's fundamental purpose is to make
>     sure select doesn't work.  Even if output is line
>     buffered, that doesn't help input, which will still
>     confound select with process buffering.  The buffer
>     parameters for the input file objects could do
>     something, but at the cost of reading input byte
>     by byte at considerable expense in system calls.
>     Use file descriptors.

   I was with you up until the last sentence.  You seem to be implying that

     select([fileObj], ...)

   is different, at some underlying level, from

     select([fileObj.fileno()], ...)

   This is not the case.  If you pass a file object to select(), Python 
will call fileno() for you.

   There is indeed buffering, but it will apply to the file descriptor 
just as much as it will to the file object, because it happens at or 
below the file descriptor level.

   Jp



More information about the Python-list mailing list