newLISP Fan Club

Forum => Anything else we might add? => Topic started by: starseed on August 02, 2006, 03:10:47 PM

Title: pipes and blocking read
Post by: starseed on August 02, 2006, 03:10:47 PM
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))
Title:
Post by: Lutz on August 02, 2006, 03:53:57 PM
'peek' is not available on Win32 only on UNIX like OSs.



Lutz
Title:
Post by: starseed on August 02, 2006, 11:25:33 PM
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?
Title:
Post by: Lutz on August 03, 2006, 05:14:38 AM
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