title: Pass Variable from Python to C
body: <p>I have Python Script embedded in C which I run in a thread. I need to pass the variable 'a' from the Python-Class 'Detect Motion' to my C program continuously. (Not as a return value)
  I know I could do this with a fifo or something like that, but is there a way to pass it directly to C, maybe by calling a C function?</p>
  
  <p>C:</p>
  
  <pre><code>#include &lt;Python.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  
  #include &lt;pthread.h&gt;
  pthread_t mythread;
  void *ThreadProc();
  
  PyObject *pName, *pModule, *pDict, *pFunc, *pFunc2;
  
  int main(int argc, char *argv[])
  {
  
      py_callback = PyCFunction_New(&amp;callback_descr, NULL);
  
  
      char *script = "motion";
      char *functionUse = "get_values";
  
      Py_Initialize();
      pName = PyString_FromString(script);
      pModule = PyImport_Import(pName);
      // pDict and pFunc are borrowed references 
      pDict = PyModule_GetDict(pModule);
      pFunc = PyDict_GetItemString(pDict, functionUse);
  
      // POSIX code
      pthread_create( &amp;mythread, NULL, ThreadProc, NULL);
  
      // Random testing code
      for(int i = 0; i &lt; 10; i++)
      {
          printf("Printed from the main thread.\n");
          sleep(1);
      }
  
      printf("Main Thread waiting for My Thread to complete...\n");
  
      // Join and wait for the created thread to complete...
      // POSIX code
      pthread_join(mythread, NULL);
  
      printf("Main thread finished gracefully.\n");
  
      return 0;
  }
  
  void *ThreadProc()
  {
      if (PyCallable_Check(pFunc)) 
      {
          PyObject_CallObject(pFunc, NULL);
      }
      else {
          PyErr_Print();
      }
  
      // Clean up
      Py_DECREF(pModule);
      Py_DECREF(pName);
  
      Py_Finalize();
      printf("My thread is finishing...\n");
  }
  </code></pre>
  
  <p>Python:</p>
  
  <pre><code>import numpy as np
  import picamera
  import picamera.array
  
  class DetectMotion(picamera.array.PiMotionAnalysis):
      def analyse(self, a):
              a = np.sqrt(
              np.square(a['x'].astype(np.float)) +
              np.square(a['y'].astype(np.float))
              ).clip(0, 255).astype(np.uint8)
              # If there're more than 10 vectors with a magnitude greater
              # than 60, then say we've detected motion
              print a 
              if (a &gt; 60).sum() &gt; 10:
                  print 'Motion detected!'
  
  def get_values():
      with picamera.PiCamera() as camera:
          with DetectMotion(camera) as output:
              camera.resolution = (640, 480)
              camera.start_preview()
              camera.start_recording(
                  '/dev/null', format='h264', motion_output=output)
              camera.wait_recording(10)
              camera.stop_recording()
              camera.stop_preview()
  </code></pre>