[Twisted-Python] Interactive Twisted client (newbie question?)

Jon Blower jdb at mail.nerc-essc.ac.uk
Wed Jan 5 08:44:33 MST 2005


Hi Glyph,

Thanks very much for your reply, very useful.  I've had another think about my
problem and I guess it boils down to this question: when I'm writing a client
program, how can I simulate a synchronous function call (avoiding callbacks)
like this pseudo-Python, which sends a message then returns its reply:

def sendMessage(message):
   transport.send(message)
   # Wait for reply to arrive
   reply = getReplySomehow()
   return reply

In the protocol I am implementing, I expect most messages to be replied to
immediately by the server so this function would not block for long (I'm only
writing a simple, naive client at the moment).  The event-driven model seems to
me to be great for implementing servers, but a little more awkward for
implementing simple client programs.  Is this comment fair?

I can see how I would implement the above pseudo-function using threads (indeed
I have done so in Java) - perhaps this is my only option to avoid blocking?

Thanks,
Jon

Quoting Glyph Lefkowitz <glyph at divmod.com>:

> On Mon, 2005-01-03 at 23:00 +0000, Jon Blower wrote:
> 
> > I'm sure I'm not the first to ask this sort of question, but I've looked
> through
> > the docs and example code and haven't been able to track down an answer. 
> I'd
> > be very grateful for any pointers that anyone could offer.
> 
> This is why I often suggest the use of 'twistd' for running your Twisted
> code - the reactor.run() call is confusing, because it makes it seem as
> though it's your code's responsibility to start and stop the reactor,
> and possibly to do things before or after that has happened.
> 
> Even if you need to start the reactor yourself - for example, if you are
> writing a GUI program - it is best to abstract out the reactor
> start/stop far from your application code.
> 
> If I understand the core of your question correctly, it's "How do I use
> Twisted at the interactive Python interpreter?"
> 
> There is no easy answer.  Twisted is not well-suited towards use at a
> normal Python prompt, because the normal Python prompt is not
> event-driven.  There have been a few different forays into interactive
> Python/Twisted integration.  The 1.3.0 release includes a 'manhole'
> client/server pair which will allow you to log in and run python code
> inside a running Twisted process.  JP Calderone has recently done quite
> a bit of work on a new, better interactive interpreter (including SSH
> access and command-line editing) which looks more like a regular Python
> prompt.
> 
> Although there are several different avenues you can take to get there,
> what you probably want to arrive at is a Twisted server that is
> listening on one port for Python commands, and on another for whatever
> protocol it is you are trying to implement.  For convenience, it may be
> useful for the "port" that it's listening on to actually be standard
> I/O.  This is possible using twisted.internet.stdio.
> 
> 
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> 


-- 
--------------------------------------------------------------
Dr Jon Blower              Tel: +44 118 378 5213 (direct line)
Technical Director         Tel: +44 118 378 8741 (ESSC)
Reading e-Science Centre   Fax: +44 118 378 6413
ESSC                       Email: jdb at mail.nerc-essc.ac.uk
University of Reading
3 Earley Gate
Reading RG6 6AL, UK
--------------------------------------------------------------

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.




More information about the Twisted-Python mailing list