[Twisted-Python] Wrapping blocking code
bob at redivi.com
Fri Jul 25 10:39:48 EDT 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:
self.deferred = defer.Deferred()
...cut and paste your code...
def processEnded(self, reason):
...cut and paste some more code...
...cut and paste again, but throw out your for loop...
p.addBoth(lambda ignore: reactor.callLater(0.0, reactor.stop))
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).
On Friday, Jul 25, 2003, at 10:17 America/New_York, Justin Johnson
> 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
> tionWarning: Foolish capitalist! Your opulent toilet will be your
> tionWarning: The proletariat is victorious.
> from twisted.internet import protocol
> from twisted.internet import win32eventreactor
> from twisted.internet import reactor
> class MyProcessProtocol(protocol.ProcessProtocol):
> finished = 0
> def connectionMade(self):
> self.data = ''
> self.err = ''
> def outReceived(self, data):
> self.data = self.data + data
> def outConnectionLost(self):
> def errReceived(self, data):
> self.err = self.err + data
> def errConnectionLost(self):
> def inConnectionLost(self):
> def processEnded(self, reason):
> self.finished = 1
> self.reason = reason
> 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:
> On Wed, 23 Jul 2003 14:42:15 -0600, "Dave Smith" <dizzyd at jabber.org>
>> -----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:
>> Hope that helps. :)
>> On Wednesday, Jul 23, 2003, at 10:16 America/Denver, Justin Johnson
>>> I have some code in my xmlrpc service that executes commands using
>>> process.py (http://starship.python.net/crew/tmick/). The service
>>> 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
>>> Twisted-Python mailing list
>>> Twisted-Python at twistedmatrix.com
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.2.1 (Darwin)
>> -----END PGP SIGNATURE-----
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
More information about the Twisted-Python