Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A blocking function is a function which doesn't return, but still leaves your process idle - unable to complete more work.</p> <p>You're asking us to make <em>your</em> blocking functions non-blocking. However – unless you're writing an operating system – <em>you</em> don't have any blocking functions. You might have functions which block because they make calls to blocking system calls, or you might have functions which "block" because they do a lot of computation.</p> <p>Making the former type of function non-blocking is impossible without making the underlying system call non-blocking. Depending on what that system call is, it may be difficult to make it non-blocking without also adding an event loop to your program; you don't just need to make the call and have it not block, you also have to make another call to determine that the result of that call will be delivered somewhere you could associate it.</p> <p>The answer to this question is a very long python program and a lot of explanations of different OS interfaces and how they work, but luckily I already wrote that answer on a different site; I called it <a href="http://twistedmatrix.com/" rel="noreferrer">Twisted</a>. If your particular task is already <a href="http://twistedmatrix.com/documents/10.2.0/api/twisted.internet.interfaces.html" rel="noreferrer">supported by a Twisted reactor</a>, then you're in luck. Otherwise, as long as your task maps to some existing operating system concept, you can extend a reactor to support it. Practically speaking there are only 2 of these mechanisms: file descriptors on every sensible operating system ever, and I/O Completion Ports on Windows.</p> <p>In the other case, if your functions are consuming a lot of CPU, and therefore not returning, they're not really blocking; your process is still chugging along and getting work done. There are three ways to deal with that:</p> <ul> <li>separate threads </li> <li>separate processes</li> <li>if you have an event loop, write a task that periodically yields, by writing the task in such a way that it does some work, then asks the event loop to resume it in the near future in order to allow other tasks to run.</li> </ul> <p>In Twisted this last technique can be accomplished in various ways, but here's a syntactically convenient trick that makes it easy:</p> <pre><code>from twisted.internet import reactor from twisted.internet.task import deferLater from twisted.internet.defer import inlineCallbacks, returnValue @inlineCallbacks def slowButSteady(): result = SomeResult() for something in somethingElse: result.workHardForAMoment(something) yield deferLater(reactor, 0, lambda : None) returnValue(result) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload