views:

73

answers:

1

hi,

I have a C++ application which uses embedded python interpreter with the Python C API. It can evaluate Python files and source code with PyRun_SimpleFile and PyObject_CallMethod.

Now I have a python source code which has a worked thread which subclasses threading.Thread and has a simple run re-implementation:

import time
from threading import Thread
class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        while True:
            print "running..."
            time.sleep(0.2)

The problem is that the "running" is printed only once in the console.

How can I make sure that python threads keep on running parallel to my C++ applications GUI loop.

Thanks in advance,

Paul

A: 

What is the main thread doing? Is it simply returning control to your C++ app? If so, remember to release the GIL (Global Interpreter Lock) when not running any Python code in your main thread, otherwise your other Python threads will stall waiting for the GIL to be released.

The easiest way to do so is to use the Py_BEGIN_ALLOW_THREADS / Py_END_ALLOW_THREADS macros.

See the doc at: http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock

Antoine P.