[Twisted-Python] Twisted's cfreactor + PyObjC = Asynchronous Networking on OS X

Bob Ippolito bob at redivi.com
Mon Oct 13 16:25:39 MDT 2003


For those of you who don't watch Twisted CVS, cfreactor is officially 
out of the sandbox.  cfreactor is the CoreFoundation integration 
reactor for Twisted ( http://www.twistedmatrix.com/ ), which means that 
you can now seamlessly integrate Cocoa and Twisted (without digging 
around in my sandbox, that is).

If you're running OS X and you'd like to play with cfreactor, check out 
the latest CVS and do a python setup.py install (so that it can build 
twisted/internet/cfsupport.so -- ignore the warnings, they are expected 
for Pyrex modules).  There are two examples provided in 
doc/examples/Cocoa.  You will need PyObjC 1.0 (though it may work with 
earlier betas, you should upgrade if you haven't yet) and MacPython 2.3 
(not tested with Panther yet, but should work).  The next release 
version of Twisted, which is expected very shortly, will include this 
new reactor and updated documentation.  When it is released, it will 
also be available from my Package Manager repository ( 
http://undefined.org/python/pimp/ ).
	
	SimpleWebClient - Pretty much a straight port of 
doc/examples/qtdemo.py, a very simple app that downloads HTML and puts 
it in a text box.
	WebServicesTool - A Twisted port of b.bum's XML-RPC demo (which is 
included with PyObjC).  For fun, compare how much simpler the Twisted 
version is (since it doesn't need to deal with threads), and how much 
more responsive it feels.

To run either demo, use the buildapp.py script provided in the same 
folder as the application:
	python buildapp.py build

And then:
	open build/ApplicationName.app

Note that when using cfreactor you should *always* call reactor.run() 
while the NSRunLoop is in action (best place to put it is probably in 
your NSApplication delegate's applicationDidFinishLaunching_ method).  
If you're using threads, make sure to only call into Twisted from the 
main runLoop (which is where you have to do your drawing anyways).  And 
of course, you should cfreactor.install() before importing any other 
Twisted modules (see the "Choosing a Reactor and GUI Toolkit 
Integration" HOWTO for more information).

-bob





More information about the Twisted-Python mailing list