[Twisted-Python] Use asyncore dispatchers with Twisted
Yes, it is possible - you can use servers or clients written for asyncore together with twisted, using the following module. Should I check it in? If so, should it be in examples/ or twisted.internet, and what's a good module name? =================================================================== """Use asyncore dispatchers with Twisted. If you have an asyncore dispatcher, you can still use it with twisted. Just import this module, and don't run asyncore.loop() -- instead start the twisted event loop, either by calling twisted.internet.main.run(), or the usual way by calling an Application instance's run() method. """ import asyncore from twisted.internet import main class AsyncoreLooper: """Run the asyncore event loop for asyncore dispatchers.""" def timeout(self): if asyncore.socket_map: return 0.0 else: return None def runUntilCurrent(self): asyncore.poll(0.001) asyncoreLooper = AsyncoreLooper() main.addDelayed(asyncoreLooper) if __name__ == '__main__': # example, run debugging SMTP proxy (included with 2.1) # telnet to port 8025 to try it out. import smtpd d = smtpd.DebuggingServer(("localhost", 8025), ("gateway1.att.net", 25)) main.run()
There has GOT to be a better way to do this :) On Tue, 2002-01-08 at 08:00, Itamar Shtull-Trauring wrote:
Yes, it is possible - you can use servers or clients written for asyncore together with twisted, using the following module. Should I check it in? If so, should it be in examples/ or twisted.internet, and what's a good module name?
=================================================================== """Use asyncore dispatchers with Twisted.
If you have an asyncore dispatcher, you can still use it with twisted. Just import this module, and don't run asyncore.loop() -- instead start the twisted event loop, either by calling twisted.internet.main.run(), or the usual way by calling an Application instance's run() method. """
import asyncore
from twisted.internet import main
class AsyncoreLooper: """Run the asyncore event loop for asyncore dispatchers."""
def timeout(self): if asyncore.socket_map: return 0.0 else: return None
def runUntilCurrent(self): asyncore.poll(0.001)
asyncoreLooper = AsyncoreLooper() main.addDelayed(asyncoreLooper)
if __name__ == '__main__': # example, run debugging SMTP proxy (included with 2.1) # telnet to port 8025 to try it out. import smtpd d = smtpd.DebuggingServer(("localhost", 8025), ("gateway1.att.net", 25)) main.run()
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python -- ______ you are in a maze of twisted little applications, all | |_\ remarkably consistent. | | -- glyph lefkowitz, glyph @ twisted matrix . com |_____| http://www.twistedmatrix.com/
Glyph Lefkowitz wrote:
There has GOT to be a better way to do this :)
Well, two other possibilities: 1) Wrap each dispatcher with a Selectable and plug those in to the twisted event loop. I think we'd still need a Delayed to deal with the difference between twisted's addReader() and asyncore's readable() (in twisted you set yourself, in asyncore the event loop does that for you.) 2) Make asyncore twisted's core event loop - very icky, and possible conflicts with other event loops, such as GUI toolkits. I actually think this way isn't bad - consider that asyncore.loop() does basically the same thing as this code. The drawbacks are of course two select() calls per loop, and the hardcoded select() timeout - but with 0.0 it started sucking up all my CPU. Anyway, the idea is to embrace and assimilate - once we've got them hooked with Twisted's *other* cool stuff they'll end up rewriting as a Protocol anyway ;)
participants (2)
-
Glyph Lefkowitz
-
Itamar Shtull-Trauring