[Python-ideas] The async API of the future: Twisted and Deferreds

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Oct 14 02:17:05 CEST 2012

Itamar Turner-Trauring wrote:

> For example, consider the following code; silly, but buggy due to the 
> context switch in yield allowing race conditions if any other code 
> modifies counter.value while getResult() is waiting for a result.
>    def addToCounter():
>         counter.value = counter.value + (yield getResult())

But at least you can *see* from the presence of the 'yield'
that suspension can occur.

PEP 380 preserves this, because anything that can yield has
to be called using 'yield from', so the potential suspension
points remain visible.

> That being said, perhaps some changes to Python syntax could solve this; 
> Allen Short 
> (http://washort.twistedmatrix.com/2012/10/coroutines-reduce-readability.html) 
> claims to have a proposal, hopefully he'll post it soon.

He argues there that greenlet-style coroutines are bad because
suspension can occur anywhere without warning. He likes
generators better, because the 'yield' warns you that suspension
might occur. Generators using 'yield from' have the same property.

If his proposal involves marking the suspension points somehow, then
syntactically it will probably be very similar to yield-from.


More information about the Python-ideas mailing list