[Twisted-Python] where to begin

Andrew Bennetts andrew-twisted at puzzling.org
Tue Feb 11 00:35:38 EST 2003

On Tue, Feb 11, 2003 at 04:59:49PM +1300, Rob Brown-Bayliss wrote:
> > Yes, this is a well-known problem.  The docs are improving, but aren't
> > perfect yet.  Hopefully the glossary at
> >     http://twistedmatrix.com/documents/howto/glossary
> > can help you get a one-sentence description of all the wacky terms, so you
> > can figure out what's relevant to you.
> And I still dont get it, am I supposed to use twisted as a base class
> for my app, derive a class for my comunication or what?

Ah, right :)

Have a look at doc/examples/echoserv.py.  It's about as simple as a Twisted
server can get.

To answer a question, Twisted is a framework.  It's not so much a library
that you inherit a class or two from, but framework to build your
application upon.  Specifically, Twisted drives your application: *Twisted*
controls the event loop, *Twisted* calls your application code.  Not the
other way around.

> Every thing seems to link on to something else, deffereds, it's own web
> servewr etc, it seems like it's a joke, or if not then certianly
> overkill for sending data over sockets.

That's a natural by-product of Twisted being a framework, unfortunately;
it's hard to take-or-leave each individual piece, you basically have to take
the whole framework or none of it.

> Yet at the same time it seems to say "This is what you need Rob, Twisted
> it is"

Well, we hope so :)  It really is a kick-arse framework :)

> So do where do I start a GUI app, with pygtk and import some object
> based on a factory/reactor setup?  And what is the point of tap's?  why
> not just run the python code as I would any other code?  why would I
> want to change reactors at startup?

Every single one of those questions should be added to the FAQ!  Well asked

Here's a brief walk-through of using the pbgtk example, which demonstrates
using both PB (Perspective Broker, our remote object protocol), and the GTK
   $ cd ~/cvs/Twisted/doc/examples
   $ python pbecho.py
   $ twistd -n -f pbecho-start.tap

That's started the PB Echo server.  Now, to run the PB gtk client, from a
different command prompt do this:
   $ python doc/examples/pbgtk.py

The code in those example files is sadly almost uncommented, though :(

The point of taps, and thus the mktap & twistd utilities, is a convenient
way to manage servers; I personally find them a distraction when I'm
developing a new system, and ignore them until want to deploy something...
(and sometimes I just ignore then completely).  Many developers love them.
I suspect I eventually will, too ;)

Basically the only reason to change reactors is if you need GUI support.
Otherwise, the default reactor is almost certainly the best choice.

> It just seems to defy logic, where as useing pythonx xml-rpc or the
> spread module is simple and obvious, 

The reason for this is simple.  Twisted is asynchronous.

This means you can't just pop open an interactive python interpreter,
instantiate something, and away you go.  It means you can't just write some
code, subclass a bit of Twisted, and have magic happen.  It means that
Twisted is the framework, and you have to work within its facilities.

The problem is that you can't do anything that blocks, and that Twisted
controls the event loop.  You can work with that interactively, though, if
you know how:
    >>> def foo(x):
    ...     print 'Hello!', x
    >>> from twisted.internet import reactor
    >>> reactor.callLater(0, foo, 'Spam...')
    <twisted.internet.base.DelayedCall instance at 0x824c9f4>
    >>> reactor.iterate()
    Hello! Spam...

But basically, you can't look at Twisted as something you can just bolt-on
to an existing code-base.  It's more fundamental than that, and it will
almost certainly mean restructuring code.  Again, this is why it's a
"framework" and not just a "library".

The good news is that the framework Twisted provides is the sort of thing
any socket server program needs to do anyway, but almost certainly better
written than just a home-brew version.  And being asynchronous is actually
an advantage once you get used to it (in my opinion).

Finally, the best source of documentation in many ways is still the irc
channel, #twisted on irc.freenode.net.  We're usually very responsive to
newbie questions.


More information about the Twisted-Python mailing list