PythonQt and Garbage Collection
Dynamic Python binding for Qt Applications
Brought to you by:
florianlink,
marcusbarann
Hi all,
we have a question regarding garbage collection. When executing one of our Python-scripts within a PythonQt-environment, sometimes the following error occurs…
"Fatal Python error: GC object already tracked"
… does anyone recognize this error? We do not have any clue what the possible reason could be. I have to mention that we are currently trying to execute more than one Python script at a time (each within a separate QThread).
Thanks in advance,
Alex
Hi Alex,
I don't recognise that error, but I'd guess that the execution of more than one Python script at a time in separate threads. If you're using Python 2.x, are you acquiring the Python Global Interpreter Lock (GIL) before executing those scripts in each thread?
I don't think the Python interpreter itself is intended to be thread-safe, and the GIL is its way to stop weird things breaking through multithreading.
Cheers,
- Chris
Hi Chris,
thank you for your quick reply! I have two more questions:
1. Is there a chance of running more than one completely independant instances of the Python-interpreter. We do not have the requirement for our scripts to exchange any data between each other.
2. What's with this GIL? In another topic Florian wrote this…
… so should we simply ensure the GIL-state as being unlocked every time when leaving a Python-method (respectively lock it when entering)?
Something like (copied from Python-docu)…
Thanks & BR,
Alex
Hi Alex,
To be honest, I haven't messed around with the GIL that much myself. In my own application, I instead just ensure that all of my Python function accesses are serialised by my own code - something like the following:
QMutex sPythonMutex;
void runPython(const QString& Command)
{
QMutexLocker lLock(sPythonMutex);
// … Run Python command here
}
void runThread1() { runPython("print 1"); }
void runThread2() { runPython("print 2"); }
This is off the top of my head, so apologies for any errors, but hopefully it illustrates the idea.
Cheers,
- Chris
Hi Chris,
thank you for your reply.
Your solution looks good, but since independant parts of our application are using PythonQt on their own we would have to implement some kind of global sync-mechanism (using Mutex and MutexLocker). We will look into that.
Besides that we will also take a look into this GIL-stuff, keeping you posted.
If anyone else has experience in using GIL, please let me know! :)
Thanks & BR,
Alex