[Twisted-Python] Wrapping blocking code

Justin Johnson justinjohnson at fastmail.fm
Fri Jul 25 09:42:29 MDT 2003


Thanks for the reply.  Questions below.

On Fri, 25 Jul 2003 10:39:48 -0400, "Bob Ippolito" <bob at redivi.com> said:
> That's kind of amusing.  I'm not particularly familiar with the log or  
> win32eventreactor modules... but as far as I can tell, those messages  
> mean "dude, don't use log.logOwner anymore".  It's not your fault, it's  
> the twisted team's fault for not keeping win32eventreactor up to date  
> with the latest conventions.  They're just DeprecationWarnings, so  
> they're harmless to everything other than your eyes and our pride :)
> 
> Also, instead of using that reactor.iterate() hack.. it's better to be  
> in the habit of writing something event driven.. for example, you could  
> override the __init__ for protocol.ProcessProtocol (well, it's not  
> really overriding anything, since it doesn't have one) from your  
> subclass, stick a Deferred in the instance, and do a callback in  
> processEnded... like this:
> 
> class MyProcessProtocol(protocol.ProcessProtocol):
> 	def __init__(self):
> 		self.deferred = defer.Deferred()
> 
> 	...cut and paste your code...
> 
> 	def processEnded(self, reason):
> 		...cut and paste some more code...
> 		self.deferred.callback(reason)

reason is an Exception from twisted.internet.error.  It is an instance of
ProcessDone if it completes successfully.  How can this be called?  When
I was using the other process.py module, it allowed me to get stdout,
stderr, and the status.  That is really all I want to do.  So it seems
like stdout and stderr are easy to get with the xxxReceived methods, and
then the status would have to be determined from the type of exception
processEnded gets as the "reason" argument, right?

I used to have code like...

cmd = "cmd arg1 arg2"
p = process.ProcessOpen(cmd)
status = p.wait()
stdout = p.stdout.read()
stderr = p.stderr.read()
if status != 0:
    raise errors.CmdError(cmd, status, stdout, stderr) # an Exception I
    defined

...and this was in a module that didn't know about twisted, and I'd
prefer to keep it that way if possible.  What is the best way to wrap a
class with methods that contain code like this up with twisted?  Attached
is the latest version of the ProcessProtocol code which is working
(except for not knowing exactly how to get the real status).  I'm just
not sure what to do with it.

> 
> ...cut and paste again, but throw out your for loop...
> p.addBoth(lambda ignore: reactor.callLater(0.0, reactor.stop))
> reactor.run()

p is not a Deferred, so I can't call addBoth.  It seems to work okay
without this though.  What was this supposed to be added to?

-Justin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: my.py
Type: application/unknown
Size: 1199 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20030725/4d6848c9/attachment-0002.bin>


More information about the Twisted-Python mailing list