Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Twisted Deferred : clarification needed
    primarykey
    data
    text
    <p>I am hoping for some clarification on the best way to deal with handling "first" <strong>deferreds</strong> , ie not just adding callbacks and errbacks to existing Twisted methods that return a deferred, but the best way of creating those <em>original</em> deferreds.</p> <p>As a concrete example, here are 2 variations of the same method : it just counts the number of lines in some rather big text files, and is used as the <strong>starting point</strong> for a chain of deferreds.</p> <p><strong>Method 1:</strong> This one does not feel so good, as the deferred is fired directly by the reactor.callLater method.</p> <pre><code>def get_line_count(self): deferred = defer.Deferred() def count_lines(result): try: print_file = file(self.print_file_path, "r") self.line_count = sum(1 for line in print_file) print_file.close() return self.line_count except Exception as inst: raise InvalidFile() deferred.addCallback(count_lines) reactor.callLater(1, deferred.callback, None) return deferred </code></pre> <p><strong>Method 2:</strong> slightly better , as the deferred is actually fired <strong>when</strong> the result is available</p> <pre><code>def get_line_count(self): deferred = defer.Deferred() def count_lines(): try: print_file = file(self.print_file_path, "r") self.line_count = sum(1 for line in print_file) print_file.close() deferred.callback(self.line_count) except Exception as inst: deferred.errback(InvalidFile()) reactor.callLater(1, count_lines) return deferred </code></pre> <p><strong>Note:</strong> You could also point out that both of these are actually synchronous, and potentially blocking methods, (and I perhaps could use "<em>MaybeDeferred</em>"?). But well, that that is actually one of the aspects I get confused by.</p> <ol> <li><p>For <strong>Method 2</strong>, if the <em>count_lines</em> method is very slow (counting the lines in some huge files etc), will it potentially "block" the whole Twisted app ? I read quite a lot of documentation on how callbacks and errbacks and the reactor behave together (callbacks need to be executed quickly, or return deferreds themselves etc), but in this case , I just don't see and would really appreciate some pointers/examples etc</p></li> <li><p>Are there some articles/<strong>clear</strong> explanations that deal with the <strong>best approach</strong> to creating these "first" deferreds? I have read through <a href="http://krondo.com/?page_id=1327" rel="nofollow noreferrer">these excellent articles</a> , and they have helped a lot with some of the basic understanding, but I still feel like I am missing a piece.</p></li> <li><p>For <strong>blocking code</strong>, would this be this a typicall case for <em>DeferToThread</em> or <em>reactor.spawnprocess</em> ? I read through a lot of questions like <a href="https://stackoverflow.com/questions/6117587/twisted-making-code-non-blocking">this one</a> and <a href="http://jessenoller.com/2009/02/11/twisted-hello-asynchronous-programming/" rel="nofollow noreferrer">this article</a>, but I still am not 100% sure on how to deal with potentially blocking code, mostly when dealing with file i/o</p></li> </ol> <p>Sorry if any of this seems too basic , but I really want to get the hang of using Twisted more thoroughly. (It has been a really powerful tool for all the more network-oriented aspects). Thank you for your time!</p>
    singulars
    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.
 

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