[Twisted-Python] Re: Synchronization techniques

Stefan Rank list-ener at strank.info
Fri Apr 6 14:18:00 EDT 2007

on 04.04.2007 23:40 glyph at divmod.com said the following:
> Run "python -m twisted/conch/stdio" for an example of an interactive 
> session that is held in a non-blocking way.  Not blocking in the code 
> *doesn't* mean not blocking for the user - it just means having the 
> option not to block.
>  >2.  Users expect certain things in an interactive python session that
>  >don't mesh well with Twisted and the asynchronous universe:
> This is _exactly_ the documentation issue I was talking about :).
>  >>>>psi = computeWavefunctionForHydrogen()
>  >>>>psi.getEnergy(1)
>  >-13.6
>  ># here the user looks at the energy (a human if statement) and decides
>  >if they actually want to
>  ># make the following plot.  If the answer were not -13.6, they would
>  >not make the plot.
>  >>>>plot(psi.getState(1))
>  >
>  >Even if you could get the reactor running in an interactive python
>  >session it would be crazy to have to write something like (in an
>  >interactive session):
>  >>>>d = computeWavefunctionForHydrogen()
>  >>>>def printAndPlot(psi, n):
>  >>>>     print psi.getEnergy(n)
>  >>>>     if abs(psi.getEnergy(n) - (psi.getEnergy(n)) < 1.0e-4:
>  >>>>         plot(psi.getState(n))
>  >>>>d.addCallback(printAndPlot, 1)
> Here's a screenshot of an interactive session using the command above:
> http://twistedmatrix.com/users/glyph/images/content/screenshots/psi-energy.png
> Unfortunately it doesn't animate, but there is a 1-second pause between 
> "<Deferred #0>" and "Deferred #0 called back:".  It's a bit crude since 
> it is "deferred-oriented" right now, rather than result oriented, but a 
> few clever implementation tricks could easily eliminate the distinction 
> (like the "_.result" line).

Am I right that this is a screenshot of the manhole client?

I just recently tried to embed the IPython shell into a twisted application.
AFAIK this is currently only possible using a separate thread
(there is an ASPN cookbook entry).

I would like to list here what I think is necessary to make this 
possible without using threads. Hopefully, you can correct me where I am 

The main IPython class has a main loop (InteractiveShell.interact) that 
uses raw_input. Further, it first sets up pyreadline for handling 
command-line niceties inside raw_input.

For embedding this in twisted, the twisted reactor needs to be the one 
calling IPython/pyreadline when necessary.
On the IPython level, I think this should be "easy" by subclassing the 
Shell and splitting/rearranging the relevant code, so that it can be 
called back at the point where raw_input would normally return.
(I did not look at the new IPython saw branch...)

On the twisted side, I think that something like twisted.internet.stdio 
  would be calling back?
Or rather something like twisted.conch.insults?

I suspect that it would be hard to recode pyreadline in a way that it 
could be called back by twisted?
So to get readline support, something like readline would need to be 
implemented using t.i.stdio / insults.

Does this seem possible, so far? :-)


More information about the Twisted-Python mailing list