[Twisted-Python] Wrapping blocking code

Bob Ippolito bob at redivi.com
Fri Jul 25 08:39:48 MDT 2003


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)

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

The reactor.callLater(0.0, ....) is just to prevent the reactor from  
shutting down before it starts, because a deferred could theoretically  
fire immediately.  I'm pretty sure it couldn't in this case, but I  
usually put it there anyway when I'm stopping a reactor because in some  
situations it can (usually a bug in my code that causes an errback  
immediately, or something).

-bob

On Friday, Jul 25, 2003, at 10:17 America/New_York, Justin Johnson  
wrote:

> Since I'm running on win2k (ugh!) I followed the instructions to use
> win32eventreactor instead of the default posix reactor.  I did read
> through the docstrings, and it does mention that process calling can be
> problematic.  I ran the code below and got the following error.  Is it
> possible to get this working on windows?  It is marked as semi-stable.
> Man I hate windows!  Unfortunately the servers I support are all  
> windows
> boxes.
>
>
> C:\Python22\Lib\site- 
> packages\twisted\internet\win32eventreactor.py:198:
> Depreca
> tionWarning: Foolish capitalist!  Your opulent toilet will be your
> undoing!!
>   log.logOwner.own(fd)
> C:\Python22\Lib\site- 
> packages\twisted\internet\win32eventreactor.py:213:
> Depreca
> tionWarning: The proletariat is victorious.
>   log.logOwner.disown(fd)
>
>
> [snip]
> from twisted.internet import protocol
> from twisted.internet import win32eventreactor
> win32eventreactor.install()
> from twisted.internet import reactor
>
> class MyProcessProtocol(protocol.ProcessProtocol):
> 	finished = 0
>
> 	def connectionMade(self):
> 		self.data = ''
> 		self.err = ''
> 		self.transport.write("connectionMade")
>
> 	def outReceived(self, data):
> 		self.data = self.data + data
>
> 	def outConnectionLost(self):
> 		self.transport.write("outConnectionLost")
>
> 	def errReceived(self, data):
> 		self.err = self.err + data
> 		self.transport.write("errReceived")
>
> 	def errConnectionLost(self):
> 		self.transport.write("errConnectionLost")
>
> 	def inConnectionLost(self):
> 		self.transport.write("inConnectionLost")
>
> 	def processEnded(self, reason):
> 		self.finished = 1
> 		self.reason = reason
> 		self.transport.write("processEnded")
>
>
> exe = r"c:\program files\rational\clearcase\bin\cleartool.exe"
> p = MyProcessProtocol()
> reactor.spawnProcess(p, exe, [exe, "lsview", "-s"], env=None)
> while not p.finished:
> 	reactor.iterate()
> [/snip]
>
>
> On Wed, 23 Jul 2003 14:42:15 -0600, "Dave Smith" <dizzyd at jabber.org>
> said:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Sounds to me like you want to use a deferToThread(...), to pass the
>> calls off to the reactor's thread pool so they can run there without
>> holding up the reactor's runloop.
>>
>> See also:
>> http://www.itamarst.org/writings/OSCON03/twisted_internet-95.html
>>
>> Hope that helps. :)
>>
>> Diz
>>
>> On Wednesday, Jul 23, 2003, at 10:16 America/Denver, Justin Johnson
>> wrote:
>>
>>> I have some code in my xmlrpc service that executes commands using
>>> process.py (http://starship.python.net/crew/tmick/).  The service
>>> appears
>>> to block this code, thus only allowing one connection to get any work
>>> done at a time, at least while the commands are being executed.
>>> Should I
>>> be wrapping this up in a Deferred somehow to avoid blocking?
>>>
>>> Slowly but surely this is starting to make sense to me.  Twisted  
>>> rocks!
>>>
>>> Thanks.
>>> -Justin
>>>
>>> _______________________________________________
>>> Twisted-Python mailing list
>>> Twisted-Python at twistedmatrix.com
>>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.2.1 (Darwin)
>>
>> iD8DBQE/HvMnYNE3chVHHsMRAplLAJ0bbnqUGSDkDz3AZPvCNTvOn1T8TgCg8nCd
>> m830zVM1AnnL3fF76V1WHqY=
>> =39jf
>> -----END PGP SIGNATURE-----
>>
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list