Read from socket async?

Dave Cragg dcragg at lacscentre.co.uk
Sun Feb 10 06:17:00 EST 2002


At 5:58 pm +1100 10/2/02, David Vaughan wrote:
>In a post which I accidentally lost Scott Raney said to Gary Denis
>that "read from...with message" is asynchronous. In what respect,
>please?
>
>I ran the following script
>
>local lcounter
>
>on mouseUp
>   global sname
>   put zero into lcounter
>   repeat 10
>     read from socket sname until return with message "dataAdded"
>     add 1 to lcounter
>     put lcounter into field "f2"
>   end repeat
>end mouseUp
>
>on dataAdded portName,info
>   wait for 1 seconds
>   put info after field "f1"
>   subtract 1 from lcounter
>   put lcounter into field "f2"
>end dataAdded
>
>Consistent with other behaviour of Rev, the counter visibly counted
>up to ten in the repeat loop then counted back down again, as the
>stacked "dataAdded" messages were executed after the mouseUp handler
>finally exited. I would call this synchronous behaviour, consistent
>with the blocking behaviour of all Rev handlers. It is exactly as if
>one did not use the msgname option but followed the "read" with
>"send dataAdded  && it to me". Am I correct, Rev people?

I think one other proviso as to be aded to Scott's description of the 
asynch nature of <read .... with message> and <send mesaage... in> 
commands. Any queued messages will only execute after any currently 
running handler completes. (Please correct me if I've got this 
wrong.) So in this simple example, you'll have to wait 1 second for 
the doIt handler to complete.

on mouseUp
   send "doIt" to me in 10 milliseconds
   put 1
   wait 1 second
end mouseUp

on doIt
   put 2
end doIt

In your example, your repeat loop must complete before any of the 
dataAdded messages will finish. This says to me that "read .. with 
message" is truly non-blocking, i.e. the handler carries on.

It may not be what you want, but  I prefer the reassurance of knowing 
that a handler will complete before any queued stuff is going to 
happen. (I find asynch stuff scary at the best of times.) And in 
those cases where you want more control over the sequencing, there 
are the "wait until ... with messages" and "wait for messages" 
commands. For example, try this slightly modified version of your 
script (untried):

on mouseUp
   global sname
   put zero into lcounter
   repeat 10
     read from socket sname until return with message "dataAdded"
     add 1 to lcounter
     wait until lcCounter = (lcCounter -1) with messages ##this is the block
     put lcounter into field "f2"
   end repeat
end mouseUp

on dataAdded portName,info
   wait for 1 seconds
   put info after field "f1"
   subtract 1 from lcounter
   put lcounter into field "f2"
end dataAdded

Cheers
Dave Cragg



More information about the Use-livecode mailing list