FIFO problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tobias Pfeiffer

    FIFO problems

    Hi!

    I want to write a "client-server-application" (only running on the same
    machine) or actually I've already begun with and have problems with the
    interprocess communication. The server, when started, opens a FIFO and
    opens it with open(infifo, 'r'). Then I check the content of the file
    with

    while 1:
    line = serverIn.readli ne()[:-1]
    if line == "bla":
    do this
    else:
    print line

    So at the beginning everything works fine. I can start the client prog
    and they will talk and understand to each other. The same if I connect
    with three or more clients simultaneously (?). If one of these clients
    exists, no problem. But when the last process exists and closes the
    FIFO-file which was opened by os.open(infifo, os.O_WRONLY), the server
    begins to print an empty string everytime it goes through the loop,
    means it won't wait for a complete line to appear before continuing.
    Why?
    Then I modified the thing above to:

    while 1:
    line = serverIn.readli ne()[:-1]
    if line == "bla":
    do this
    elif line == "":
    open(infifo, 'r')
    print "help!!!"
    else:
    print line

    So in contrast to what I thought, there was not a bunch of "help!!!"
    lines printed, but nothing. When I tried then to connect again, there
    was a single "help!!!" and then the correct message for the client
    having connected.

    So, anyone any idea how I can fix this. I just want the server to
    continue line-by-line-reading...

    Bye
    Tobias
  • Tobias Pfeiffer

    #2
    Re: FIFO problems

    Hi!

    Sorry, big mistake!!

    Tobias Pfeiffer <BoteDesSchatte ns@web.de> wrote in
    news:bjp0sg$li4 db$1@ID-162581.news.uni-berlin.de:
    [color=blue]
    > these clients exists, no problem. But when the last process exists[/color]

    EXITS, not exists...

    Bye
    Tobias

    Comment

    • Gary Herron

      #3
      Re: FIFO problems

      The most generic method of doing interprocess communication (with
      process on the same or different machine) is to use sockets. See
      Python's socket module. There are lots of other ways -- pipes (see
      the os.popen family) and shared memory, but you'll be able to find
      many examples and much documentation for sockets. Reading and writing
      straight files (if that is really what you are doing) is probably not
      going to work well.

      Gary Herron


      On Wednesday 10 September 2003 10:23 pm, Tobias Pfeiffer wrote:[color=blue]
      > Hi!
      >
      > I want to write a "client-server-application" (only running on the same
      > machine) or actually I've already begun with and have problems with the
      > interprocess communication. The server, when started, opens a FIFO and
      > opens it with open(infifo, 'r'). Then I check the content of the file
      > with
      >
      > while 1:
      > line = serverIn.readli ne()[:-1]
      > if line == "bla":
      > do this
      > else:
      > print line
      >
      > So at the beginning everything works fine. I can start the client prog
      > and they will talk and understand to each other. The same if I connect
      > with three or more clients simultaneously (?). If one of these clients
      > exists, no problem. But when the last process exists and closes the
      > FIFO-file which was opened by os.open(infifo, os.O_WRONLY), the server
      > begins to print an empty string everytime it goes through the loop,
      > means it won't wait for a complete line to appear before continuing.
      > Why?
      > Then I modified the thing above to:
      >
      > while 1:
      > line = serverIn.readli ne()[:-1]
      > if line == "bla":
      > do this
      > elif line == "":
      > open(infifo, 'r')
      > print "help!!!"
      > else:
      > print line
      >
      > So in contrast to what I thought, there was not a bunch of "help!!!"
      > lines printed, but nothing. When I tried then to connect again, there
      > was a single "help!!!" and then the correct message for the client
      > having connected.
      >
      > So, anyone any idea how I can fix this. I just want the server to
      > continue line-by-line-reading...
      >
      > Bye
      > Tobias[/color]



      Comment

      • Donn Cave

        #4
        Re: FIFO problems

        Quoth Tobias Pfeiffer <BoteDesSchatte ns@web.de>:
        ....
        | So at the beginning everything works fine. I can start the client prog
        | and they will talk and understand to each other. The same if I connect
        | with three or more clients simultaneously (?). If one of these clients
        | exits, no problem. But when the last process exists and closes the
        | FIFO-file which was opened by os.open(infifo, os.O_WRONLY), the server
        | begins to print an empty string everytime it goes through the loop,
        | means it won't wait for a complete line to appear before continuing.
        | Why?

        It's at end of file. That's what happens when the write end of
        a pipe closes, and there's no data left: subsequent reads return
        end of file, which in Python is an empty string.

        You may close it and open it again at this point.

        Donn Cave, donn@drizzle.co m

        Comment

        • Steve Holden

          #5
          Re: FIFO problems

          "Tobias Pfeiffer" <BoteDesSchatte ns@web.de> wrote in message
          news:bjp0sg$li4 db$1@ID-162581.news.uni-berlin.de...[color=blue]
          > Hi!
          >
          > I want to write a "client-server-application" (only running on the same
          > machine) or actually I've already begun with and have problems with the
          > interprocess communication. The server, when started, opens a FIFO and
          > opens it with open(infifo, 'r'). Then I check the content of the file
          > with
          >
          > while 1:
          > line = serverIn.readli ne()[:-1]
          > if line == "bla":
          > do this
          > else:
          > print line
          >[/color]

          The problem here is that you aren't testing correctly for and end-of-file
          condition.

          The slice notation you use to "remove the line terminator" unfortunately
          gives the same result for an empty line (one containing only a line
          terminator) and end-of-file (which returns a line containing no characters
          at all).

          There are various ways around this. Since you are talking interactive
          multi-process stuff here it's probably safest to do somehting like the
          following (untested) code:

          while 1:
          line = serverIn.readli ne()
          if not line:
          break
          del line[:-1]
          if line == "bla":
          do something incredibly interesting
          else:
          print line

          regards
          --
          Steve Holden http://www.holdenweb.com/
          Python Web Programming http://pydish.holdenweb.com/pwp/



          Comment

          • Duncan Booth

            #6
            Re: FIFO problems

            "Steve Holden" <sholden@holden web.com> wrote in
            news:tJZ7b.67$o 71.53@news2.cen tral.cox.net:
            [color=blue]
            > There are various ways around this. Since you are talking interactive
            > multi-process stuff here it's probably safest to do somehting like the
            > following (untested) code:
            >
            > while 1:
            > line = serverIn.readli ne()
            > if not line:
            > break
            > del line[:-1]
            > if line == "bla":
            > do something incredibly interesting
            > else:
            > print line[/color]

            Untested is right. Your 'del' statement is a little bit too destructive, or
            at least it would be if strings were mutable.

            Here's my (equally untested) alternative:

            for line in serverIn:
            line = line[:-1]
            if line == "bla":
            do something incredibly interesting
            else:
            print line



            --
            Duncan Booth duncan@rcp.co.u k
            int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
            "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

            Comment

            • Steve Holden

              #7
              Re: FIFO problems

              "Duncan Booth" <duncan@NOSPAMr cp.co.uk> wrote in message
              news:Xns93F388B 7855duncanrcpco uk@127.0.0.1...[color=blue]
              > "Steve Holden" <sholden@holden web.com> wrote in
              > news:tJZ7b.67$o 71.53@news2.cen tral.cox.net:
              >[color=green]
              > > There are various ways around this. Since you are talking interactive
              > > multi-process stuff here it's probably safest to do somehting like the
              > > following (untested) code:
              > >
              > > while 1:
              > > line = serverIn.readli ne()
              > > if not line:
              > > break
              > > del line[:-1]
              > > if line == "bla":
              > > do something incredibly interesting
              > > else:
              > > print line[/color]
              >
              > Untested is right. Your 'del' statement is a little bit too destructive,[/color]
              or[color=blue]
              > at least it would be if strings were mutable.
              >[/color]
              :-) Well, I can only remind myself that "the person who never made a mistake
              never made anything".
              [color=blue]
              > Here's my (equally untested) alternative:
              >
              > for line in serverIn:
              > line = line[:-1]
              > if line == "bla":
              > do something incredibly interesting
              > else:
              > print line
              >[/color]

              Unfortunately this doesn't include the end-of-file test that caused me to
              write my original incorrect code. If it did, however, I'm sure it would work
              ;-)

              regards
              --
              Steve Holden http://www.holdenweb.com/
              Python Web Programming http://pydish.holdenweb.com/pwp/



              Comment

              • Tobias Pfeiffer

                #8
                Re: FIFO problems

                Hi!

                "Steve Holden" <sholden@holden web.com> wrote in
                news:tJZ7b.67$o 71.53@news2.cen tral.cox.net:
                [color=blue][color=green]
                >> while 1:
                >> line = serverIn.readli ne()[:-1]
                >> if line == "bla":
                >> do this
                >> else:
                >> print line
                >>[/color]
                >
                > The problem here is that you aren't testing correctly for and
                > end-of-file condition.[/color]

                So the end-of-file-thingy also ends a line for the readline-command, I
                suppose? OK, would make sense for normal file read-processes... *grin*
                [color=blue]
                > The slice notation you use to "remove the line terminator"
                > unfortunately gives the same result for an empty line (one
                > containing only a line terminator) and end-of-file (which returns a
                > line containing no characters at all).
                >
                > There are various ways around this. Since you are talking
                > interactive multi-process stuff here it's probably safest to do
                > somehting like the following (untested) code:
                >
                > while 1:
                > line = serverIn.readli ne()
                > if not line:
                > break[/color]

                as changed:
                line = line[:-1]
                [color=blue]
                > if line == "bla":
                > do something incredibly interesting
                > else:
                > print line[/color]

                In which case will the "if not line" condition be true? When there is
                an end-of-file? But I just don't want it to break, I want it to
                continue and wait for a somewhat useful line, e.g. if another client
                connects.

                Bye and thanks for your help
                Tobias

                Comment

                Working...