[capi-sig] Pause/Resume embedded python interpreter

Ulf Worsoe ulf.worsoe at mosek.com
Fri Feb 22 19:45:25 CET 2013


Hi,

You could implement the stop() function in C using the Python/C API, and
make sure not to release the global interpreter lock in the function. Your
C pseudo code would be something like this:

PyObject * stop_function(PyObject * args)
{
// Dont release the global interpreter lock during this function call
//...fetch the current stack trace and access the environment of the caller
of this function
//... do some work ...
//... write new value to some other variables in python-script
}

main()
{
script ="python_script.py";
...
env = make_python_execution_environment();
env.add_function_to_environment(stop_function, "stop");

  RunScript(script,env);//-- python script using the environment containing
"stop"
 }

If I remember correctly, Python relies on the native code to hand back the
global interpreter lock, so if you don't do that, you will effectively have
paused python during the call to stop_function.

/ulfw
On Fri, Feb 22, 2013 at 1:59 PM, Тушев Сергей <sergy5 at mail.ru> wrote:

>
> Hello!
> Tell me please, Is there any possibility to pause/resume the work of
> embedded python interpreter in place, where I need? For example:
> C++ pseudo-code part:
> main()
> {
>     script ="python_script.py";
>     ...
>     RunScript(script);//-- python script runs till the command 'stop'
>     while(true){
>         //... read values from some variables in python-script
>         //... do some work ...
>         //... write new value to some other variables in python-script
>         ResumeScript(script);//-- python script resumes it's work where//
> it was stopped. Not from begin!    }
>     ...
> }
> Python script pseudo-code part:
> #... do some init-work
> whiletrue:
>     #... do some work
>     stop # - here script stops and C++-function RunScript()
>          # returns control to C++-part
>     #... After calling C++-function ResumeScript
>     # the work continues from this line
> Is this possible to do with Python/C API?
> Thanks
> _______________________________________________
> capi-sig mailing list
> capi-sig at python.org
> http://mail.python.org/mailman/listinfo/capi-sig
>



-- 
Ulf Worsøe
Mosek ApS

ulf.worsoe at mosek.com
www.mosek.com


More information about the capi-sig mailing list