[英]Using Main() return value as an argument for a python script
I want my C++ processes to pass their return value to open a python script.我希望我的 C++ 进程传递它们的返回值以打开 python 脚本。
cpp example file cpp 示例文件
int main()
{
//do something
//process is going to finish and is gonna return, for simplicity, 0
//retval = 0, or whatever the return value is going to be
popen("mypython_script.py retval")
return 0;
}
mypython_script.py我的python_script.py
if __name__ == "__main__":
cpp_retval = sys.argv[1]
print(cpp_retval)
I don't know if it's possible for the C++ file to send their return value as described, but this is just the general behaviour I want to achieve.我不知道 C++ 文件是否可以按照描述发送它们的返回值,但这只是我想要实现的一般行为。
I also have control over the parent process of each C++ process: it's another python script which is in charge of opening C++ files and killing their process if needed, so if you have a solution that involves using the parent process to fetch the return value from "cpp example file", that's more than welcome我还可以控制每个 C++ 进程的父进程:它是另一个 python 脚本,负责打开 C++ 文件并在需要时使用父进程来获取解决方案,因此如果您有从父进程获取的解决方案“cpp 示例文件”,非常受欢迎
EDIT: I forgot to add that I cannot ask the parent python process to wait for the C++ program to return something.编辑:我忘了补充一点,我不能要求父 python 进程等待 C++ 程序返回一些东西。 In no way I can have "waits" of any sort in my program.
在我的程序中,我绝不可能有任何形式的“等待”。
You can capture the return value of the C++ program from the parent python script, if you run the C++ program using cpp_retval = subprocess.call(...)
, or cpp_retval = subprocesss.run(...).returncode
( https://docs.python.org/3/library/subprocess.html ). You can capture the return value of the C++ program from the parent python script, if you run the C++ program using
cpp_retval = subprocess.call(...)
, or cpp_retval = subprocesss.run(...).returncode
returncode ( https: //docs.python.org/3/library/subprocess.html )。
Then you can pass that to the other python script.然后您可以将其传递给其他 python 脚本。 So, something like this:
所以,像这样:
cpp_retval = subprocess.call(["my_cpp_program.exe"])
subprocess.call(["my_python_script.py", str(cpp_retval)])
If you want to directly pass the value from the C++ program to the python script you could do it like this:如果您想直接将 C++ 程序中的值传递给 python 脚本,您可以这样做:
#include <cstdlib> // std::system
#include <string>
int main()
{
const int retval = do_stuff();
std::system((std::string("my_python_script.py") + ' ' + std::to_string(retval)).c_str());
return retval;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.