pipes and blocking read

For the Compleat Fan
Locked
starseed
Posts: 38
Joined: Thu Jul 27, 2006 8:45 pm

pipes and blocking read

Post by starseed »

When using pipes, read is blocking.
Is there a way to check for available data, before trying to read?
From the help file, peek seems not to work, and when I try
(peek ((pipe) 0))
I get "invalid function (peek ((pipe) 0))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

'peek' is not available on Win32 only on UNIX like OSs.

Lutz

starseed
Posts: 38
Joined: Thu Jul 27, 2006 8:45 pm

Post by starseed »

Right, there it is, "only available for Unix like systems" ...

And how about the real question behind it? How can I work with pipes without blocking?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

It very much depends what kind of application your are building.

Normally one part is the controlling part, i.e. in an GUI application the GUI could be sending the request and the other process could wait in a blocking loop for commands. This program is a good example for this:

http://newlisp.org/index.cgi?page=Tk_and_newLISP

newLISP starts Tcl/Tk then waits for input generated when the mouse is moved a button is pressed etc.

If this doesn't work for you than use other channels of communications, like or UDP Tcp/Ip together with 'select' to inquire the status of the channel.

Another possibility is, to let the controlling application send some kind of heart beat which keeps the waiting loop on the other side moving and with the abbiity to do other work while waiting. The receiving pipe unblocks on line-feeds. Running of a timer the other application could send a line-feed every 50 milli seconds. The loop on the receiving end could run like a state machine.

You also could use threads (not on Win32) or child processes (also on Win32) communicating via shared memory and synchronize using semaphores.

Lutz

Locked