<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jun 6, 2016 at 1:54 PM, Glyph <span dir="ltr"><<a href="mailto:glyph@twistedmatrix.com" target="_blank">glyph@twistedmatrix.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br><div style="word-wrap:break-word"><span class=""><br><div><blockquote type="cite"><div>On Jun 6, 2016, at 08:29, Guido van Rossum <<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>> wrote:</div><br><div><div>On Sun, Jun 5, 2016 at 10:16 PM, Glyph <<a href="mailto:glyph@twistedmatrix.com" target="_blank">glyph@twistedmatrix.com</a>> wrote:<br><blockquote type="cite"><br>On Jun 4, 2016, at 13:25, Ben Darnell <<a href="mailto:ben@bendarnell.com" target="_blank">ben@bendarnell.com</a>> wrote:<br><br>If things are so sensitive to minor changes in timing, doesn't that set the<br>bar impossibly high for interoperability?<br><br><br>The sensitivity is not to changes in timing - i.e. when the wall-clock runs,<br>or ordering of non-deterministically ordered events - but rather to<br>reentrancy - whether certain things complete synchronously while the caller<br>is still on the stack and can depend on them having done so upon return.<br><br>The recommended way of writing tests within Twisted these days depends<br>heavily on `.callback´ synchronously resolving a Deferred, which is what<br>adding a call_soon breaks.<br></blockquote><br>That's interesting, and also potentially worrisome (for interop, I'm<br>not saying Twisted is wrong here).<br><br>I think asyncio depends on the opposite: that if you add a callback to<br>a Future that's ready it does *not* immediately run. Asyncio's promise<br>is pretty strongly that callbacks are serialized (no callbacks running<br>inside other callbacks). IIRC we experimented with other semantics and<br>found that it was harder to reason about. (IMO if you *know* a Future<br>is ready why add a callback to it rather than just calling the damn<br>thing if that's what you want?)</div></div></blockquote><br></div></span><div>I don't think Twisted is necessarily <i>right</i> here either.  You're absolutely right that it's easier to reason about reentrancy in some cases if you have a might-be-fired-might-not Future vs. the same sort of Deferred.  I like the property where you can do:</div><div><br></div><div>def test(test_case):</div><div>    a = asynchronously_something()</div><div>    test_case.this_must_not_have_a_result(a)</div><div>    cause_a_to_fire()</div><div>    test_case.assertEqual(test_case.this_must_have_a_result(a), something)</div><div><br></div><div>but this is (somewhat) opposed to the fact that call_soon means you never get the nasty surprise where the callback added in the middle of a function gets run before the rest of it does.  So I think there are good properties in both cases and given some thought it is probably possible to map between them, but Deferred is lower-level here in the sense that it provides a way to do this both with the event loop and without.  You can always call_soon(deferred.callback) but you can't go the other way and force a Future to resolve synchronously - right?</div><span class="HOEnZb"></span></div></blockquote></div><br></div><div class="gmail_extra">Right. I'm still unclear on what the compelling use case for that is (other than that Twisted has always done this). Is it performance? Is it callback ordering?<br><br></div><div class="gmail_extra">I suppose Deferred has a method to mark it done. Does that immediately run the callbacks? Or does it come in two flavors? Can a Deferred that's marked done ever revert back to being not done? (I believe I once read the Deferred code enough to be able to find the answers, but I'm afraid I've never really needed what I learned then, so I've forgotten...)<br clear="all"></div><div class="gmail_extra"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>