On 07/05/2010 04:57 PM, Jason Heeris wrote:
> On 5 July 2010 17:36, A.T.Hofkamp <a.t.hofk...@tue.nl> wrote:
>> Or you could drop threads entirely, and do your async activities using the
>> Twisted framework, designed for making asynchronous programs (where GTK
>> event handling is just one of the asynchronous sources).
> 
> Please don't take this as a flame, but I've had this advice before
> (usually on #python), and I usually go through this process:
> 
> 1. Look at Twisted website
> 2. Only read things about twisted being a networking framework
> 3. Someone corrects me, saying "Twisted is so much more!" (That may be
> the case, but the fact that the website goes on and on without
> mentioning this does not inspire confidence.)
> 4. I search the Twisted website for documentation on/examples of GTK
> applications
> 5. I give up and search the Twisted website for documentation
> on/examples of *any* non-network based applications
> 6. I give up and search the greater web for documentation on/examples
> of Twisted GTK applications
> 7. I give up on Twisted and find another way.
> 
> Really, I would love to get a grip on Twisted. It seems really useful.
> But I've still not found any kind of stepping stone into it. Please,
> please, please if you know of one, post it so I can add another tool
> to my belt. I will rescind everything I've said here and then some.
I don't know of any examples unfortunately and I must admit that I spent
quite a bit of time getting to grips with it, but in the end it is
remarkably simple (much more simple than I first thought too - so don't
let that put you off):
import gtk.gdk
gtk.gdk.threads_init()
from twisted.internet import gtk2reactor, reactor
gtk2reactor.install(useGtk=useGtk)
reactor.run()

You can just use pygtk and twisted as you normally would: network events
are handled just like gui events, there is just one main loop waiting,
events get dispatched to whatever method you registered. (ie: via
connect for gtk, or reactor.XXX for twisted)

There are pretty good examples for using Twisted on their website (when
it is up). As for pygtk, finding examples shouldn't be a problem.

Now, for IO operations that can block (ie: db query, file operations),
just run those in a separate thread (thread.start_new_thread) and if you
need to interact with the GUI from that thread, do it via gobject.idle_add()

For someone who was used to Java/C#/C threading models, this is a
totally different approach and I must say that I really like the
simplicity of it all.

Hope this helps.

Antoine

> 
> (Since the Twisted website is currently down, I can't tell if things
> have changed and I should get over it and have another try.)
> 
> Cheers,
> Jason
> _______________________________________________
> pygtk mailing list   pygtk@daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/

_______________________________________________
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to