It looks like it is blocking the application, but what is doing is processing events. One of the events has to terminate the loop. You can check the idea behind the event loops in Wikipedia.
If you do not want to program a graphical interface, then you do not need Gtk, you only need Glib. Here an example to show you how the main loop works (the concept is similar in Gtk and Glib):
from gi.repository import GLib, GObject
counter = 0
def callback(*args):
global counter
counter += 1
print 'callback called', counter
if counter > 10:
print 'last call'
return False
return True
def terminate(*args):
print 'Bye bye'
loop.quit()
GObject.timeout_add(100, callback)
GObject.timeout_add(3000, terminate)
loop = GLib.MainLoop()
loop.run()
If the callback returns False
, then it will be removed and not called anymore. If you want the callback to be called again, it has to return True
(as you can see in the function callback
).
I set another callback terminate
to show how to quit from the loop. If you do not do it explicitly, then GLib will continue waiting for more events (It does not have any way to know what you want to do).
With the PyGTK (old and deprecated), the code will be:
import gobject, glib, gtk
counter = 0
def callback(*args):
global counter
counter += 1
print 'callback called', counter
if counter > 10:
print 'last call'
return False
return True
def terminate(*args):
print 'Bye bye'
loop.quit()
gobject.timeout_add(100, callback)
gobject.timeout_add(3000, terminate)
loop = glib.MainLoop()
loop.run()