[Twisted-Python] Tie two event loops

Kevin Horn kevin.horn at gmail.com
Wed Jul 2 14:37:09 EDT 2008


On Wed, Jul 2, 2008 at 9:46 AM, Paul Wilson <paulalexwilson at gmail.com>
wrote:

> Hi all,
>
> I'm new to twisted but am having trouble getting my head around how to
> structure my application with this new approach.
>
> I'm creating a server that accepts client connections and then drives
> the client according to a simple, line-oriented protocol. The server
> sends the request (1 line), and receives a single lined result. I'd
> like the server to offer an API to generate the correct request lines
> to the client, and return the results (either blocking or
> asynchronously). So in effect, I have two event loops, one from the
> Network and one from whatever's sending the API requests (command
> line?).
>
> Can anyone offer any guidance on how to tie these two parts together?
> My instinct is to have the two event loops in separate threads and
> communicate through Queue.Queue, but that doesn't seem to follow the
> spirit of twisted. And I guess I'd have to somehow tell the reactor to
> poll the Queue object for requests, and then dispatch it to the
> relevant method to send out to the client. I've looked at
> threadedselectreactor but can't seem to find any decent documentation
> to even decide if it's write for what I want.
>
> Any advice would be much appreciated.
>
> Thanks,
> Paul
>

If I understand your problem correctly (and I'm not sure that I do, so keep
that in mind), then what you probably want is something like a Twisted app
using two Protocol objects (perhaps in two different "services"), one to
talk to your clients, and one to talk to your API-generator-thingy (command
line).  Then you just need a way for these to communicate...someplace to
store the state.  A Queue.Queue is probably a good choice, but I'm not
entirely sure where you would put it.  Try looking around for examples that
have multiple services and see how they share state.  I think the Twisted
book might have something useful in this regard, though I don't have it with
me ATM.

Perhaps wiser heads than mine will have better ideas/comments.

Kevin Horn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20080702/8d67c929/attachment.htm 


More information about the Twisted-Python mailing list