Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #72177 > unrolled thread
| Started by | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| First post | 2014-05-28 21:00 +0200 |
| Last post | 2014-05-28 21:00 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: how avoid delay while returning from C-python api? Stefan Behnel <stefan_ml@behnel.de> - 2014-05-28 21:00 +0200
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2014-05-28 21:00 +0200 |
| Subject | Re: how avoid delay while returning from C-python api? |
| Message-ID | <mailman.10414.1401303676.18130.python-list@python.org> |
Lakshmipathi.G, 28.05.2014 12:22:
> 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
> <snip>
> offset=0
> size=4
> write_object(offset,size)
> </snip>
>
>
> 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, &size))
> Py_RETURN_NONE;
>
> printf("before call");
> write_this_c(offset, size);
> printf("after call");
> Py_RETURN_NONE; ##delay happens here
> }
>
> How to avoid this delay time? Thanks for any help/pointers!
You already found the problem yourself, so let me just give you a general
advice that you can avoid a lot of the hassle of writing code like the
above and struggling to get it right by writing it in Cython instead of
plain C. Here's a complete example of the above code in Cython, including
the module setup code etc., but without the bugs:
# external declarations:
cdef extern from "someheader.h":
void write_this_c(int offset, int size)
# your module function:
def write_object(int offset, int size):
"""write some stuff"""
print("before call")
write_this_c(offset, size)
print("after call")
That's it.
Stefan
Back to top | Article view | comp.lang.python
csiph-web