Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'static': 0.04; 'function:': 0.09; 'null,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:while': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; '"""write': 0.16; 'extern': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'invoking': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'seconds.': 0.16; 'size)': 0.16; 'size):': 0.16; 'size;': 0.16; 'statement.': 0.16; 'subject:python': 0.16; 'module': 0.19; 'stefan': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'external': 0.29; '----': 0.29; 'quickly': 0.29; 'skip:( 20': 0.30; 'code': 0.31; 'fine,': 0.31; 'void': 0.31; 'stuff': 0.32; '-----': 0.33; 'plain': 0.33; 'subject:from': 0.34; 'problem': 0.35; 'advice': 0.35; 'something': 0.35; 'received:84': 0.35; 'but': 0.35; 'next': 0.36; 'method': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'complete': 0.62; 'here': 0.66; 'below.': 0.71; 'hassle': 0.84; 'received:arcor-ip.net': 0.84; 'received:pools .arcor-ip.net': 0.84; 'yourself,': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: how avoid delay while returning from C-python api? Date: Wed, 28 May 2014 21:00:54 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dslb-084-056-010-170.pools.arcor-ip.net User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401303676 news.xs4all.nl 2835 [2001:888:2000:d::a6]:36764 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72177 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 > > 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, &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