Some programming language libraries have special queues that can be used to pass messages between threads in a multithreaded application. Some languages, like Erlang or Termite Scheme , have message passing so ingrained that you'd be crazy not to use those features.

Programming with functions or objects is also a form of message passing. In this case, calling a function or method is equivalent to sending a message to your program or to an object in your program. Essentially, your program is communicating with itself. But that's still message passing. Just like how people sometimes like to talk to themselves (well, at least I do).

Message passing is the means by which processes communicate. Much like humans communicate by talking to each other or writing notes to each other, so do computer programs communicate with each other. Sockets and pipes are prime examples of message passing technologies. Shared memory can also be used for message passing.

Message Passing with GTK

Here I am going to explain message passing between GTK and your custom main loop. I am not explaining message passing inside GTK (e.g. between GTK widgets - you'll have to read the GTK docs for more info about that).

GTK has a so-called "idle loop." This idle loop is how you can interface with GTK's main loop. GTK has a special function called "idle_add" which lets us add messages to be processed in the idle loop. I am going to describe two types of messages here:

Asynchronous messages These are messages that you insert into GTK's idle loop, but you do not care about getting a response. Synchronous messages: These are messages that you insert into GTK's idle loop, but you do care about getting a response.

In the Python programming language, functions are first-class citizens that can be passed around just like normal data. Thus, a message really is just a function reference (or a functor) that we inject into GTK's idle loop. GTK's idle loop will merely call/execute the function. In C, this is similar to putting a function pointer into a queue which another loop dequeues and then calls the function associated with that function pointer.

Asynchronous GTK Message Here is the magic trick for asynchronous GTK messages in Python. By the way, if this code looks like giberish to you, then read up on variable arguments and keyword arguments in Python. You'll also want to read about tuple unpacking in Python. You might also want to read about higher order functions which is a concept from functional programming. (Sorry, I know that is a long list of things to read about.): import gobject # This is a function which takes a function as an argument and # returns yet another function (this is what higher order functions # are all about!). (Side note: this can be invoked/applied with # Python's decorator syntax, '@', if you so desire.): def asynchronous_gtk_message(fun): def worker((function, args, kwargs)): # Apply is a function that takes a function as its first # argument and calls it with positional argument taken # from "args" and keyword arguments taken from "kwargs": apply(function, args, kwargs) # This is a special type of function known as a "closure" # (although it is not quite as advanced as a closure in Lisp # or Ruby. Challenge question: do you know why?). In C++, # a class-based functor which defines operator() (or Boost.Lambda) # is the closest you can get to a closure: def fun2(*args, **kwargs): # "worker" is none other than the function just defined # above (with "def worker"): gobject.idle_add(worker, (fun, args, kwargs)) # Here, the closure is returned and must be called at some later # point in the program: return fun2 The code above has a lot of comments, so here is the same code again with all the comments stripped out (some people may find this easier to read): import gobject def asynchronous_gtk_message(fun): def worker((function, args, kwargs)): apply(function, args, kwargs) def fun2(*args, **kwargs): gobject.idle_add(worker, (fun, args, kwargs)) return fun2 Despite being a few lines of code, there are some pretty deep programming concepts required to understand the code above. But it is concise and expressive and fairly hard to introduce bugs (because it is so simple). Here is an example of using "asynchronous_gtk_message" to manipulate a web browser widget (specifically, WebKit) running in GTK: browser = ... # read about synchronous_gtk_message below to see what goes here ... asynchronous_gtk_message(browser.execute_script)('alert("oops")') # or, alternatively: async_execute_script = asynchronous_gtk_message(browser.execute_script) async_execute_script('alert("oops")') Note that "asynchronous_gtk_message" does not actually do anything. All it does is return a special function (remember the closure from above?). And it is that special function which we must call whenever we want to actually send an asynchronous message. Notice how we ignore the return value from the message? Well, that is what makes it asynchronous; And fast.