A simple, non-network example of Twisted / PyGTK

I struggled with getting some asynchronous activity to work in PyGTK when someone suggested I look at using Twisted.

I know that Twisted started as a network environment, but it can be used for other things. However, every single example I have ever seen includes many network codes. I would like to see an example of using Twisted for a simple PyGTK desktop application, without requiring too much mental effort to understand the network aspect of things.

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

(Yes, I saw pbgtk2.py . It is uncommented, network oriented and completely puzzled by the newbie.)

Updated: I listed various problems with glib.idle_add / gtk.gdk.lock , and friends did not work fine under Windows. This was all justified on the pygtk list - there is some kind of trickery that is needed with PyGTK to get asynchronous behavior running on Windows.

However, my point of view is still that at any time when I mention asynchronous activity in PyGTK, someone says: "Do not use threads, use Twisted!" I want to know why and how.

+4
source share
1 answer

Twisted to perform asynchronous tasks in pygtk just uses functions like gobject.io_add_watch / glib.io_add_watch and gobject.timeout_add / glib.timeout_add (plus some others, you will find them in gobject and glib module ), so there is not much difference in using the original ones pygtk functions or twisted if you don't need a network.

As an add-on, twisted has the same problems as pygtk with asynchronous tasks, twisted uses the same loop as pygtk, and therefore it blocks if you perform some kind of blocking task!

It is best to use one of the glib functions, which are mainly designed to handle such situations.

I tested the correct behavior in the application under the twisted + pygtk windows, but avoided doing blocking things (maximum reading from a large file, piece by piece mainly using glib.idle_add or glib.io_add_watch , in the sense that twisted uses something like that).

For example, I'm not sure if the spawning process and handling stdout with glib.io_add_watch does not seem to work. I wrote an article on my blog that handles the execution of asynchronous processes in pygtk, not very sure what works on Windows, although this may depend on the version.

+2
source

Source: https://habr.com/ru/post/1314793/


All Articles