Ticket #4568: core-lowlevel.xhtml.patch
| File core-lowlevel.xhtml.patch, 17.1 KB (added by jdb, 3 years ago) |
|---|
-
doc/core/howto/defer.xhtml
diff --git a/doc/core/howto/defer.xhtml b/doc/core/howto/defer.xhtml index 21da54e..2a131d3 100644
a b 38 38 <h2>Deferreds</h2> 39 39 40 40 <p>Twisted uses the <code class="API" 41 base="twisted.internet.defer">Deferred</code> object to manage the callback 42 sequence. The client application attaches a series of functions to the 43 deferred to be called in order when the results of the asychronous request are 44 available (this series of functions is known as a series of 45 <strong>callbacks</strong>, or a <strong>callback chain</strong>), together 46 with a series of functions to be called if there is an error in the 47 asychronous request (known as a series of <strong>errbacks</strong> or an 48 <strong>errback chain</strong>). The asychronous library code calls the first 49 callback when the result is available, or the first errback when an error 50 occurs, and the <code>Deferred</code> object then hands the results of each 51 callback or errback function to the next function in the chain.</p> 41 base="twisted.internet.defer">Deferred</code> object to manage the 42 callback sequence. The client application attaches a series of 43 functions to the deferred to be called in order when the results of 44 the asychronous request are available (this series of functions is 45 known as a series of <strong>callbacks</strong>, or a <strong>callback 46 chain</strong>), together with a series of functions to be called if 47 there is an error in the asychronous request (known as a series 48 of <strong>errbacks</strong> or an <strong>errback 49 chain</strong>). The asychronous library code calls the first callback 50 when the result is available, or the first errback when an error 51 occurs, and the <code>Deferred</code> object then hands the results of 52 each callback or errback function to the next function in the 53 chain.</p> 52 54 53 55 <h2>Callbacks</h2> 54 56 … … 296 298 d.addCallbacks(callback2, errback2) 297 299 </pre> 298 300 299 <p>If an error occurs in <code class="python">callback1</code>, then for Case 1300 <code class="python">errback1</code> will be called with the failure. For Case 301 2, <code class="python">errback2</code> will be called. Be careful with your 302 call backs and errbacks.</p>301 <p>If an error occurs in <code class="python">callback1</code>, then 302 for Case 1 <code class="python">errback1</code> will be called with 303 the failure. For Case 2, <code class="python">errback2</code> will be 304 called. Be careful with your callbacks and errbacks.</p> 303 305 304 <p>What this means in a practical sense is in Case 1, "A" will 305 handle a success condition from <code>getDeferredFromSomewhere</code>, and 306 "B" will handle any errors that occur <em>from either the upstream 307 source, or that occur in 'A'</em>. In Case 2, "C"'s errback1 308 <em>will only handle an error condition raised by 309 <code>getDeferredFromSomewhere</code></em>, it will not do any handling of 310 errors raised in callback1.</p> 306 <p>What this means in a practical sense is in Case 1, "A" 307 will handle a success condition 308 from <code>getDeferredFromSomewhere</code>, and "B" will 309 handle any errors that occur <em>from either the upstream source, or 310 that occur in 'A'</em>. In Case 2, "C"'s errback1 <em>will 311 only handle an error condition raised 312 by </em> <code>getDeferredFromSomewhere</code>, it will not do any 313 handling of errors raised in callback1.</p> 311 314 312 315 313 316 <h3>Unhandled Errors</h3> … … 390 393 return d 391 394 </pre> 392 395 393 <p> Our original implementation of <code>authenticateUser</code> expected 394 <code>isValidUser</code> to be synchronous, but now we need to change it to handle both 395 synchronous and asynchronous implementations of <code>isValidUser</code>. For this, we 396 use <code class="API" base="twisted.internet.defer">maybeDeferred</code> to 397 call <code>isValidUser</code>, ensuring that the result of <code>isValidUser</code> is a Deferred, 398 even if <code>isValidUser</code> is a synchronous function: 396 <p> Our original implementation of <code>authenticateUser</code> 397 expected <code>isValidUser</code> to be synchronous, but now we need 398 to change it to handle both synchronous and asynchronous 399 implementations of <code>isValidUser</code>. For this, we 400 use <code class="API" 401 base="twisted.internet.defer">maybeDeferred</code> to 402 call <code>isValidUser</code>, ensuring that the result 403 of <code>isValidUser</code> is a Deferred, even 404 if <code>isValidUser</code> is a synchronous function: 399 405 </p> 400 406 401 407 <pre class="python"> … … 413 419 </pre> 414 420 415 421 <p> 416 Now <code>isValidUser</code> could be either <code>synchronousIsValidUser</code> or 417 <code>asynchronousIsValidUser</code>. 422 Now <code>isValidUser</code> could be 423 either <code>synchronousIsValidUser</code> 424 or <code>asynchronousIsValidUser</code>. 418 425 </p> 419 426 420 427 <p>It is also possible to modify <code>synchronousIsValidUser</code> to return … … 436 443 dl = defer.DeferredList([deferred1, deferred2, deferred3]) 437 444 </pre> 438 445 439 <p>You can now treat the DeferredList like an ordinary Deferred; you can call 440 <code>addCallbacks</code> and so on. The DeferredList will call its callback 441 when all the deferreds have completed. The callback will be called with a list 442 of the results of the Deferreds it contains, like so:</p> 446 <p>You can now treat the DeferredList like an ordinary Deferred; you 447 can call <code>addCallbacks</code> and so on. The DeferredList will 448 call its callback when all the deferreds have completed. The callback 449 will be called with a list of the results of the Deferreds it 450 contains, like so:</p> 443 451 444 452 <pre class="python"> 445 453 def printResult(result): … … 518 526 519 527 <h3>Other behaviours</h3> 520 528 521 <p>DeferredList accepts three keyword arguments that modify its behaviour: 522 <code>fireOnOneCallback</code>, <code>fireOnOneErrback</code> and 523 <code>consumeErrors</code>. If <code>fireOnOneCallback</code> is set, the 524 DeferredList will immediately call its callback as soon as any of its Deferreds 525 call their callback. Similarly, <code>fireOnOneErrback</code> will call errback 526 as soon as any of the Deferreds call their errback. Note that DeferredList is 527 still one-shot, like ordinary Deferreds, so after a callback or errback has been 528 called the DeferredList will do nothing further (it will just silently ignore 529 any other results from its Deferreds).</p> 529 <p>DeferredList accepts three keyword arguments that modify its 530 behaviour: <code>fireOnOneCallback</code>, <code>fireOnOneErrback</code> 531 and <code>consumeErrors</code>. If <code>fireOnOneCallback</code> is 532 set, the DeferredList will immediately call its callback as soon as 533 any of its Deferreds call their callback. 534 Similarly, <code>fireOnOneErrback</code> will call errback as soon as 535 any of the Deferreds call their errback. Note that DeferredList is 536 still one-shot, like ordinary Deferreds, so after a callback or 537 errback has been called the DeferredList will do nothing further (it 538 will just silently ignore any other results from its Deferreds).</p> 530 539 531 540 <p>The <code>fireOnOneErrback</code> option is particularly useful when you 532 541 want to wait for all the results if everything succeeds, but also want to know 533 542 immediately if something fails.</p> 534 543 535 <p>The <code>consumeErrors</code> argument will stop the DeferredList from 536 propagating any errors along the callback chains of any Deferreds it contains 537 (usually creating a DeferredList has no effect on the results passed along the 538 callbacks and errbacks of their Deferreds). Stopping errors at the DeferredList 539 with this option will prevent <q>Unhandled error in Deferred</q> warnings from 540 the Deferreds it contains without needing to add extra errbacks<span 541 class="footnote">Unless of course a later callback starts a fresh error — 542 but as we've already noted, adding callbacks to a Deferred after its used in a 543 DeferredList is confusing and usually avoided.</span>. Passing a true value 544 for the <code>consumeErrors</code> parameter will not change the behavior of 545 <code>fireOnOneCallback</code> or <code>fireOnOneErrback</code>.</p> 544 <p>The <code>consumeErrors</code> argument will stop the DeferredList 545 from propagating any errors along the callback chains of any Deferreds 546 it contains (usually creating a DeferredList has no effect on the 547 results passed along the callbacks and errbacks of their Deferreds). 548 Stopping errors at the DeferredList with this option will 549 prevent <q>Unhandled error in Deferred</q> warnings from the Deferreds 550 it contains without needing to add extra 551 errbacks<span class="footnote">Unless of course a later callback 552 starts a fresh error — but as we've already noted, adding 553 callbacks to a Deferred after its used in a DeferredList is confusing 554 and usually avoided.</span>. Passing a true value for 555 the <code>consumeErrors</code> parameter will not change the behavior 556 of <code>fireOnOneCallback</code> 557 or <code>fireOnOneErrback</code>.</p> 546 558 547 559 <a name="class"></a> 548 560 … … 553 565 substitute for the docstrings in the Deferred class, but can provide guidelines 554 566 for its use.</p> 555 567 556 <p>There is a parallel overview of functions used by the Deferred's 557 <em>creator</em> in <a href="gendefer.xhtml#class">Generating Deferreds</a>.</p> 568 <p>There is a parallel overview of functions used by the 569 Deferred's <em>creator</em> 570 in <a href="gendefer.xhtml#class">Generating Deferreds</a>.</p> 558 571 559 572 <h3>Basic Callback Functions</h3> 560 573 -
doc/core/howto/deferredindepth.xhtml
diff --git a/doc/core/howto/deferredindepth.xhtml b/doc/core/howto/deferredindepth.xhtml index 9a7edfc..479a601 100644
a b 10 10 <h2>Introduction</h2> 11 11 12 12 <p>Deferreds are quite possibly the single most confusing topic that a 13 newcomer to Twisted has to deal with. I am going to forgo the normal talk14 about what deferreds are, what they aren't, and why they're used in Twisted. 15 Instead, I'm going show you the logic behind what they 16 <strong>do</strong>.</p>13 newcomer to Twisted has to deal with. I am going to forgo the normal 14 talk about what deferreds are, what they aren't, and why they're used 15 in Twisted. Instead, I'm going show you the logic behind what 16 they <strong>do</strong>.</p> 17 17 18 18 19 19 <p>A deferred allows you to encapsulate the logic that you'd normally use to … … 254 254 got result: damage control successful! 255 255 </pre> 256 256 257 <p>Two things to note here. First, "- A -" was skipped, like we wanted it to, 258 and the second thing is that after "- A -", noDecision is called, because 259 <strong>it is the next errback that exists in the chain</strong>. It returns a 260 non-failure, so processing continues with the next callback at "- B -", and 261 the errback at the end of the chain is never called </p> 257 <p>Two things to note here. First, "- A -" was skipped, like 258 we wanted it to, and the second thing is that after "- A -", 259 noDecision is called, because <strong>it is the next errback that 260 exists in the chain</strong>. It returns a non-failure, so processing 261 continues with the next callback at "- B -", and the errback 262 at the end of the chain is never called </p> 262 263 263 264 <h2>Hints, tips, common mistakes, and miscellaney</h2> 264 265 -
doc/core/howto/gendefer.xhtml
diff --git a/doc/core/howto/gendefer.xhtml b/doc/core/howto/gendefer.xhtml index d391ea0..47da1ba 100644
a b 120 120 d.addCallback(printNumber) 121 121 </pre> 122 122 123 <p>You will notice that despite creating a Deferred in the 124 <code>largeFibonnaciNumber</code> function, these things happened:</p> 123 <p>You will notice that despite creating a Deferred in 124 the <code>largeFibonnaciNumber</code> function, these things 125 happened:</p> 125 126 <ul> 126 127 <li>the "Total time taken for largeFibonnaciNumber call" output 127 128 shows that the function did not return immediately as asynchronous functions … … 286 287 287 288 <h3>Firing Deferreds more than once is impossible</h3> 288 289 289 <p>Deferreds are one-shot. You can only call <code>Deferred.callback</code> or 290 <code>Deferred.errback</code> once. The processing chain continues each time 291 you add new callbacks to an already-called-back-to Deferred.</p> 290 <p>Deferreds are one-shot. You can only 291 call <code>Deferred.callback</code> or <code>Deferred.errback</code> 292 once. The processing chain continues each time you add new callbacks 293 to an already-called-back-to Deferred.</p> 292 294 293 295 <h3>Synchronous callback execution</h3> 294 296 295 <p>If a Deferred already has a result available, addCallback 296 <strong>may</strong> call the callback synchronously: that is, immediately 297 after it's been added. In situations where callbacks modify state, it is 298 might be desirable for the chain of processing to halt until all callbacks are 299 added. For this, it is possible to <code>pause</code> and <code>unpause</code> 300 a Deferred's processing chain while you are adding lots of callbacks.</p> 297 <p>If a Deferred already has a result available, 298 addCallback <strong>may</strong> call the callback synchronously: that 299 is, immediately after it's been added. In situations where callbacks 300 modify state, it is might be desirable for the chain of processing to 301 halt until all callbacks are added. For this, it is possible 302 to <code>pause</code> and <code>unpause</code> a Deferred's processing 303 chain while you are adding lots of callbacks.</p> 301 304 302 305 <p>Be careful when you use these methods! If you <code>pause</code> a 303 Deferred, it is <em>your</em> responsibility to make sure that you unpause it. 304 The function adding the callbacks must unpause a paused Deferred, it should 305 <em>never</em> be the responsibility of the code that actually fires the 306 callback chain by calling <code>callback</code> or <code>errback</code> as 307 this would negate its usefulness!</p> 306 Deferred, it is <em>your</em> responsibility to make sure that you 307 unpause it. The function adding the callbacks must unpause a paused 308 Deferred, it should <em>never</em> be the responsibility of the code 309 that actually fires the callback chain by 310 calling <code>callback</code> or <code>errback</code> as this would 311 negate its usefulness!</p> 308 312 309 313 </body> 310 314 </html> -
doc/core/howto/process.xhtml
diff --git a/doc/core/howto/process.xhtml b/doc/core/howto/process.xhtml index af4b2ec..8a67a32 100644
a b 85 85 86 86 </ul> 87 87 88 <p><code>args</code> and <code>env</code> have empty default values, but 89 many programs depend upon them to be set correctly. At the very least, 90 <code>args[0]</code> should probably be the same as <code>executable</code>. 91 If you just provide <code>os.environ</code> for <code>env</code>, the child 92 program will inherit the environment from the current process, which is 93 usually the civilized thing to do (unless you want to explicitly clean the 94 environment as a security precaution). The default is to give an empty 95 <code>env</code> to the child.</p> 88 <p><code>args</code> and <code>env</code> have empty default values, 89 but many programs depend upon them to be set correctly. At the very 90 least, <code>args[0]</code> should probably be the same 91 as <code>executable</code>. If you just 92 provide <code>os.environ</code> for <code>env</code>, the child 93 program will inherit the environment from the current process, which 94 is usually the civilized thing to do (unless you want to explicitly 95 clean the environment as a security precaution). The default is to 96 give an empty <code>env</code> to the child.</p> 96 97 97 98 <p><code>reactor.spawnProcess</code> returns an instance that 98 99 implements <code base="twisted.internet.interfaces" class="API"> … … 167 168 168 169 <h2>Things that can happen to your ProcessProtocol</h2> 169 170 170 <p>These are the methods that you can usefully override in your subclass of171 <code>ProcessProtocol</code>:</p>171 <p>These are the methods that you can usefully override in your 172 subclass of <code>ProcessProtocol</code>:</p> 172 173 173 174 <ul> 174 175 … … 380 381 381 382 <h3>ProcessProtocols with extra file descriptors</h3> 382 383 383 <p>When you provide a <q>childFDs</q> dictionary with more than the normal 384 three fds, you need addtional methods to access those pipes. These methods 385 are more generalized than the <code>.outReceived</code> ones described above. 386 In fact, those methods (<code>outReceived</code> and 387 <code>errReceived</code>) are actually just wrappers left in for 388 compatibility with older code, written before this generalized fd mapping was 389 implemented. The new list of things that can happen to your ProcessProtocol 390 is as follows:</p> 384 <p>When you provide a <q>childFDs</q> dictionary with more than the 385 normal three fds, you need addtional methods to access those 386 pipes. These methods are more generalized than 387 the <code>.outReceived</code> ones described above. In fact, those 388 methods (<code>outReceived</code> and <code>errReceived</code>) are 389 actually just wrappers left in for compatibility with older code, 390 written before this generalized fd mapping was implemented. The new 391 list of things that can happen to your ProcessProtocol is as 392 follows:</p> 391 393 392 394 <ul> 393 395
