named pipe input

M

max(01)*

hi there.

i have some problems understanding following behaviour.

consider this:

....
$ cat file_input_3.pl
#!/usr/bin/perl

open MIAPIPE, "una_pipe";

while ($riga = <MIAPIPE>)
{
print STDOUT ("$riga");
}

$ cat file_input_3.py
#!/usr/bin/python

import sys

MIAPIPE = open("una_pipe", "r")

for riga in MIAPIPE:
print riga,
....

where una_pipe is a named pipe (created with mkfifo).

when i run this on console #1:

....
$ ./file_input_3.pl
....

and this un console #2:

....
$ cat > una_pipe
aaa
bbb
ccc
....

then each line typed in console #2 appears on console #1 as soon as the
line is terminated (hit return).

BUT if i try to do the same with the python code, something different
happens: i have to type ALL the lines on console #2 and complete the cat
command (ctrl-d) before seeing the lines echoed on console #1.

i tried the -u flag but doesnt seem to work.

any help?

bye
 
E

Eric Nieuwland

max(01)* said:
$ cat file_input_3.py
#!/usr/bin/python

import sys

MIAPIPE = open("una_pipe", "r")

for riga in MIAPIPE:
print riga,
...
[...]
BUT if i try to do the same with the python code, something different
happens: i have to type ALL the lines on console #2 and complete the
cat
command (ctrl-d) before seeing the lines echoed on console #1.

You could try:

for riga in MIAPIPE:
print riga # NO COMMA!
sys.stdout.flush()
 
M

max(01)*

Eric said:
max(01)* said:
$ cat file_input_3.py
#!/usr/bin/python

import sys

MIAPIPE = open("una_pipe", "r")

for riga in MIAPIPE:
print riga,
...
[...]
BUT if i try to do the same with the python code, something different
happens: i have to type ALL the lines on console #2 and complete the cat
command (ctrl-d) before seeing the lines echoed on console #1.


You could try:

for riga in MIAPIPE:
print riga # NO COMMA!
sys.stdout.flush()

doesn't work! :-(
 
D

Donn Cave

"max(01)* said:
i have some problems understanding following behaviour.

consider this:
$ cat file_input_3.pl
#!/usr/bin/perl

open MIAPIPE, "una_pipe";

while ($riga = <MIAPIPE>) ....

$ cat file_input_3.py
#!/usr/bin/python

import sys

MIAPIPE = open("una_pipe", "r")

for riga in MIAPIPE:
....
BUT if i try to do the same with the python code, something different
happens: i have to type ALL the lines on console #2 and complete the cat
command (ctrl-d) before seeing the lines echoed on console #1.


Seems to me something like this came up here
not long ago. It turns out that

for line in file:

doesn't do the same thing as Perl's

while ($line = <file>)

If you use file.readline() instead (in a loop,
of course, I think you'll get the data one line
at a time, but "in file" apparently reads the
whole file first. That's what I vaguely remember,
I don't use it myself.

Donn Cave, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top