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:
And for older ones:
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
: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.