[Python-Dev] Forking and pipes (original) (raw)
Lars Kotthoff lists at larsko.org
Tue Dec 9 20:26:51 CET 2008
- Previous message: [Python-Dev] Floating-point implementations
- Next message: [Python-Dev] Forking and pipes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Dear list,
I recently noticed a python program which uses forks and pipes for communication between the processes not behaving as expected. The minimal example program:
#!/usr/bin/python
import os, sys
r, w = os.pipe() write = os.fdopen(w, 'w') print >> write, "foo" pid = os.fork() if pid: os.waitpid(pid, 0) else: sys.exit(0) write.close() read = os.fdopen(r) print read.read() read.close()
This prints out "foo" twice although it's only written once to the pipe. It seems that python doesn't flush file descriptors before copying them to the child process, thus resulting in the duplicate message. The equivalent C program behaves as expected,
#include <stdio.h> #include <stdlib.h> #include <unistd.h>
int main(void) { int fds[2]; pid_t pid; char* buf = (char*) calloc(4, sizeof(char));
pipe(fds);
write(fds[1], "foo", 3);
pid = fork();
if(pid) {
waitpid(pid, NULL, 0);
} else {
return EXIT_SUCCESS;
}
close(fds[1]);
read(fds[0], buf, 3);
printf("%s\n", buf);
close(fds[0]);
free(buf);
return EXIT_SUCCESS;
}
Is this behaviour intentional? I've tested both python and C on Linux, OpenBSD and Solaris (python versions 2.5.2 and 2.3.3), the behaviour was the same everywhere.
Thanks,
Lars
- Previous message: [Python-Dev] Floating-point implementations
- Next message: [Python-Dev] Forking and pipes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]