Search code examples
pythontwisted

Python twisted Reactor class


What is the significance of the decorators

 @reactor.callWhenRunning,
 @results_deferred.addCallback
 @results_deferred.addErrback.

Also what are deferred strings, for example in the

 twisted.internet.utils.getProcessOutput()

returns a deferred string what exactly is happening here?

I am new to twisted hence this might be a very simple question but reading twisted documentation did not help me much


Solution

  • A deferred is a like a promise to return output in the future. You really should read the documentation on Deferreds here and here. Also, you should read up on Python decorators in general. One introduction is here.

    More specifically, what is happening is that when you call getProcessOutput(), the result is not quite ready. It might be ready in an instant or in an hour. But you probably don't care: whenever it is ready, you probably want to take the output and pass it to a function. So instead of returning the output (which is not going to be ready right away), getProcessOutput returns a deferred object. When the output is finally ready, the deferred object will notice and call whatever processing function you supply, passing along the actual process output data. You really should read up on deferreds though.