2013-06-27

I sometimes run into situations where it would be nice to have one of my Python scripts communicate with another of my Python scripts. For example, I might want to send a message from a command-line script that runs in the background to my wxPython GUI that’s running on the same machine. I had heard of a solution involving Python’s socket module a couple of years ago, but didn’t investigate it until today when one of my friends was asking me how this was done. It turns out Cody Precord has a recipe in his wxPython Cookbook that covers this topic fairly well. I’ve taken his example and done my own thing with it for this article.

wxPython, threads and sockets, oh my!

Yes, we’re going to dive into threads in this article. They can be pretty confusing, but in this case it’s really very simple. As is the case with every GUI library, we need to be aware of how we communicate with wxPython from a thread. Why? Because if you use an unsafe wxPython method, the result is undefined. Sometimes it’ll work, sometimes it won’t. You’ll have weird issues that are hard to track down, so we need to be sure we’re communicating with wxPython in a thread-safe manner. To do so, we can use one of the following three methods:

wx.CallAfter (my favorite)

wx.CallLater (a derivative of the above)

wx.PostEvent (something I almost never use)

Now you have the knowledge of how to talk to wxPython from a thread. Let’s actually write some code! We’ll start with the thread code itself:

I went ahead and copied Cody’s name for this class, although I ended up simplifying my version quite a bit. IPC stands for inter-process communication and since that’s what we’re doing here, I thought I’d leave the name alone. In this call, we set up a socket that’s bound to 127.0.0.1 (AKA the localhost) and is listening on port 8080. If you know that port is already in use, be sure to change that port number to something that isn’t in use. Next we daemonize the thread so it will run indefinitely in the background and then we start it. Now it’s basically running in an infinite loop waiting for someone to send it a message. Read the code a few times until you understand how it works. When you’re ready, you can move on to the wxPython code below.

Note that the threading code above and the wxPython code below go into one file.

Here we set up our user interface using a simple frame and a panel with two widgets: a text control for displaying the messages that the GUI receives from the socket and a button. We use the button for testing the socket server thread. When you press the button, it will send a message to the socket listener which will then send a message back to the GUI to update the display. Kind of silly, but it makes for a good demo that everything is working the way you expect. You’ll notice we’re using pubsub to help in sending the message from the thread to the UI. Now we need to see if we can communicate with the socket server from a separate script.

So make sure you leave your GUI running while you open a new editor and write some code like this:

Now if you execute this second script in a second terminal, you should see the string “Python rocks!” appear in the text control in your GUI. It should look something like the following if you’ve pressed the button once before running the script above:



Wrapping Up

This sort of thing would also work in a non-GUI script too. You could theoretically have a master script running with a listener thread. When the listener receives a message, it tells the master script to continue processing. You could also use a completely different programming language to send socket messages to your GUI as well. Just use your imagination and you’ll find you can do all kinds of cool things with Python!

Additional Reading

The socket library’s official docs

The select library’s official docs

wxPython and threads

wxPython’s wiki entry on threads

wxPython and PubSub: A Simple Tutorial

Download the Source

Note: This code was tested using Python 2.6.6, wxPython 2.8.12.1 on Windows 7

wxipc.zip

Show more