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
-- Ulf Worsøe Mosek ApS
ulf.worsoe@mosek.com www.mosek.com

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
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Feb 24 2013)
Python Projects, Consulting and Support ... http://www.egenix.com/ mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
participants (3)
-
M.-A. Lemburg
-
Ulf Worsoe
-
Тушев Сергей