Is there a way to run a python thread in the background without locking down the rest of python during time-consuming instructions?
I'm trying to do time-consuming calculations in a background thread of a python (pygtk) application. I understand how threads work. The problem is that each time I run an expensive operation in any thread (example: PIL's image.load() for large images), it blocks all python threads until the operation is completed, even though it is in a separate thread.
So, is there a way to run a python thread in the background without locking down the rest of python? (I don't care how long they take as long as they don't lock down my GUI. I just can't have my GUI unresponsive for several seconds at a time). Using sleep statements doesn't work because my problem is with single commands that take a long time (like image.load()).
Since you're using pygtk, did you call threads_init()
?
For newer versions:
>>> from gi.repository import GObject
>>> GObject.threads_init()
And for older ones:
>>> import gobject
>>> gobject.threads_init()
Also make sure you do not call any GUI method from your thread or your application will break in weird ways. An easy way around this is to use GObject.idle_add
:
idle_add(callable, user_data=None, priority=None) -> source id
callable receives (user_data)
Adds a callable to be called whenever there are no higher priority
events pending to the default main loop.
For doing processing on the background, you can also go with subprocess of python. It will create a sub_process (independent of the original execution ) , for which you can use .poll at certain intervals to check the if the process is done . If you run .communicate then whole process will wait for subprocess to complete. Also, you can refer this document :- threading-subprocess
I also believe that there are ways to get around you problem , just by using normal threading in python.