1

I'm trying to implement aprecision index in a Turbodecode algorythm written in C. After finding a way to do that i'm trying to embedd a python function in order tu use matplotlib and plot a graph showing the precision of the decoder.

Problem is trying embedding stops at PYImport_Import() without going anyfurther and without giving any error message.

my python file is called plot.py and inside it is a simple:

import matplotlib.pyplot as plt

def show_plt(max_x, y):
    print("enter")
    fig, ax1 = plt.subplots()
    x_array = range(0,max_x+1)
    ax1.plot(x_array, y)
    plt.show()

while my C part of code where i'm trying to call this python function is written like this:

#include <python2.7/Python.h>

...

if (decision)
    {
        printf("here1\n");
        Py_Initialize();
        //PyRun_SimpleString("import sys; sys.path.append('.')");
        decoded = malloc(packet_length * sizeof *decoded);
        double est[packet_length];
        PyObject *tuple = PyList_New(packet_length);
        for (int i = 0; i < packet_length; ++i)
        {
            double one = app[1][i] + extrinsic[1][i];
            double zero = app[0][i] + extrinsic[0][i];
            double delta = abs(one + zero);
            if (delta > 150)//THRESHHOLD TO DEFINE
                delta = 150;
            est[i] = (150 - delta) / 150;
            PyList_Append(tuple,PyFloat_FromDouble(est[i]));
            decoded[i] = one > zero;
        }
        printf("here2\n");
        PyObject *myModuleString = PyString_FromString((char *)"plot");    
        printf("here3\n");
        PyObject *myModule = PyImport_Import(myModuleString);
        printf("here4\n");
        PyObject *myFunction = PyObject_GetAttrString(myModule, (char *)"show_plt");
        printf("here5\n");
        PyObject *args = PyTuple_Pack(2, packet_length, tuple);
        printf("here6\n");
        PyObject_CallObject(myFunction, args);
        printf("here7\n");
    }

note that code stops after printf("here3") without errors.

I'm not familiar with calling python functions in C so any help would be most appreciated.

  • What does “stops […] without errors” mean? – Davis Herring Jan 31 '20 at 20:39
  • Considering it is used inside a GNUradio project it simply stops executing the block where the code is, printing a general ">>Done (return -11) that is the most general error ever in gnuradio simpli for not being able to complete the cycle. However compiler doesn't throw any runtime error. – Massimo Isonni Feb 03 '20 at 09:22
  • 2
    The -11 sounds like a segmentation fault (as reported by `subprocess`); you’ll want to attach a debugger. – Davis Herring Feb 03 '20 at 14:15

1 Answers1

0

I encountered this issue when I imported matplotlib.pyplot from outside of the main thread--at least if done through import of another module ("plot" in your case). Moving the import to the main thread fixed the hang for me.