Python C API

Discussion in 'Python' started by googler.1.webmaster@spamgourmet.com, Dec 5, 2008.

  1. Guest

    Hi :)

    I have a main() function of my app which intializes the Python
    Interpreter and some other stuff. When I am finished I call:

    PyGILState state = PyGILState_Ensure()
    //call PyRun_String()
    PyGILStateRelease(state);

    The first question is, I found out the API contains other commands lik
    PyEval_AcquireLock(). I don't really understand if I have to use them
    too, could anyone explain? Thanks.

    Okay, back to topic. In PyRun_String() I call a wrapped function. This
    wrapped function does some internal calculations and takes a pointer
    to another function which is called in another thread while it
    calculates the stuff.

    void MyProgressbar(Real p, void* hook) // this function wil be called
    in another thread
    {
    PyGILState_STATE gilstate = PyGILState_Ensure();

    PyObject* func = (PyObject*)hook;
    //do some python stuff

    PyGILState_Release(gilstate)
    }

    PyObject *pyMyFunction(PyObject *pSelf, PyObject *args, PyObject
    *keywords)
    {
    static char *kwlist[] = {"hook", NULL};

    PyObject *hook=NULL;

    if (!PyArg_ParseTupleAndKeywords(args, keywords, "O!", kwlist,
    &PyFunction_Type, &hook))
    return NULL;

    LONG ok = MyFunction(myprogress, hook); //hook is a pointer which
    is passed to the threaded function.

    Py_RETURN_INT(ok);
    }

    I want to do the same in Python. I want to pass a reference of a
    function to the function which is called from the other thread. But it
    stops (not crash) here: PyGILState_STATE gilstate = PyGILState_Ensure
    ();



    What can I do? Thank you very much.
    , Dec 5, 2008
    #1
    1. Advertising

  2. wrote:
    > The first question is, I found out the API contains other commands lik
    > PyEval_AcquireLock(). I don't really understand if I have to use them
    > too, could anyone explain? Thanks.


    That's unrelated. The GIL is special in that it has its own handling functions.


    > void MyProgressbar(Real p, void* hook) // this function wil be called
    > in another thread
    > {
    > PyGILState_STATE gilstate = PyGILState_Ensure();
    >
    > PyObject* func = (PyObject*)hook;
    > //do some python stuff
    >
    > PyGILState_Release(gilstate)
    > }
    >
    > PyObject *pyMyFunction(PyObject *pSelf, PyObject *args, PyObject
    > *keywords)
    > {
    > static char *kwlist[] = {"hook", NULL};
    >
    > PyObject *hook=NULL;
    >
    > if (!PyArg_ParseTupleAndKeywords(args, keywords, "O!", kwlist,
    > &PyFunction_Type, &hook))
    > return NULL;


    You have to release the GIL around the following function call, so that the
    thread above can acuire it while you wait for the function to return in
    this thread here (which I assume you do).

    > LONG ok = MyFunction(myprogress, hook); //hook is a pointer which
    > is passed to the threaded function.


    Stefan
    Stefan Behnel, Dec 5, 2008
    #2
    1. Advertising

  3. Guest

    Hi!

    thats a very interesting point and good to know. I have to release
    the GIL but how do I do?
    In this case i need PyEval_AcquireLock and PyEval_ReleaseLock?


    Thanks.
    , Dec 5, 2008
    #3
  4. Guest

    Hi!

    Any ideas? Thanks, :)
    , Dec 5, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shlomo Anglister
    Replies:
    1
    Views:
    399
    Default User
    Aug 2, 2004
  2. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    361
    Praveen, Tayal (IE10)
    Mar 17, 2005
  3. John123

    Profiling API or Membership API

    John123, Oct 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    John123
    Oct 20, 2006
  4. George2

    platform specific API or C standard API

    George2, Nov 12, 2007, in forum: C Programming
    Replies:
    13
    Views:
    731
    Tor Rustad
    Nov 13, 2007
  5. Timothy Grant
    Replies:
    5
    Views:
    405
    Timothy Grant
    Aug 6, 2008
Loading...

Share This Page