named pipe input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • max(01)*

    named pipe input

    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
  • Eric Nieuwland

    #2
    Re: named pipe input

    max(01)* wrote:[color=blue]
    > $ 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.[/color]

    You could try:

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

    Comment

    • max(01)*

      #3
      Re: named pipe input

      Eric Nieuwland wrote:[color=blue]
      > max(01)* wrote:
      >[color=green]
      >> $ 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.[/color]
      >
      >
      > You could try:
      >
      > for riga in MIAPIPE:
      > print riga # NO COMMA!
      > sys.stdout.flus h()
      >[/color]

      doesn't work! :-(

      Comment

      • Donn Cave

        #4
        Re: named pipe input

        In article <3gIRe.6091$O6. 328317@news3.ti n.it>,
        "max(01)*" <max2@fisso.cas a> wrote:
        [color=blue]
        > i have some problems understanding following behaviour.
        >
        > consider this:[/color]
        [color=blue]
        > $ cat file_input_3.pl
        > #!/usr/bin/perl
        >
        > open MIAPIPE, "una_pipe";
        >
        > while ($riga = <MIAPIPE>)[/color]
        ....
        [color=blue]
        > $ cat file_input_3.py
        > #!/usr/bin/python
        >
        > import sys
        >
        > MIAPIPE = open("una_pipe" , "r")
        >
        > for riga in MIAPIPE:[/color]

        ....[color=blue]
        > 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.[/color]


        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, donn@u.washingt on.edu

        Comment

        Working...