Menu

PythonQt and Garbage Collection

Help
superfisi
2011-05-11
2013-04-06
  • superfisi

    superfisi - 2011-05-11

    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

     
  • Chris Bevan

    Chris Bevan - 2011-05-11

    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

     
  • superfisi

    superfisi - 2011-05-11

    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…

    The other issue is the Python GIL (global interpreter lock), this is not released by PythonQt, so while being in a Qt slot/signal call, the Python interpreter will be locked (so no other thread in Python can do anything). Probably it would be enough to unlock the GIL at all places where the Qt code is entered, but I am not sure about that.

    … 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)…

    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();
    /* Perform Python actions here. */
    result = CallSomeFunction();
    /* evaluate result or handle exception */
    /* Release the thread. No Python API allowed beyond this point. */
    PyGILState_Release(gstate);
    

    Thanks & BR,

    Alex

     
  • Chris Bevan

    Chris Bevan - 2011-05-11

    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

     
  • superfisi

    superfisi - 2011-05-11

    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

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.