简体   繁体   中英

Pause/Resume embedded python interpreter

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
while true:
       #... 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

I too have recently been searching for a way to manually "drive" an embedded language and I came across this question and figured I'd share a potential workaround.

I would implement the "blocking" behavior either through a socket, or some kind of messaging system. Instead of actually stopping the whole python interpreter, just have it block when it is waiting for C++ to do it's evaluations.

C++ will start the embedded runtime, then enter a loop of some sort that waits for python to "throw the signal" that it's ready. For instance C++ listens on port 5000, starts python, python does work, connects to port 5000 on localhost, then C++ sees the connection and grabs the data from python, performs work on it, then shuffles the data back over the socket to python, where python then receives the data and leaves the blocking loop.

I still need a way to fully pause the virtual runtime, but in your case you could achieve the same thing with a socket and some blocking behavior that uses the socket to coordinate the two pieces of code.

Good luck :)

EDIT: You may be able to hook this "injection" functionality used in this answer to completely stop python. Just modify it to inject a wait-loop perhaps.

Stopping embedded Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM