I'm writing a Python + GObject app that needs to read a non-trivial amount of data from disk upon start. The data is read synchronously and it takes about 10 seconds to finish the read operation, during which time the loading of the UI is delayed.
I'd like to run the task asynchronously, and get a notification when it's ready, without blocking the UI, more or less like:
def take_ages():
read_a_huge_file_from_disk()
def on_finished_long_task():
print "Finished!"
run_long_task(task=take_ages, callback=on_finished_long_task)
load_the_UI_without_blocking_on_long_task()
I've used GTask in the past for this sort of thing, but I'm concerned that its code hasn't been touched in 3 years, let alone been ported to GObject Introspection. Most importantly, it's no longer available in Ubuntu 12.04. So I'm looking for an easy way to run tasks asynchronously, either in a standard Python way or in a GObject/GTK+ standard way.
Edit: here's some code with an example of what I'm trying to do. I've tried python-defer
as suggested in the comments, but I could not manage to run the long task asynchronously and let the UI load without having to wait for it to finish. Browse the test code.
Is there an easy and widely used way of running asynchronous tasks and get notified when they're finished?
Your problem is a very common one, therefore there are tons of solutions (sheds, queues with multiprocessing or threading, worker pools, ...)
Since it is so common, there is also a python build-in solution (in 3.2, but backported here: http://pypi.python.org/pypi/futures) called concurrent.futures. 'Futures' are available in many languages, therefore python calls them the same. Here are the typical calls (and here is your full example, however, the db part is replaced by sleep, see below why).
Now to your problem, which is much more complicated than your simple example suggests. In general you have threads or processes to solve this, but here is why your example is so complicated:
slow_load
from the DB are not pickelable, which means that they can not simply be passed between processes. So: no multiprocessing with softwarecenter results!print
, no gtk state changes, except adding a callback!threads_init
, and if you call a gtk or alike method, you have to protect that method (in earlier versions this wasgtk.gdk.threads_enter()
,gtk.gdk.threads_leave()
. see for example gstreamer: http://pygstdocs.berlios.de/pygst-tutorial/playbin.html).I can give you the following suggestion:
slow_load
to return pickelable results and use futures with processes.As a note: the solutions given by the others (
Gio.io_scheduler_push_job
,async_call
) do work withtime.sleep
but not withsoftwarecenter.db
. This is, because it all boils down to threads or processes and threads to not work with gtk andsoftwarecenter
.Here's another option using GIO's I/O Scheduler (I've never used it before from Python, but the example below seems to run fine).
Use the introspected
Gio
API to read a file, with its asynchronous methods, and when making the initial call, do it as a timeout withGLib.timeout_add_seconds(3, call_the_gio_stuff)
wherecall_the_gio_stuff
is a function which returnsFalse
.The timeout here is necessary to add (a different number of seconds may be required, though), because while the Gio async calls are asynchronous, they are not non-blocking, meaning that the heavy disk activity of reading a large file, or large number of files, can result in blocked UI, as the UI and I/O are still in the same (main) thread.
If you want to write your own functions to be async, and integrate with the main loop, using Python's file I/O APIs, you'll have to write the code as a GObject, or to pass callbacks around, or use
python-defer
to help you do it. But it's best to use Gio here, as it can bring you a lot of nice features, especially if you're doing file open/save stuff in the UX.You can also use GLib.idle_add(callback) to call the long running task once the GLib Mainloop finishes all it's higher priority events (which I believe includes building the UI).
I think it bears noting that this is a convoluted way to do what @mhall suggested.
Essentially, you've got a run this then run that function of async_call.
If you want to see how it works, you can play with the sleep timer and keep clicking the button. It's essentially the same as @mhall's answer except that there's example code.
Based on this which is not my work.
Additional note, you have to let the other thread finish before it will terminate properly or check for a file.lock in your child thread.
Edit to address comment:
Initially I forgot
GObject.threads_init()
. Evidently when the button fired, it initialized threading for me. This masked the mistake for me.Generally the flow is create the window in memory, immediately launch the other thread, when the thread completes update the button. I added an additional sleep before I even called Gtk.main to verify that the full update COULD run before the window was even drawn. I also commented it out to verify that the thread launch doesn't impede window drawing at all.