Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, you've got it right: you need threads or separate processes to avoid blocking the Twisted event loop. Using Deferreds wont magically make your code non-blocking. For your questions:</p> <ol> <li><p>Yes, you would block the event loop if <code>count_lines</code> is very slow. Deferring it to a thread would solve this.</p></li> <li><p>I used <a href="http://twistedmatrix.com/documents/current/core/howto/index.html" rel="nofollow">Twisteds documentation</a> to learn how Deferreds work, but I guess you've already been through that. The article on <a href="http://twistedmatrix.com/documents/current/core/howto/rdbms.html" rel="nofollow">database support</a> was information since it clearly says that this library is built using threads. This is how you bridge the synchronous–asynchronous gap.</p></li> <li><p>If the call is truly blocking, then you need to <code>DeferToThread</code>. Python itself is kind-of single threaded, meaning that only one thread can execute Python byte code at a time. However, if the thread you create will block on I/O anyway, then this model works fine: the thread will release the global interpreter lock and so let other Python threads run, including the main thread with the Twisted event loop.</p> <p>It can also be the case that you can use non-blocking I/O in your code. This can be done with the <a href="http://docs.python.org/library/select.html" rel="nofollow"><code>select</code></a> module, for example. In that case, you don't need a separate thread. Twisted uses this technique internally and you don't have to think of this if you do normal network I/O. But if you're doing something exotic, then it's good to know how things are built so that you can do the same.</p></li> </ol> <p>I hope that makes things a bit clearer!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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