<div class="gmail_quote">On Mon, Jan 31, 2011 at 10:58 PM, Jason Heeris <span dir="ltr">&lt;<a href="mailto:jason.heeris@gmail.com">jason.heeris@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hi,<br>
<br>
I&#39;d like to come to grips with Twisted, and so far I&#39;ve been following<br>
and tweaking example after example. But I&#39;m finding it incredibly hard<br>
to get a high level view of the Twisted &quot;landscape&quot; — and because I<br>
don&#39;t know what I don&#39;t know, it&#39;s hard to know what questions to even<br>
ask. I want to get to the point where I can just sit down and know<br>
where to start, and hopefully someone here can give me a bit of<br>
direction.<br>
<br>
I understand many of the microscopic components. Deferred results,<br>
file descriptors and sockets, the reactor loop concept — I get those.<br>
I&#39;ve read the tutorial[1], another Twisted Introduction[2], and about<br>
50% of the API documentation (possibly not the right 50%, though).<br>
<br>
But I just can&#39;t seem to wrap my head around the interfaces, factories<br>
and wiring it all together. Maybe some examples of the speedbumps I<br>
keep encountering will help:<br>
<br>
1. The &quot;finger&quot; intro &quot;Drop Connections&quot; example[3] just straight out<br>
uses the self.transport member of a protocol. But how do I, a Twisted<br>
newbie, know that this even exists? After hours of digging around, I<br>
find it in the docs for a BaseProtocol method[4]. Later on, this same<br>
example uses the &quot;self.factory&quot; member. I still haven&#39;t found that<br>
one. Where do I look up these assumed-to-exist members? How do I know<br>
what other members exist?<br></blockquote><div> <br>Protocol objects typically (always) have a self.factory method, but you won&#39;t<br>find it in the docs for a protocol.  The factory has a method called buildProtocol<br>
(<a href="http://twistedmatrix.com/documents/10.2.0/api/twisted.internet.protocol.Factory.html#buildProtocol">http://twistedmatrix.com/documents/10.2.0/api/twisted.internet.protocol.Factory.html#buildProtocol</a>)<br>which creates Protocol instances and sets the p.factory member to itself.<br>
<br>So it does something like (in the Factory, so self is the Factory instance):<br>p = self.protocol<br>p.factory = self<br>return p<br><br>It&#39;s a pretty screwy API and is not particularly well documented anywhere.<br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
2. What is a Factory, anyway? What are they *for*?<br></blockquote><div><br>As Andrew said (or quoted) a Factory is an object which creates Protocol <br>instances when a connection is made. Sometimes that&#39;s all there is to it, <br>
but they are often used to store state across connections.  For example,<br>if you want to know how many connections your IMAP (or whatever) server <br>currently has open, putting a counter in the Factory which was incremented <br>
each time a connection starts (it creates a protocol) and decremented each <br>time a connection closes would make sense.<br><br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

3. The finger example goes over writing a server — what about an<br>
asynchronous client that must follow a particular protocol? Where do I<br>
start with that? Is there an example?<br>
<br></blockquote><div><br>You already found this one. :)<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
4. What if I&#39;m not interested in networking? I primarily deal with<br>
serial lines, files and subprocesses... are there examples for those?<br>
<br></blockquote><div><br>Not many, I don&#39;t think.  I&#39;m pretty sure there&#39;s a serial port example, though...<br><br>(rummage, rummage)<br><br>here it is!  check out the mouse.py and gpsfix.py examples under<br>
/doc/core/examples (<a href="http://twistedmatrix.com/trac/browser/trunk/doc/core/examples">http://twistedmatrix.com/trac/browser/trunk/doc/core/examples</a>)<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Points #3 and #4 are my motivation for using Twisted — I am currently<br>
mired in a pygtk program that uses threads and all sorts of locks and<br>
synchronisation mechanisms to send and receive data over a serial<br>
line, updating UI with progress, errors and success. I&#39;d like to see<br>
if using Twisted simplifies things a bit, and I&#39;ll ask a more specific<br>
question about how to structure that when I can condense it to a<br>
reasonable level :)<br>
<br>
I hope I&#39;m not being too vague, but Twisted is rather large,<br>
conceptually, to absorb. I keep being pointed to the documentation for<br>
specific parts, but I have no idea how they fit together and that&#39;s<br>
what I&#39;m really after.<br>
<br>
Thanks,<br>
Jason<br>
<br>
[1] <a href="http://twistedmatrix.com/documents/current/core/howto/tutorial/index.html" target="_blank">http://twistedmatrix.com/documents/current/core/howto/tutorial/index.html</a><br>
[2] <a href="http://krondo.com/?page_id=1327" target="_blank">http://krondo.com/?page_id=1327</a><br>
[3] <a href="http://twistedmatrix.com/documents/current/core/howto/tutorial/intro.html#auto4" target="_blank">http://twistedmatrix.com/documents/current/core/howto/tutorial/intro.html#auto4</a><br>
[4] <a href="http://twistedmatrix.com/documents/current/api/twisted.internet.protocol.BaseProtocol.html#makeConnection" target="_blank">http://twistedmatrix.com/documents/current/api/twisted.internet.protocol.BaseProtocol.html#makeConnection</a><br>

<br></blockquote><div><br>A couple of us are getting fired up about cleaning up the documentation to make it easier for those new to Twisted to find what they&#39;re looking for, so please do speak up with any feedback you have.<br>
<br>Kevin Horn <br></div></div><br>