[Python-Dev] Musings on concurrency and scoping ("replacing" Javascript)
Bob Ippolito
bob at redivi.com
Fri Jul 7 02:22:57 CEST 2006
On Jul 6, 2006, at 5:04 PM, Ka-Ping Yee wrote:
> On Thu, 6 Jul 2006, Phillip J. Eby wrote:
>> As much as I'd love to have the nested scope feature, I think it's
>> only
>> right to point out that the above can be rewritten as something
>> like this
>> in Python 2.5:
>>
>> def spam():
>> local_A = do_work()
>> result_1 = yield do_network_transaction()
>> local_B = do_work(result_1)
>> result_2 = yield do_network_transaction()
>> do_work(local_A, local_B, result_1, result_2)
>> ...
>>
>> All you need is an appropriate trampoline (possibly just a
>> decorator) that
>> takes the objects yielded by the function, and uses them up to set up
>> callbacks that resume the generator with the returned result.
>
> Clever! Could you help me understand what goes on in
> do_network_transaction() when you write it this way? In the
> Firefox/JavaScript world, the network transaction is fired off
> in another thread, and when it's done it posts an event back
> to the JavaScript thread, which triggers the callback.
>
> And what happens if you want to supply more than one continuation?
> In my JavaScript code i'm setting up two continuations per step --
> one for success and one for failure, since with a network you never
> know what might happen.
When you have a failure the yield expression raises an exception
instead of returning a result.
-bob
More information about the Python-Dev
mailing list