0

Can Someone please explain to me how I can pass a Variable from embedded Python to my C Program? I've looked everywhere on the web and what I found I did not understand, because I know very little Python.

I tried to create a callback function in C, but I did not understand how its supposed to work.

Now, my main program is in C. There I create a Python Object and in a thread and call a Python Function from a Python Script. This Function produces values and these values I need to pass back to the C program for further use.

Clifford
  • 88,407
  • 13
  • 85
  • 165
user3030653
  • 41
  • 2
  • 9
  • I don't understand your question, can you give an example, and what did you try already? – Iharob Al Asimi Jan 14 '15 at 15:04
  • http://stackoverflow.com/questions/27940739/embedding-python-in-c-and-continuesly-pass-values-from-a-python-loop-to-the-c-pr This was my initial question but I didnt get any answers and just a few views, so I thought Id try to ask a very simple question.. thanks for downvoting. – user3030653 Jan 14 '15 at 15:08

1 Answers1

0

For embedding, I would recommend looking at the docs on the Python website: https://docs.python.org/3.4/extending/embedding.html#pure-embedding

The section that is of interest to you is:

if (pModule != NULL) {
    pFunc = PyObject_GetAttrString(pModule, argv[2]);
    /* pFunc is a new reference */

    if (pFunc && PyCallable_Check(pFunc)) {
        pArgs = PyTuple_New(argc - 3);
        for (i = 0; i < argc - 3; ++i) {
            pValue = PyLong_FromLong(atoi(argv[i + 3]));
            if (!pValue) {
                Py_DECREF(pArgs);
                Py_DECREF(pModule);
                fprintf(stderr, "Cannot convert argument\n");
                return 1;
            }
            /* pValue reference stolen here: */
            PyTuple_SetItem(pArgs, i, pValue);
        }
        pValue = PyObject_CallObject(pFunc, pArgs);
        Py_DECREF(pArgs);
        if (pValue != NULL) {
            printf("Result of call: %ld\n", PyLong_AsLong(pValue));
            Py_DECREF(pValue);
        }
        else {
            Py_DECREF(pFunc);
            Py_DECREF(pModule);
            PyErr_Print();
            fprintf(stderr,"Call failed\n");
            return 1;
        }
    }
    else {
        if (PyErr_Occurred())
            PyErr_Print();
        fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
    }
    Py_XDECREF(pFunc);
    Py_DECREF(pModule);
}

specifically this line:

pValue = PyObject_CallObject(pFunc, pArgs);

this is calling a python function (callable python object), pFunc, with arguments pArgs, and returning a python object pValue.

I would suggest reading through that entire page to get a better understanding of embedding python. Also, since you say you know very little python, I would suggest getting more familiar with the language and how different it is from c/c++. You'll need to know more about how python works before you can be effective with embedding it.

Edit: If you need to share memory between your c/c++ code and python code (Running in separate thread), I don't believe you can share memory directly, and least not in the way you would normally do with just c/c++. However, you can create a memory mapped file to achieve the same effect with about the same performance. Doing so is going to be platform dependent, and I don't have any experience with doing so, but here is a link that should help: http://www.codeproject.com/Articles/11843/Embedding-Python-in-C-C-Part-II.

Basically, you just create the mmap in c (this is the platform dependent part) and create an mmap to the same file in your python code, then write/read to/from the file descriptor in each of your threads.

Michael B.
  • 331
  • 1
  • 5
  • I've been effectivly embedding Python into my Program and I know how to get a return value. Its not the Issue. Once more here is my initial question: http://stackoverflow.com/questions/27940739/embedding-python-in-c-and-continuesly-pass-values-from-a-python-loop-to-the-c-pr I can only return a value once a function is finished. What I need is to update a value continuesly while the Python function is runnning. – user3030653 Jan 15 '15 at 08:22
  • Ah, I see. I've updated my answer with some more info. Hopefully that helps. – Michael B. Jan 16 '15 at 17:27