ItGo.me Focus on IT Recommend

Home > asynchronous - Avoiding recursion when reading/writing a port synchronously?

asynchronous - Avoiding recursion when reading/writing a port synchronously?

All port operations in Rebol 3 are asynchronous. The only way I can find to do synchronous communication is calling wait.

But the problem with calling wait in this case is that it will check events for all open ports (even if they are not in the port block passed to wait). Then they call their responding event handlers, but a read/write could be done in one of those event handlers. That could result in recursive calls to "wait".

How do I get around this?

asynchronous io rebol rebol3
|
  this question
edited Nov 13 '13 at 15:24 HostileFork 22.2k 6 61 117 asked Nov 13 '13 at 14:43 Shixin Zeng 818 6 13 8   Actually, I don't think there is a solution to this in current R3 implementation, so I went ahead to add a "/only" refinement to "wait", with which, it will only wait on the ports provided to "wait", and thus avoid the recursive calls. See my pull request at: github.com/rebol/rebol/pull/177 –  Shixin Zeng Feb 16 '14 at 16:51 1   Out of curiousity, why do you need it to be synchronous? –  toadzky Mar 9 '15 at 13:49 1   There are a lot of situations that coding with synchronous port is much easier: suppose you want to send an email with a click on a button, and report if it succeeds or fails. It's much easier to wait for it to be finished before doing anything else. –  Shixin Zeng Mar 9 '15 at 14:40 1   do you absolutely have to use Rebol? –  Rivenfall May 17 '15 at 11:32 1   Yes. This is actually more a question about Rebol 3 than synchronous communication in general. –  Shixin Zeng May 18 '15 at 18:12

 | 

3 Answers
3

in cases where there are only asynchronous events and we are in need on synchronous reply, start a timer or sleep for timeout, if the handler or required objective is met then say true, else false and make sure the event gets cancelled /reset for the same if critical.


|
  this answer
answered Sep 3 '15 at 10:09 mkumar 95 10

 | 

Why don´t you create a kind of "Buffer" function to receive all messages from assyncronous entries and process them as FIFO (first-in, first-out)?

This way you may keep the Assync characteristics of your ports and process them in sync mode.


|
  this answer
answered Sep 8 '15 at 13:55 David BS 1,486 1 6 17

 | 

You can just use a lock. Cummunication1 can set some global lock state i.e. with a variable (be sure that it's thread safe). locked = true. Then Communication2 can wait until it's unlocked.

loop do
    sleep 10ms
    break if not locked
end
locked = true
handle_communication()

|
  this answer
answered May 17 '15 at 11:37 Rivenfall 448 4 10 1   This is actually more a question about Rebol 3 than synchronous communication in general. –  Shixin Zeng May 18 '15 at 18:14

 | 

protected by Community♦ Sep 1 '15 at 5:20

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged asynchronous io rebol rebol3 or ask your own question.

oriUlr:http://stackoverflow.com/questions/19956665/avoiding-recursion-when-reading-writing-a-port-synchronously

------splitte line----------------------------