Embedded Python - Blocking Python Function

Discussion in 'Python' started by andy@britishideas.com, Nov 14, 2007.

  1. Guest

    I embed multiple interpreters. I create the interpreter and modules in
    the primary thread of my application:

    PyEval_AcquireLock();
    thread = Py_NewInterpreter();
    PyThreadState_Swap(thread);

    ....initialize modules, etc....

    PyThreadState_Swap(maininterpreter);
    PyEval_ReleaseLock();

    Then I create a C thread called "main" which calls a function called
    "Main" in the Python interpreter:

    PyEval_AcquireLock();
    PyThreadState_Swap(thread);
    moduledictionary = PyModule_GetDict(pmodule);
    PyObject_CallObject(PyDict_GetItemString(moduledictionary, "Main"),
    NULL);
    PyThreadState_Swap(maininterpreter);
    PyEval_ReleaseLock();

    The problem is that the function "Main" in the Python script can take
    up to 60 seconds to execute. How can I terminate this thread (and
    therefore the Main function in python) cleanly from the primary thread
    of my application?

    If I try to call Py_EndInterpreter(thread); then I get a runtime error
    (presumably because the Main function is still executing).

    thanks, Andy
    , Nov 14, 2007
    #1
    1. Advertising

  2. En Wed, 14 Nov 2007 20:02:42 -0300, <> escribió:

    > The problem is that the function "Main" in the Python script can take
    > up to 60 seconds to execute. How can I terminate this thread (and
    > therefore the Main function in python) cleanly from the primary thread
    > of my application?


    Not forcibly - you need some cooperation from the Main function. Maybe
    setting a global variable that Main checks periodically.

    --
    Gabriel Genellina
    Gabriel Genellina, Nov 14, 2007
    #2
    1. Advertising

  3. Guest

    On Nov 14, 4:20 pm, "Gabriel Genellina" <>
    wrote:
    > Not forcibly - you need some cooperation from the Main function. Maybe
    > setting a global variable that Main checks periodically.


    Thanks. I'll give that a try!

    Andy
    , Nov 15, 2007
    #3
  4. Guest

    On Nov 15, 9:43 am, wrote:
    > On Nov 14, 4:20 pm, "Gabriel Genellina" <>
    > wrote:
    >
    > > Not forcibly - you need some cooperation from the Main function. Maybe
    > > setting a global variable that Main checks periodically.

    >
    > Thanks. I'll give that a try!
    >
    > Andy


    It works but the problem is that the script will be written by the end
    user. If they make a mistake and the cancel flag isn't perodically
    checked then it seems I have no way of cleanly ending the interpreter.
    If I wait for a specific time after requesting the Main function stop
    I need to be able to kill the interpreter without a runtime error. Any
    ideas?

    Andy
    , Nov 15, 2007
    #4
  5. En Thu, 15 Nov 2007 16:18:45 -0300, <> escribió:

    > On Nov 15, 9:43 am, wrote:
    >> On Nov 14, 4:20 pm, "Gabriel Genellina" <>
    >> wrote:
    >>
    >> > Not forcibly - you need some cooperation from the Main function. Maybe
    >> > setting a global variable that Main checks periodically.

    >
    > It works but the problem is that the script will be written by the end
    > user. If they make a mistake and the cancel flag isn't perodically
    > checked then it seems I have no way of cleanly ending the interpreter.
    > If I wait for a specific time after requesting the Main function stop
    > I need to be able to kill the interpreter without a runtime error. Any
    > ideas?


    You could use PyThreadState_SetAsyncExc - it's supposed to raise an
    exception in another thread. There is a Cookbook recipe using it here
    <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960>
    I've never actually used it, but I want to try it some day, so please
    report back your findings if you decide to use this function.

    --
    Gabriel Genellina
    Gabriel Genellina, Nov 16, 2007
    #5
  6. Guest

    On Nov 15, 5:03 pm, "Gabriel Genellina" <>
    wrote:
    > En Thu, 15 Nov 2007 16:18:45 -0300, <> escribió:
    >
    > > On Nov 15, 9:43 am, wrote:
    > >> On Nov 14, 4:20 pm, "Gabriel Genellina" <>
    > >> wrote:

    >
    > >> > Not forcibly - you need some cooperation from the Main function. Maybe
    > >> > setting a global variable that Main checks periodically.

    >
    > > It works but the problem is that the script will be written by the end
    > > user. If they make a mistake and the cancel flag isn't perodically
    > > checked then it seems I have no way of cleanly ending the interpreter.
    > > If I wait for a specific time after requesting the Main function stop
    > > I need to be able to kill the interpreter without a runtime error. Any
    > > ideas?

    >
    > You could use PyThreadState_SetAsyncExc - it's supposed to raise an
    > exception in another thread. There is a Cookbook recipe using it here
    > <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960>
    > I've never actually used it, but I want to try it some day, so please
    > report back your findings if you decide to use this function.
    >
    > --
    > Gabriel Genellina



    It seems this is the function I need, however the following gave an
    access violation:

    PyEval_AcquireLock();
    PyThreadState_Swap(thread);

    // stop interpreter by sending system exit exception to it's thread
    PyThreadState_SetAsyncExc(thread->thread_id, PyExc_SystemExit);

    PyThreadState_Swap(maininterpreter);
    PyEval_ReleaseLock();

    Andy
    , Nov 16, 2007
    #6
    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. Hendra Gunawan
    Replies:
    1
    Views:
    12,415
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    912
    Valentin Tihomirov
    Oct 12, 2003
  3. nukleus
    Replies:
    14
    Views:
    816
    Chris Uppal
    Jan 22, 2007
  4. Christian
    Replies:
    5
    Views:
    720
    Esmond Pitt
    Dec 2, 2007
  5. Serge Savoie
    Replies:
    4
    Views:
    255
    Serge Savoie
    Oct 1, 2008
Loading...

Share This Page