source | issues

pygtkhelpers.gthreads

Helpers for integration of aysnchronous behaviour in PyGTK.

Warning

in order to get well-behaved threading, run :function:`initial_setup` as early as possible (befor doing any gui operations

copyright:2005-2010 by pygtkhelpers Authors
license:LGPL 2 or later (see README/COPYING/LICENSE)
class pygtkhelpers.gthreads.AsyncTask(work_callback=None, loop_callback=None, daemon=True)

Perform lengthy tasks without delaying the UI loop cycle.

AsyncTasks removes the boilerplate of deferring a task to a thread and receiving intermittent feedback from the thread. It Handles creating and starting a thread for the task, and forcing any user interface calls to be pushed to the GTK main loop from the thread, thus ensuring against insanity which invariably ensues if this precaustion is not taken.

It is also assumed that each action that the async worker performs cancels the old one (if it’s still working), thus there’s no problem when the task takes too long. You can either extend this class or pass two callable objects through its constructor.

The first on is the ‘work_callback’ this is where the lengthy operation must be performed. This object may return an object or a group of objects, these will be passed onto the second callback ‘loop_callback’. You must be aware on how the argument passing is done. If you return an object that is not a tuple then it’s passed directly to the loop callback. If you return None no arguments are supplied. If you return a tuple object then these will be the arguments sent to the loop callback.

The loop callback is called inside Gtk+’s main loop and it’s where you should stick code that affects the UI.

start(*args, **kwargs)

Start the task

This is:
  • not threadsave
  • assumed to be called in the gtk mainloop
class pygtkhelpers.gthreads.GeneratorTask(work_callback, loop_callback, complete_callback=None, priority=200, pass_generator=False)

The diference between this task and AsyncTask is that the work callback returns a generator. For each value the generator yields the loop callback is called inside Gtk+’s main loop.

Parameters:
  • work – callback that returns results
  • loop – callback inside the gtk thread
  • priority – gtk priority the loop callback will have
  • pass_generator – will pass the generator instance as generator_task to the worker callback

A simple example:

def work():
    for i in range(10000):
        yield i

def loop(val):
    print val

gt = GeneratorTask(work, loop)
gt.start()
import gtk
gtk.main()
pygtkhelpers.gthreads.gcall(func, *args, **kwargs)

Calls a function, with the given arguments inside Gtk’s main loop. Example:

gcall(lbl.set_text, "foo")

If this call would be made in a thread there could be problems, using it inside Gtk’s main loop makes it thread safe.

pygtkhelpers.gthreads.initial_setup()
  • set up gdk threading
  • enter it
  • set up glib mainloop threading
pygtkhelpers.gthreads.invoke_in_mainloop(func, *args, **kwargs)

invoke a function in the mainloop, pass the data back