This came up in relation to this answer
Is it possible to set a timeout for LinkWrite
? LinkWrite
seems to block until the sent data is read on the other end of the link. Can it block indefinitely or does it have a long timeout? If it doesn't have a default timeout, is it possible to set one?
An example
Let's create two new kernels in the front end (this might overwrite your existing kernels, but it will be reversed after restarting the front end):
SetOptions[$FrontEndSession,
EvaluatorNames -> {"Local" -> {"AutoStartOnLaunch" -> True},
"K1" -> {"AutoStartOnLaunch" -> False},
"K2" -> {"AutoStartOnLaunch" -> False}}]
Then open two new notebooks, and in the first one evaluate
SetOptions[EvaluationNotebook[], Evaluator -> "K2"]
link = LinkCreate["alink"]
LinkWrite[link, "boo"]
In the second one evaluate
SetOptions[EvaluationNotebook[], Evaluator -> "K1"]
link = LinkConnect["alink"]
LinkRead[link]
LinkWrite
in the first one will only return after LinkRead
has finished evaluating in the second one.
If the answer is that it's not possible to set a timeout, that can be reasonable too. I was wondering about this: suppose we're running a long parallel calculation, and we interrupt the main kernel to enter a subsession and inspect the state (Evaluation -> Interrupt -> Enter subsession
). Is it possible that one of the parallel kernels will time out while trying to send the result, and the calculation won't be able to resume after returning from the subsession?
Parallel`Settings`$MathLinkTimeout
and has a default value of 15 seconds. May I also suggest that you consider changing the title of this question, given thatLinkWrite
is non-blocking on a properly initialized MathLink connection? (To flush the message queues, one would useLinkFlush
, which is blocking until the messages have been sent.) $\endgroup$Dialog[]
. I still don't feel confident enough about this interrupting a long calculation and going into a dialog, but at least in those simple tests I did it seemed to work without issues. $\endgroup$