FIFO problems

Discussion in 'Python' started by Tobias Pfeiffer, Sep 11, 2003.

  1. 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.readline()[:-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.readline()[:-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, Sep 11, 2003
    #1
    1. Advertising

  2. Hi!

    Sorry, big mistake!!

    Tobias Pfeiffer <> wrote in
    news:bjp0sg$li4db$-berlin.de:

    > these clients exists, no problem. But when the last process exists


    EXITS, not exists...

    Bye
    Tobias
     
    Tobias Pfeiffer, Sep 11, 2003
    #2
    1. Advertising

  3. Tobias Pfeiffer

    Gary Herron Guest

    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:
    > 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.readline()[:-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.readline()[:-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
     
    Gary Herron, Sep 11, 2003
    #3
  4. Tobias Pfeiffer

    Donn Cave Guest

    Quoth Tobias Pfeiffer <>:
    ....
    | 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 Cave, Sep 11, 2003
    #4
  5. Tobias Pfeiffer

    Steve Holden Guest

    "Tobias Pfeiffer" <> wrote in message
    news:bjp0sg$li4db$-berlin.de...
    > 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.readline()[:-1]
    > if line == "bla":
    > do this
    > else:
    > print line
    >


    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.readline()
    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/
     
    Steve Holden, Sep 11, 2003
    #5
  6. Tobias Pfeiffer

    Duncan Booth Guest

    "Steve Holden" <> wrote in
    news:tJZ7b.67$:

    > 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.readline()
    > if not line:
    > break
    > del line[:-1]
    > if line == "bla":
    > do something incredibly interesting
    > else:
    > print line


    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
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Sep 11, 2003
    #6
  7. Tobias Pfeiffer

    Steve Holden Guest

    "Duncan Booth" <> wrote in message
    news:Xns93F388B7855duncanrcpcouk@127.0.0.1...
    > "Steve Holden" <> wrote in
    > news:tJZ7b.67$:
    >
    > > 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.readline()
    > > if not line:
    > > break
    > > del line[:-1]
    > > if line == "bla":
    > > do something incredibly interesting
    > > else:
    > > print line

    >
    > Untested is right. Your 'del' statement is a little bit too destructive,

    or
    > at least it would be if strings were mutable.
    >

    :) Well, I can only remind myself that "the person who never made a mistake
    never made anything".

    > Here's my (equally untested) alternative:
    >
    > for line in serverIn:
    > line = line[:-1]
    > if line == "bla":
    > do something incredibly interesting
    > else:
    > print line
    >


    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/
     
    Steve Holden, Sep 11, 2003
    #7
  8. Hi!

    "Steve Holden" <> wrote in
    news:tJZ7b.67$:

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

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


    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*

    > 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.readline()
    > if not line:
    > break


    as changed:
    line = line[:-1]

    > if line == "bla":
    > do something incredibly interesting
    > else:
    > print line


    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
     
    Tobias Pfeiffer, Sep 11, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page