GTK4 Questions: using / applying GTK4 -- how do we go about threading? [beginner]

hi,

a Question if I may:

(There seems to be no dedicated topic for questions and answers about using (not developing) GTK)

Using Python, how do we do threading?

Q-a:

Is this right?

asyncio and threading serve two different purposes… ?

(I think I’m interested in threading, cause I want to display search results, searching through local files, continuously, as it’s happening, in a textview, adding lines, periodically.)

The distinction between I/O-bound and CPU-bound work is the single most important concept for deciding when to use asyncio. If your program spends time waiting — for the network, for the database, for a file system operation — asyncio can help. If it spends time computing — parsing, number crunching, data transformations — asyncio will not help, and you need multiprocessing instead. Knowing which category your bottleneck falls into saves you from applying the wrong tool.
openpython dot org / articles/python-asyncio-async-await-guide

Q-b:

Threading exists in GTK4, right?

Q-c:

This example here is a good and valid one, right?

https://pygobject.gnome.org/guide/threading.html

Q-d:

Is this valid for GTK, too?
https://docs.python.org/3/library/threading.html

Q-e:

Are there any “extra” recommendations? :slight_smile:

Thanks in advance!
(and sorry if this isn’t the right place to ask about using (not developing) GTK)

Peter *

______
* I am a programming enthusiast, not a developer.

Hi,

(a)
In Python, if you’re I/O-bound (like your searching example) then both threading or asyncio can help. It’s just an implementation detail.

If you’re CPU-bound, them multiprocessing can be useful.

(b)
What actually matters is if the programming language you use with GTK (Python in your case) supports threading.

GTK itself is not thread-safe, so all calls to GTK APIs shall be done from the main thread. Other treads may be used for async I/O operations (like the searching part in your example).

(c)
Yes!

(d)
Yes too. Actually the (c) example is an adaptation of (d) to work with GTK.

(e)
Most important is the thread-safety limitation mentioned in (b).

You can look at Asynchronous Programming — PyGObject to see how using GTK with asyncio looks like.

Thanks very much!

Such a great boost to my advance :slight_smile: