Running computation in background thread python/pygtk

僤鯓⒐⒋嵵緔 提交于 2019-12-12 11:25:16

问题


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()).


回答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.




回答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.



来源:https://stackoverflow.com/questions/12094555/running-computation-in-background-thread-python-pygtk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!