[Qgis-developer] Bug? Python plug-in threadS seem to run only when Python console is busy.

Martin Dobias wonder.sk at gmail.com
Tue Jul 24 22:59:49 PDT 2012


On Tue, Jul 24, 2012 at 12:28 AM, Mikhail Titov <mlt at gmx.us> wrote:
> All:
>
> I am writing a multi-threaded python plug-in. It is meant to slowly
> extract data from NLDAS-2 for selected features using OPeNDAP in the
> background as I don't want to block GUI.
>
> It seems that the thread I create in my plug-in is mostly dormant unless
> I have something like time.sleep(alot) running in Python console. Is it
> normal? I even tried to start QThread with QThread.HighPriority with the
> same result:(

Hi Mikhail

It has to do with Python's Global Interpreter Lock (GIL). The main GUI
thread holds the lock all the time, that's why code in other python
thread does not run. Doing something in python console helps because
when python runs any commands, it releases and re-acquires the lock
every now and then - but when it does not run, the lock is being held.

If I remember correctly, I have managed to solve this issue (with some
more) in threading branch by calling PyEval_InitThreads() and
PyEval_SaveThread() from QgsPythonUtilsImpl::initPython(). Other c++
functions doing python API calls then have to always acquire GIL and
release it afterwards with PyGILState_Ensure() and PyGILState_Release(
gstate ).

These changes probably have to be ported in order to have working
Python threads in QGIS, e.g. from Alex's port of threading branch:
https://github.com/alexbruy/Quantum-GIS/blob/adopt-threading/src/python/qgspythonutilsimpl.cpp

Martin


More information about the Qgis-developer mailing list