【问题标题】:Pause/Resume embedded python interpreter暂停/恢复嵌入式 python 解释器
【发布时间】:2023-04-05 21:56:01
【问题描述】:

是否有可能在我需要的地方暂停/恢复嵌入式 python 解释器的工作?例如:

C++伪代码部分:

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脚本伪代码部分:

#... 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

这可能与 Python/C API 相关吗?

谢谢

【问题讨论】:

    标签: python-c-api python


    【解决方案1】:

    我最近也在寻找一种手动“驱动”嵌入式语言的方法,我遇到了这个问题,并想我会分享一个潜在的解决方法。

    我会通过套接字或某种消息传递系统来实现“阻塞”行为。与其真正停止整个 python 解释器,不如让它在等待 C++ 进行评估时阻塞。

    C++ 将启动嵌入式运行时,然后进入某种循环,等待 python “发出信号”它已准备好。例如,C++ 监听端口 5000,启动 python,python 工作,连接到 localhost 上的端口 5000,然后 C++ 看到连接并从 python 获取数据,对其执行工作,然后通过套接字将数据重新洗牌到 python,然后python接收数据并离开阻塞循环。

    我仍然需要一种完全暂停虚拟运行时的方法,但在您的情况下,您可以使用套接字和一些使用套接字协调两段代码的阻塞行为来实现相同的目的。

    祝你好运:)

    编辑:您可以挂钩此答案中使用的“注入”功能以完全停止 python。只需修改它以注入一个等待循环。

    Stopping embedded Python

    【讨论】:

      猜你喜欢
      • 2014-01-27
      • 1970-01-01
      • 2012-01-26
      • 2019-07-28
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多