FIFO problems

T

Tobias Pfeiffer

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
 
G

Gary Herron

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
 
D

Donn Cave

Quoth Tobias Pfeiffer <[email protected]>:
....
| 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, (e-mail address removed)
 
S

Steve Holden

Tobias Pfeiffer said:
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
 
D

Duncan Booth

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
 
S

Steve Holden

Duncan Booth said:
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
 
T

Tobias Pfeiffer

Hi!

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top