Skip to content Skip to sidebar Skip to footer

Running Computation In Background Thread Python/pygtk

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

Solution 1:

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.

Solution 2:

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.

Post a Comment for "Running Computation In Background Thread Python/pygtk"