Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > FIFO problems

Reply
Thread Tools

FIFO problems

 
 
Tobias Pfeiffer
Guest
Posts: n/a
 
      09-11-2003
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
 
Reply With Quote
 
 
 
 
Tobias Pfeiffer
Guest
Posts: n/a
 
      09-11-2003
Hi!

Sorry, big mistake!!

Tobias Pfeiffer <(E-Mail Removed)> wrote in
news:bjp0sg$li4db$(E-Mail Removed)-berlin.de:

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


EXITS, not exists...

Bye
Tobias
 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      09-11-2003
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




 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      09-11-2003
Quoth Tobias Pfeiffer <(E-Mail Removed)>:
....
| 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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      09-11-2003
"Tobias Pfeiffer" <(E-Mail Removed)> wrote in message
news:bjp0sg$li4db$(E-Mail Removed)-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/



 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      09-11-2003
"Steve Holden" <(E-Mail Removed)> wrote in
news:tJZ7b.67$(E-Mail Removed):

> 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 (E-Mail Removed)
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?
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      09-11-2003
"Duncan Booth" <(E-Mail Removed)> wrote in message
news:Xns93F388B7855duncanrcpcouk@127.0.0.1...
> "Steve Holden" <(E-Mail Removed)> wrote in
> news:tJZ7b.67$(E-Mail Removed):
>
> > 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/



 
Reply With Quote
 
Tobias Pfeiffer
Guest
Posts: n/a
 
      09-11-2003
Hi!

"Steve Holden" <(E-Mail Removed)> wrote in
news:tJZ7b.67$(E-Mail Removed):

>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FIFO, FIFO, It's Off To Queue We Go... Lawrence D'Oliveiro NZ Computing 7 05-09-2009 08:37 AM
any body having complete code for synchronous fifo or know a link where fifo codes are available plz help chaitu VHDL 1 06-01-2007 07:03 PM
any body having complete code for a synchronous FIFO or know a link where FIFO codes are available chaitu VHDL 1 06-01-2007 03:45 AM
any body having complete code for a synchronous FIFO or know a link where FIFO codes are available chaitu VHDL 1 05-31-2007 03:31 PM
any body having complete code for a synchronous FIFO or know a link where FIFO codes are available chaitu VHDL 0 05-31-2007 02:28 PM



Advertisments