LinkRead documentation states:
LinkReadwill wait until it has read a complete expression before returning.
You can test whether an expression is ready to be read from a particular link using
Therefore one must be careful with
LinkRead. Trying to read from a link which has nothing on it will lock up the system (and in my experience aborting in such a situation can render the link unusable).
LinkReadyQ documentation states,
LinkReadyQ[link]tests whether there is an expression ready to read from the specified WSTP connection.
LinkRead[link]will not block under any normal circumstances.
Yet in the following example,
LinkRead does block, even though
LinkReadyQ said yes. What is happening here, and what am I doing wrong?
For this example, you will need two kernels running, (Kernel 1) and (Kernel 2).
From (Kernel 1),
link = LinkCreate["mylink"] (* LinkObject["mylink", 1065, 13] *) LinkReadyQ[link] (* False *)
Now from (Kernel 2),
link = LinkConnect["mylink"] (* LinkObject["mylink", 537, 4] *)
From (Kernel 1),
LinkReadyQ[link] (* True *) LinkRead[link] (* This call blocks!! *)
This call did not return. I had no choice but to abort it. Now if I try any operations on the link in (Kernel 1), I get an error:
In:= LinkReadyQ[link] During evaluation of In:= LinkObject::linkn: Argument LinkObject[mylink,1065,13] in LinkReadyQ[LinkObject[mylink,1065,13]] has an invalid LinkObject number; the link may be closed. Out= LinkReadyQ[LinkObject["mylink", 1065, 13]]
In (Kernel 2) I also get an error:
In:= LinkWrite[link, 1] During evaluation of In:= LinkConnect::linkc: Unable to connect to LinkObject[mylink,537,4]. Out= $Failed
Furthermore, I am unable to use the name
"mylink" again until I close (Kernel 1).
Another scenario where
True when it arguably shouldn't is
link = LinkConnect["foo"] (* foo is nonexistent *) LinkReadyQ[link] (* True *)
Perhaps this is a clear misuse of
This is related to How to implement LinkLaunch-like functionality with timeout?