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) |
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 the task
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: |
|
---|
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()
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.
invoke a function in the mainloop, pass the data back