[Python-ideas] Async API: some code to review
Steve Dower
Steve.Dower at microsoft.com
Wed Oct 31 16:51:35 CET 2012
Guido van Rossum wrote:
> This is also one of the advantages of yield-from; you *never* go back to the end
> of the ready queue just to invoke another layer of abstraction. (Steve tries to
> approximate this by running the generator immediately until the first yield, but
> the caller still ends up suspending to the scheduler, because they are using
> yield which doesn't avoid the suspension, unlike yield-from.)
This is easily changed by modifying lines 141 and 180 of scheduler.py to call _step() directly instead of requeuing it. The reason why it currently requeues the task is that there is no guarantee that the caller wanted the next step to occur in the same scheduler, whether because the completed operation or a previous one continued somewhere else. (I removed the option to attach this information to the Future itself, but it is certainly of value in some circumstances, though mostly involving threads and not necessarily sockets.)
The change I would probably make here is to test self.target and only requeue if it is different to the current scheduler (alternatively, a scheduler could implement its submit() to do this). Yes, this adds a little more overhead, but I'm still convinced that in general the operations being blocked on will take long enough for it to be insignificant. (And of course using a mechanism to bypass the decorator and use 'yield from' also avoids this overhead, though it potentially changes the program's behaviour).
Cheers,
Steve
More information about the Python-ideas
mailing list