I’am writing a multi-threaded program in C. Before creating the threads, a global python environment is initialized by calling Py_Initialize(). Then, in every created thread, the global python environment is shared, and each thread calls a python method with the parameters converted in C. Everything works well until here.
When I use time.sleep() in loaded python modules, the C program raises a Segmentation Fault. Furthermore, the loaded python module is supposed to load another C lib to continue the work. I’ve written the following stupid counter lib to test it:
# python part, call the counter function
lib = ctypes.cdll.LoadLibrary(libpycount.so)
for i in xrange(10):
lib.count()
// C part, dummy countings
#include <stdio.h>
int counter = 1;
void
count() {
printf("counter:%d \n", counter);
counter++;
}
I guess that it might be because I didn’t manage the complex thread creation in the right way. And I’ve found Non-Python created threads in the python doc.
Any ideas or suggestions?
My problem has been solved. You may have your problems more particular, so I’m trying to write my solution in a more generic way here. Hope it helps.
– In main C thread
– In every thread, or we can say in the body of thread_entrance function
– back to main C thread