top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to avoid delay while returning from C-python API?

+1 vote
444 views

I have C-Python API like below. It works fine, but the problem is while invoking this method from python script say

#cat script.py

offset=0
size=4
write_object(offset,size)

This calls write_this_c() C api and returns quickly to next printf statement. But the return call (Py_RETURN_NONE) takes something like 4-6 seconds.

static
PyMethodDef xyz_methods[] = {
{"write_object", write_object, METH_VARARGS,"write some stuff "},
{NULL, NULL, 0, NULL}
};
....

static PyObject *
write_object(PyObject *self, PyObject *args)
{
 int offset, size;
 if (!PyArg_ParseTuple(args,"ii", &offset, 

 printf("before call");
 write_this_c(offset, size);
 printf("after call");
 Py_RETURN_NONE; ##delay happens here
}

How to avoid this delay time?

posted May 28, 2014 by Jagan Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

It can't be as simple as the Py_RETURN_NONE taking 4-6 seconds: hundreds of built-in functions in Python are coded exactly this way, and they don't have a delay on return. Something else is going on.

How did you determine that it was the return that was taking the time?

answer May 28, 2014 by Garima Jain
Just found out there is core-dump "segmentation fault (core dumped) python" ..Probably fixing core dump should resolve the issue. Thanks...
Similar Questions
0 votes

I'm currently writing a C extension module for python using the "raw" C-API. I would like to be able to define "nested classes" like in the following python code

class A: 
   class B: 
     def __init__(self): 
     self.i = 2 
     def __init__(self): 

     self.b = A.B() 
     a = A() 
     b = A.B() 
     print(a.b.i) 
print(b.i) 

How can I create such nested class with the C-API?

+1 vote

For example gcd(3, -7) returns -1, which means that a co-prime test that would work in many other languages 'if gcd(x, y) == 1' will fail in Python for negative y.

And, of course, since -|x| is less than |x|, returning -|x| rather than |x| is not returning the greatest common divisor of x and y when y is negative.

...