| 1 |
=============== |
| 2 |
Multi Threading |
| 3 |
=============== |
| 4 |
|
| 5 |
Operations which could potentially block should not be executed in the main |
| 6 |
loop. The main loop is in charge of input processing and drawing and |
| 7 |
blocking it results in the user interface freezing. For the user this means |
| 8 |
not getting any feedback and not being able to pause or abort the operation |
| 9 |
which causes the problem. |
| 10 |
|
| 11 |
Such an operation might be: |
| 12 |
|
| 13 |
* Loading external resources like an image file on the web |
| 14 |
* Searching the local file system |
| 15 |
* Writing, reading and copying files |
| 16 |
* Calculations where the runtime depends on some external factor |
| 17 |
|
| 18 |
The following examples show |
| 19 |
|
| 20 |
* how Python threads, running in parallel to GTK, can interact with the UI |
| 21 |
* how to use and control asynchronous I/O operations in glib |
| 22 |
|
| 23 |
This page will discuss multi-threading. A separate page discusses :doc:asynchronous. |
| 24 |
|
| 25 |
This example uses a Python thread to execute code in the background |
| 26 |
while still showing feedback on the progress in a window. |
| 27 |
|
| 28 |
.. code:: python |
| 29 |
|
| 30 |
import threading |
| 31 |
import time |
| 32 |
import gi |
| 33 |
|
| 34 |
gi.require_version('Gtk', '4.0') |
| 35 |
from gi.repository import GLib, Gtk, GObject |
| 36 |
|
| 37 |
|
| 38 |
class Application(Gtk.Application): |
| 39 |
|
| 40 |
def do_activate(self): |
| 41 |
window = Gtk.ApplicationWindow(application=self) |
| 42 |
self.progress = Gtk.ProgressBar(show_text=True) |
| 43 |
|
| 44 |
window.set_child(self.progress) |
| 45 |
window.present() |
| 46 |
|
| 47 |
thread = threading.Thread(target=self.example_target) |
| 48 |
thread.daemon = True |
| 49 |
thread.start() |
| 50 |
|
| 51 |
def update_progress(self, i): |
| 52 |
self.progress.pulse() |
| 53 |
self.progress.set_text(str(i)) |
| 54 |
return False |
| 55 |
|
| 56 |
def example_target(self): |
| 57 |
for i in range(50): |
| 58 |
GLib.idle_add(self.update_progress, i) |
| 59 |
time.sleep(0.2) |
| 60 |
|
| 61 |
|
| 62 |
app = Application() |
| 63 |
app.run() |
| 64 |
|
| 65 |
|
| 66 |
The example shows a simple window containing a progress bar. After everything |
| 67 |
is set up it constructs a Python thread, passes it a function to execute, |
| 68 |
starts the thread and the GTK main loop. After the main loop is started it is |
| 69 |
possible to see the window and interact with it. |
| 70 |
|
| 71 |
In the background ``example_target()`` gets executed and calls |
| 72 |
:func:`GLib.idle_add` and :func:`time.sleep` in a loop. In this example |
| 73 |
:func:`time.sleep` represents the blocking operation. :func:`GLib.idle_add` |
| 74 |
takes the ``update_progress()`` function and arguments that will get passed to |
| 75 |
the function and asks the main loop to schedule its execution in the main |
| 76 |
thread. This is needed because GTK isn't thread safe; only one thread, the |
| 77 |
main thread, is allowed to call GTK code at all times. |
| 78 |
|
| 79 |
|
| 80 |
Threads: FAQ |
| 81 |
------------ |
| 82 |
|
| 83 |
* I'm porting code from pygtk (GTK 2) to PyGObject (GTK 3). Has anything |
| 84 |
changed regarding threads? |
| 85 |
|
| 86 |
Short answer: No. |
| 87 |
|
| 88 |
Long answer: ``gtk.gdk.threads_init()``, ``gtk.gdk.threads_enter()`` and |
| 89 |
``gtk.gdk.threads_leave()`` are now :func:`Gdk.threads_init`, |
| 90 |
:func:`Gdk.threads_enter` and :func:`Gdk.threads_leave`. |
| 91 |
``gobject.threads_init()`` can be removed. |
| 92 |
|
| 93 |
* I'm using :func:`Gdk.threads_init` and want to get rid of it. What do I |
| 94 |
need to do? |
| 95 |
|
| 96 |
* Remove any :func:`Gdk.threads_init()`, :func:`Gdk.threads_enter` and |
| 97 |
:func:`Gdk.threads_leave` calls. In case they get executed in a thread, |
| 98 |
move the GTK code into its own function and schedule it using |
| 99 |
:func:`GLib.idle_add`. Be aware that the newly created function will be |
| 100 |
executed some time later, so other stuff can happen in between. |
| 101 |
|
| 102 |
* Replace any call to ``Gdk.threads_add_*()`` with their GLib counterpart. |
| 103 |
For example :func:`GLib.idle_add` instead of :func:`Gdk.threads_add_idle`. |
| 104 |
|
| 105 |
* What about signals and threads? |
| 106 |
|
| 107 |
Signals get executed in the context they are emitted from. In which context |
| 108 |
the object is created or where ``connect()`` is called from doesn't matter. |
| 109 |
In GStreamer, for example, some signals can be called from a different |
| 110 |
thread, see the respective signal documentation for when this is the case. |
| 111 |
In case you connect to such a signal you have to make sure to not call any |
| 112 |
GTK code or use :func:`GLib.idle_add` accordingly. |
| 113 |
|
| 114 |
* What if I need to call GTK code in signal handlers emitted from a thread? |
| 115 |
|
| 116 |
In case you have a signal that is emitted from another thread and you need |
| 117 |
to call GTK code during and not after signal handling, you can push the |
| 118 |
operation with an :class:`threading.Event` object to the main loop and wait |
| 119 |
in the signal handler until the operation gets scheduled and the result is |
| 120 |
available. Be aware that if the signal is emitted from the main loop this |
| 121 |
will deadlock. See the following example |
| 122 |
|
| 123 |
.. code:: python |
| 124 |
|
| 125 |
# [...] |
| 126 |
|
| 127 |
toggle_button = Gtk.ToggleButton() |
| 128 |
|
| 129 |
def signal_handler_in_thread(): |
| 130 |
|
| 131 |
def function_calling_gtk(event, result): |
| 132 |
result.append(toggle_button.get_active()) |
| 133 |
event.set() |
| 134 |
|
| 135 |
event = threading.Event() |
| 136 |
result = [] |
| 137 |
GLib.idle_add(function_calling_gtk, event, result) |
| 138 |
event.wait() |
| 139 |
toggle_button_is_active = result[0] |
| 140 |
print(toggle_button_is_active) |
| 141 |
|
| 142 |
# [...] |
| 143 |
|
| 144 |
* What about the Python `GIL |
| 145 |
<https://en.wikipedia.org/wiki/Global_Interpreter_Lock>`__ ? |
| 146 |
|
| 147 |
Similar to I/O operations in Python, all PyGObject calls release the |
| 148 |
GIL during their execution and other Python threads can be executed |
| 149 |
during that time. |