pipe problem on FreeBSD....

Jarkko Torppa torppa at polykoira.megabaud.fi
Sat Jan 8 18:57:58 EST 2000


In article <200001082039.PAA12903 at acer.spvi.com>, Steve Spicklemire wrote:
>   Hmmm.. what I thought started out as a "Zope Bug" isn't. Let me 
>forgo all the history here and cut to the current analysis. There
>seems to be a problem on FreeBSD with python sending large amounts of 
>data through a pipe that blocks.
...
>Here is the reference to that problem report:
>http://lists.zope.org/pipermail/zope/1999-December/015874.html
...
>I think I've now simplified it to it's most basic form:

This is quite different matter that what you are seeing in Zope.
>This python script:
...
>fails on FreeBSD when invoked thusly:
>
>pluto.spvi.com> python tryit.py | head -3
...
>IOError: [Errno 32] Broken pipe
>where this c program does not:
..

Here is c program that does tries more or less match python's 
behaviour. 
----
#include <stdio.h>
#include <err.h>
#include <signal.h>

int main() {
  size_t charCount,c;
  char buffer[1024];
  FILE *f;

  signal(SIGPIPE,SIG_IGN);

  if((f = fopen("blah","r")) != NULL) {
    do {
      charCount = fread(buffer,1,sizeof(buffer),f);
      c=fwrite(buffer,1,charCount,stdout);
      if ( c != charCount )
	err(1,"WriteFailed");
    } while(charCount == sizeof(buffer));
    close(f);
  }
  else
    printf("Can't open file... blah\n");
}
---
tuhnu[2]%./tryit | head -3
...
tryit: WriteFailed: Broken pipe

>My thinking is that they should behave in the same way. (This also
>happens with 'more' and other such blocking programs.) Where in python
>should I look for the difference between tryit.c and tryit.py? Of
>course on linux both programs *do* behave the same way... I just
>created the 'c' version to see if it was deep in FreeBSD or somewhere
>in the python port. The python and c version work correctly in linux.
><dang!>

By default SIGPIPE terminates the program, also your cprogram
did not check writes return value so it could not have
caught the error anyhow. 

Behaviour you are seeing is correct. But this is different from
blocking it should not give SIGPIPE, writing thread should
just stall untill there is space in pipe to put the data.

This python piece does blocking reads on stdin

import time,sys
while 1:
    r=sys.stdin.read(1024)
    if len(r) == 0:
        break
    sys.stdout.write(r)
    time.sleep(30)

-- 
 Jarkko Torppa                torppa at staff.megabaud.fi
  Megabaud Internet-palvelut



More information about the Python-list mailing list