From: Max Maischein Date: 2002-06-28T19:38:28+09:00 Subject: Transparent asynchronous execution Hello all ! I've read some interesting parts of the E language description (http://www.skyhunter.com/marcs/ewalnut.html), and while the language itself seems to solve problems I don't have, the idea of Promises seems to be useful to me, as it would allow an easy way to introduce parallelism into programs where it is affordable / possible. What I want is simple - I want a class that is used like this : foo = Promise(1,2,3) { |a,b,c| r1 = some_function_that_takes_long_to_compute( a,b,c ) r2 = some_other_function_that_takes_even_longer( r1 ) r2 } # => runs in background thread, foreground thread will block on access to foo bar = a_third_function_that_takes_long_to_compute( 1,2,3 ) print calculate_statistics( bar, foo ) That is, the evaluation of foo is done in a second thread in the background, and the foreground thread blocks if the result of the calculation is not yet available. In principle, this is not different from the following code : t = Thread.new( 1,2,3 ) { |a,b,c| r1 = some_function_that_takes_long_to_compute( a,b,c ) r2 = some_other_function_that_takes_even_longer( r1 ) r2 }; bar = a_third_function_that_takes_long_to_compute( 1,2,3 ) print calculate_statistics(bar, t.join()) except that I find this cumbersome - I would have to sprinkle Thread.new() and t.join() everywhere, when all I (as the front-end user) care about is the result and not how I get the result, and I have to keep the Thread object around even after the result became available. Switching from this implementation to a non-parallel implementation is also not nice, since I have to remove the threading stuff from everywhere, instead of just overwriting the Promise class with a plain serial Promise class that directly evaluates the given block and stores the result. For the implementation, almost everything seems to go smoothly except for the delegation part. Before the result becomes available, I want the Promise class to block the calling thread on every message sent to it (this could maybe done by overriding Object#send), and after the result becomes available, I want the Promise to send all messages to the result (i.e., I want the Promise to be as transparent to the caller as possible). I've looked at the Delegator class, but this class wants to know which object it delegates to - the Promise dosen't know which object it will delegate to until the result becomes available - how can I approach this problem ? It's OK for me if errors (like "method not found") get raised after the Promise has completed the calculation - this is the price one pays for not using a language with down-to-the-byte typed elements. Thanks, -max Thinking further about this, there could be three versions of a Promise, the SynchronousPromise, which directly evaluates the given block, the ThreadPromise, which runs the given block in the background, possibly blocking the foreground thread if it wants the result too early, and the LazyPromise, which evaluates the given block exactly when the foreground thread sends a message to the Promise...