[Twisted-Python] Need advice on changing blocking calls to be Twisted-aware...

Andrew Bennetts andrew-twisted at puzzling.org
Tue May 20 00:26:49 MDT 2003


On Tue, May 20, 2003 at 04:07:43PM +1000, Stuart Hungerford wrote:
> 
> What I'm effectively doing in the render() call is:
> 
>         def render(...)
>             ...generate some HTML
>             do blocking system call
>             ...generate some more HTML
> 
> Which I understand is a no-no in the reactor and callback
> based Twisted framework. I believe that the Twisted way to do

Correct.

> this is with a reactor.spawnProcess() call which in turn
> returns a Deferred object.  Or maybe I should be using
> threads instead?

Threads are unnecessary here.

> Can someone point me to some example code where a
> blocking-system-call-and-it's-results-needed-here pattern
> is expressed in the spawnProcess/Deferred/callback world
> of Twisted?

Use twisted.internet.utils.getProcessOutput, which returns a Deferred.
Your code should something like:

    def render(...):
        request.write('some html')
        d = getProcessOutput('random program')
        d.addCallback(self.renderProcessOutput, request)
        d.addErrback(self.renderError, request)
        d.addCallback(self.finishPage, request)
        return NOT_DONE_YET

    def renderProcessOutput(self, output, request):
        request.write(output)

    def renderError(self, failure, request):
        request.write('it broke!')
    
    def finishPage(self, _, request):
        request.write('</html>')
        request.finish()

This is obviously very rough, but hopefully it gives you the right idea.

-Andrew.





More information about the Twisted-Python mailing list