multi-thread python interpreaters and c++ program

M

myopc

hi, all
I am ruuning a c++ program (boost python) , which create many python
interpreaters and each run a python script with use multi-thread
(threading).
when the c++ main program exit, I want to shut down python interpreaters,
but it crashed. I have googled a lot but cant get any clue.
here is my code, your suggestion will be greatly appreciated.

c++ :
/**** test multi interpreaters ***/
#include <Python.h>
#include <boost/python.hpp>
#include <boost/bind.hpp>
#include <string>
#include <windows.h>

using namespace boost::python;

static const char* strs[3]={
"m1","m2","m3"
};
static void xxx(const char* abc){
fprintf(stderr, "this is xxx %s\n", abc);
}

PyThreadState* testPy(int i){
char buf[128];
PyThreadState* ts = Py_NewInterpreter();

object main_namespace = import("__main__").attr("__dict__");
main_namespace["xxx"]= xxx;
main_namespace["aaa"]= i+1;
main_namespace["strs"]= strs;

sprintf(buf, "execfile('pyinter/%d.py')",i+1);
if(!PyRun_SimpleString(buf)){
return ts;
}else return 0;
}

int main(int argc, char** argv){
PyThreadState *ts[3];
Py_InitializeEx(0);
PyEval_InitThreads();
for(int i=0 ; i< 3; i++){
ts= testPy(i);
if(! ts ){
printf("run %d error\n",i);
} else {
printf("run %d ok\n",i);
}
}

PyEval_ReleaseLock(); /// release the lock, interpreaters run
Sleep(3500);
for(int i=0; i< 3; i++){
if(! ts)continue;

printf("delete %d ", i);
PyEval_AcquireThread(ts);
Py_Finalize(); ///shut down interpreaters ,crash here
PyEval_ReleaseLock();
Sleep(10);
printf(" ok\n");
}
Sleep(1500);
printf("exit...\n");
}

each interpreater uses the same python script:

class MyTimer(object):
def __init__(self, interval, function, args=[], kwargs={}):
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs

def start(self):
self.stop()
import threading
self._timer = threading.Timer(self.interval, self._run)
self._timer.setDaemon(True)
self._timer.start() ### start a python thread, keep running

def restart(self):
self.start()

def stop(self):
if self.__dict__.has_key("_timer"):
self._timer.cancel()
del self._timer

def _run(self):
try:
self.function(strs)
except:
pass
self.restart()

abc= MyTimer(aaa,xxx);
abc.start();
 
L

Lie Ryan

myopc said:
hi, all
I am ruuning a c++ program (boost python) , which create many python
interpreaters and each run a python script with use multi-thread
(threading).
when the c++ main program exit, I want to shut down python
interpreaters, but it crashed. I have googled a lot but cant get any clue.
here is my code, your suggestion will be greatly appreciated.

There is no clean way to terminate a thread cleanly from outside. You
need to let the thread terminates by itself. If it is a pure python
code, this can be done by one of:
1) setting a global/thread stop value that is checked by each threads
2) inserting a QUIT event to the event-loop


I have never used boost before, so I don't know what it can and cannot
do from outside the python's interpreter. However, this should give you
an idea of what to do...
 
M

myopc

thanks for your reply!

Lie Ryan said:
There is no clean way to terminate a thread cleanly from outside. You
need to let the thread terminates by itself. If it is a pure python
code, this can be done by one of:
1) setting a global/thread stop value that is checked by each threads
2) inserting a QUIT event to the event-loop


I have never used boost before, so I don't know what it can and cannot
do from outside the python's interpreter. However, this should give you
an idea of what to do...
 
F

Floris Bruynooghe

  I am ruuning a c++ program (boost python) , which create many python
interpreaters and each run a python script with use multi-thread
(threading).
when the c++ main program exit, I want to shut down python interpreaters,
but it crashed.

Your threads are daemonic, you could be seeing http://bugs.python.org/issue1856
You'll have to check your stack in a debugger to know. But as said
this can be avoided by making the threads finish themself and joining
them.

Regards
Floris
 
M

myopc

thanks,
I use gdb to debug, and cant find any symbol in the stack.
I wanna to exit from outside python, without tell anything to the
interpreator,
but it seems impossible

"Floris Bruynooghe" <[email protected]>
:6deac4c8-b208-4fbf-8098-8dc7af7f881b@r34g2000vba.googlegroups.com...
I am ruuning a c++ program (boost python) , which create many python
interpreaters and each run a python script with use multi-thread
(threading).
when the c++ main program exit, I want to shut down python interpreaters,
but it crashed.

Your threads are daemonic, you could be seeing
http://bugs.python.org/issue1856
You'll have to check your stack in a debugger to know. But as said
this can be avoided by making the threads finish themself and joining
them.

Regards
Floris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,708
Messages
2,569,347
Members
44,654
Latest member
7Jhamal74

Latest Threads

Top