Pause/Resume embedded python interpreter

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

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@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@python.org http://mail.python.org/mailman/listinfo/capi-sig

I think you're better off with writing the C code you want to run as function that you call from within Python.
The function will have the global interpreter lock (GIL) acquired during the call, so other Python threads cannot run, effectively pausing Python.
On 22.02.2013 13:59, Тушев Сергей 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@python.org http://mail.python.org/mailman/listinfo/capi-sig
participants (3)
-
M.-A. Lemburg
-
Ulf Worsoe
-
Тушев Сергей