tags:

views:

91

answers:

1

I was struggling with getting some asynchronous activity to work under PyGTK, when someone suggested that I look at using Twisted.

I know that Twisted started as a networking framework, but that it can be used for other things. However, every single example I've ever seen involves a whole lot of network-based code. I would like to see an example of using Twisted for a simple PyGTK desktop app, without the needing to expend the extra mental effort of understanding the network aspect of things.

So: Is there a clean, simple tutorial for or example of using Twisted to create a GTK (PyGTK) app and perform asynchronous tasks?

(Yes, I've seen pbgtk2.py. It's uncommented, network-centric and completely baffling to a newcomer.)

Updated: I had listed various gripes with glib.idle_add/gtk.gdk.lock and friends not working properly under Windows. This was all reasoned out on the pygtk list - there's some trickery that is needed with PyGTK to get asynchronous behaviour working under Windows.

However, my point still stands that any time I mention doing asynchronous activity in PyGTK, someone says "don't use threads, use Twisted!" I want to know why and how.

+1  A: 

Twisted to perform is asynchronous tasks in pygtk simply uses functions such as gobject.io_add_watch/glib.io_add_watch and gobject.timeout_add/glib.timeout_add (plus some others, you find them in the gobject and glib module), so there's not much difference in using raw pygtk functions or twisted if you don't need networking.

As an addition twisted has the same problems as pygtk with asynchronous tasks, twisted use the same loop as of pygtk and so it gets blocked if you perform some blocking task!

The best thing to do is to use one of the glib functions that are intended basically for handle such situations.

I've tested in an application the correct behaviour under windows of twisted+pygtk but I avoided to do blocking stuff (max reading from a large file, chunk per chunk basically using glib.idle_add or glib.io_add_watch, in the sense that twisted uses something like that).

For example I'm not sure that spawning process and processing stdout with glib.io_add_watch seems to not work. I've written an article on my blog that handle the performing of asynchronous processes in pygtk, not very sure that works on windows though it may depend on the version.

pygabriel
I was originally confused about the various main loop helper functions in glib/pygtk, but this has been sorted out. The rest of my question still stands, more or less. *I've updated my question to cover this.*
detly
These functions _are_ the only option, twisted don't do anything different than them! (when in conjunction with pygtk)And they work on windows. No magic, the code is there: http://twistedmatrix.com/trac/browser/tags/releases/twisted-9.0.0/twisted/internet/gtk2reactor.pyThe problem you have is related to "blocking" stuff, not completely related with the asyncronous framework twisted (if you run blocking code in twisted, it's however runned in the loop, same as pygtk). You have to use "tricks" anyway.
pygabriel
Using the the `glib` functions for asynchronous activity isn't enough, I need to use them in conjunction with the Python `threading` tools (for heavy I/O) or `multiprocessing` (for heavy CPU usage). I understand that those functions are used under the hood for Twisted anyway, but I was under the impression that Twisted might give me a better API for long blocking activities than a whole mess of threads. **But I can't tell if that's really true, since there are no real GTK examples!** Hence, the question. Now, if Twisted really adds nothing of real benefit, then that's a valid answer too.
detly
To put it another way: is using Twisted for this sort of stuff like trying to squeeze a square peg into a round hole? If so, fine. If not, I'd appreciate an example. Either way, I learn something :)
detly
I focused on the fact that twisted doesn't give any addition in respect of pygtk for handling such blocking tasks, and suffers for the same problems (while I've not mentioned examples).As far as I know there isn't escape to use threading or processes for such tasks, you may want to take a look to pygtkhelpers that have some nice functions for handling async tasks:http://bitbucket.org/aafshar/pygtkhelpers-main/src/tip/pygtkhelpers/gthreads.py
pygabriel
That won't work under Windows. The GUI calls need to be wrapped in `with gtk.gdk.lock` as well as calling `gtk.gdk.threads_init()` BEFORE starting the main loop. In fact, the main loop needs to be started inside the `gtk.gdk.lock`. See the PyGTK mailing list thread referenced in my question. I'll file an issue.
detly