python: twisted app with interacive python shell - python

Would it be possible to write a twisted-powered application that opens an interactive shell, e.g. to tweak the business objects served via the twisted protocol layer?
My current problem is that the reactor.run() blocks the application, and the IPython.embed() is only run after the reactor has finished.

Unless you specifically need extras that are only in ipython, you really should check out manhole (twistedmatrix example code)
It lets you access a python interactive shell that is natively running in the twisted reactor. Normally the shell is accessed via telnet or ssh, but if you wanted to be creative you could wire it up any way you want.

... Meanwhile I found an answer to my question: http://twistedmatrix.com/documents/current/core/howto/threading.html
def interact():
import IPython
IPython.embed()
reactor.callInThread(interact)
reactor.run()

Related

Sending commands to and receiving their output from a running Python application (via remote connection)

I have a long-running server application written in Python 3, which I would like to interactively debug from time to time. For that I would like to run Python commands "within" the server application process, inspect the values of global variables etc., like in a REPL or the standardd Python console.
It seems the Python standard library's code module and its InteractiveConsole class seems to be what I am looking for. I was thinking of running that in a separate thread so that the main application is not blocked while I communicate with it.
However, it seems that class provides interaction via standard input and output. That might not be exactly what I need. Is there a way to make that interactive console listen / connect to a socket and send input and output through this socket, so that I can connect to the console via a TCP connection?
Or is there another, better way to implement my requirement without this code module?

How to convert a wxPython app to run as a headless unix job?

I am currently running 2.8.9.1 of wx in a linux box.
The App I am working on was originally written to run on MS Windows. We are planning to port part of the core logic to linux and run it as a process.
The problem is that the linux box is headless. We will not have a X-windows environment. But the existing codebase was written in such a way that it is tightly coupled with the wx layer.
For example, I have a couple of classes which are subclass of wx.EvtHandler
I probably can rewrite them one by one, but it is really not ideal.
In the new wx Pheonix, there is an AppConsole class which seems to be able to start an event loop without X-Windows. However it is not available in my local version of wx.
The Goal is ultimately to run the code in a cron job
I am basically looking for some advices/pointers as to how to tackle this issue. It would be nice to avoid as much rewrite as possible.
One way is to to use your local display. Ssh into your server with option -X to redirect the display to your workstation
ssh -X server
And on your server start the application, which will use your workstation's display automatically.
For Windows, there exists Xming or Cygwin for example.
As an alternative, you can use Xvfb, which provides a headless X server. You can then start your application by using xvfb-run as
xvfb-run my_wx_application
It turns out the rewriting is not too bad.
There are only three dependencies on wx objects in my code
1) subclassing wx.EvtHandler
2) wx.CallLater
3) wx.CallAfter
So the first case requires a reimplmentation
The other can be replaced by threading.Timer easily.

Python: connect to already opened console application

I have an interactive console application and I need to work with it using Python (send commands and receive output). The application is started by another one, I can't start it from Python script.
Is it possible to connect to already running console application and get access to its stdin/stdout?
Ideally the solution should work both in Windows and Unix, but just Windows version would also be helpful. Currently I am using the solution found here
http://code.activestate.com/recipes/440554/
but it doesn't allow connecting to existing process.
Thanks for any input,
Have you considered using sockets since they are straight forward for simple/streaming. They are also platform independent.
The most critical point is thread safety where having to pass IO streams between threads/processes tends to be hectic.
If on the other hand you use a socket, a lot can be communicated without adding too much complexity to how the processes work(coding an error prone RPC for instance).
try Documentation or
example

Strategies or support for making parts of a Twisted application reloadable?

I've written a specialized JSON-RPC server and just started working my way up into the application logic and finding it is a tad annoying to constantly having to stop/restart the server to make certain changes.
Previously I had a handler that ran in intervals to compare module modified time stamps with the past check then reload the module as needed. Unfortunately I don't trust it to work correctly now.
Is there a way for a reactor to stop and restart itself in a manner similar to Paster's Reloadable HTTPServer?
Shipped with Twisted is the twisted.python.rebuild module, so that is probably a good place to start.
Also see this SO question: Checking for code changes in all imported python modules
You could write something similar to paster's reloader, that would work like this:
start your main function, and before importing / using any twisted code, fork/spawn a subprocess.
In the subprocess, run your twisted application.
In the main process, run your code which checks for changed files. If code has changed, reload the subprocess.
However, the issue here is that unlike a development webserver, most twisted apps have a lot more state and just flat out killing / restarting the process is a bad idea, you may lose some state.
There is a way to do it cleanly:
When you spawn the twisted app, use subprocess.Popen() or similar, to get stdin/stdout pipes. Now in your subprocess, use the twisted reactor to listen on stdin (there is code for this in twisted, see twisted.internet.stdio which allows you to have a Protocol which talks to a stdio transport, in the usual twisted non-blocking manner).
Finally, when you decide it's time to reload, write something to the stdin of the subprocess telling it to shutdown. Now your twisted code can respond and shut down gracefully. Once it's cleanly quit, your master process can just spawn it again.
(Alternately you can use signals to achieve this, but this may not be OS portable)

Embedding a remote Python shell in an application

You can embed the IPython shell inside of your application so that it launches the shell in the foreground. Is there a way to embed a telnet server in a python app so that you can telnet to a certain port and launch a remote IPython shell?
Any tips for redirecting the input/output streams for IPython or how to hook it up to a telnet server library or recommendations for other libraries that could be used to implement this are much appreciated.
Python includes a telnet client, but not a telnet server. You can implement a telnet server using Twisted. Here's an example. As for hooking these things together, that's up to you.
Use Twisted Manhole. Docs are a bit lacking, but it's easy enough to set up a telnet-based remote server and it comes with a GTK-based GUI.
Main Twisted site
twisted.manhole API docs
I think you should base your server class on the SocketServer class from the standard library. You'll need to write a RequestHandler to read and echo input but a lot of the heavy lifting is already done for you.
You can use the ThreadingMixIn to make the server multi-threaded very easily.
Try to use xmlrpc namespace

Categories

Resources