[Twisted-Python] Writing Servers

Itamar Shtull-Trauring itamar at itamarst.org
Wed Mar 15 13:07:54 EST 2006

On Wed, 2006-03-15 at 08:36 -0800, A S wrote:

> That when connection is accepted the new *instance* of
> a protocol is created. However as it is single
> threaded, can use the same instance?

In theory, yes, in practice the way the APIs work make this harder.

> For example (In case of UDP):

UDP doesn't have a factory, or for that matter connections; it's one
protocol instance per port you listen on.

> I want to have same protocol *instance running* for
> diff interface on same port. As this protocol does not
> store any state.

Just have it forward to an object that does the actual work. You can't
have same instance because of the self.transport attribute - how would
it work with two different transports?

> Also instead of creating one when connection comes and
> descard is when connection is broken. Use a single
> one.

That is already the case for UDP.

> I am once the datagramReceived is call, it must return
> before any other request is served. (i am not using
> any deferrer)
> Q1) Am i missing some concept?


> Q2) What if the request is delegated to deferrer or
> threads, can we still use the same instance of
> protocol class?


> Q3) What are the ways to create deferrer itself
> There is little about it, reactor callfromthread or
> callinthread? Any good doc?

Deferreds don't magically make things not block the event loop, and they
almost never need threads. For example,
twisted.web.client.getPage("http://google.com") returns a Deferred but
doesn't use threads. A deferred is just a promise that some code,
responding to some future event (callLater, datagramReceived, a thread,
etc.) will give you a result.

More information about the Twisted-Python mailing list