Python 和 C++脚本交互

发布于 2024-10-14 17:50:28 字数 710 浏览 6 评论 0原文

我正在尝试优化两个脚本之间的交互。 我想到的两件事是 c++ 程序不会终止,除非您手动终止它,或者在将其提供给 c++ 之前在 python 中生成所有信息。

问题说明:

脚本的作用: C++程序(不是我做的,我不能很好地用C++编程):接受一个7数字数组并返回一个数字,简单。 Python 脚本(我的,我可以用 python 编程):生成这 7 个数字数组,将它们提供给 C++ 程序,等待答案并将其添加到列表中。然后它生成下一个数组。

从理论上讲,这是可行的。然而,就像现在一样,它会为每次调用打开和关闭 C++ 程序。对于一个阵列来说这是没有问题的,但我正在尝试升级到 25k 阵列,并在未来升级到 6+000000 个阵列。显然,每次打开/关闭它不再可行,特别是因为 C++ 程序首先必须加载 130mb VCD 文件才能运行。

我自己想到的两个选择是首先在 python 中生成所有数组,然后将它们提供给 c++ 程序,然后分析所有结果。但是,我不知道如何使用 6M 数组执行此操作。然而,我返回的结果与我输入的数组的顺序相同并不重要。

我想到的第二个选项是让 c++ 程序在每次调用后不退出。我无法用 C++ 编程,所以我不知道这是否可能,保持它“活动”,这样你就可以有时将数组输入其中并得到答案。

(注意:除了 python 之外,我无法使用其他语言进行编程,并且想使用 python 来完成此项目。由于速度原因,c++ 程序无法转换为 python。)

提前致谢,Max。

I am trying to optimize the interaction between two scripts I have.
Two things I thought of are the c++ program not terminating unless you manually kill it, or generating all info in python before feeding it to c++.

Explanation of the problem:

What the scripts do:
C++ program (not made by me, and I can't program in c++ very well): takes a 7 number array and returns a single number, simple.
Python script (mine, and I can program a bit in python): generates those 7 number arrays, feeds them to the c++ program, waits for an answer and adds it to a list. It then makes the next array.

In theory, this works. However, as it is right now, it opens and closes the c++ program for each call. For one array that is no problem, but I'm trying to upscale to 25k arrays, and in the future to 6+ million arrays. Obviously it is then no longer feasible to open/close it each time, especially since the c++ program first has to load a 130mb VCD file to function.

Two options I thought of myself were to generate all arrays first in python, then feed them to the c++ program and then analyze all results. However, I wouldn't know how to do this with 6M arrays. It is not important however that the results I get back are in the same order as the arrays I feed in.

Second option I thought of was to make the c++ program not quit after each call. I can't program in c++ though so I don't know if this is possible, keeping it 'alive' so you can just feed arrays into it at times and get an answer.

(Note: I cannot program in anything else than python, and want to do this project in python. The c++ program cannot be translated to python for speed reasons.)

Thanks in advance, Max.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

七月上 2024-10-21 17:50:28

首先,迂腐一点,正常使用时没有 C++ 脚本。 C++ 最终编译为机器代码,并且 C++ 程序被正确地称为“程序”而不是“脚本”。

但为了回答你的问题,你确实可以将 C++ 程序设置为保留在内存中,它侦听连接并将响应发送到你的 Python 脚本。您想要研究 Unix IPC,特别是套接字。

另一种方法是将 C++ 程序的功能合并到 Python 脚本中,而完全忘记 C++。

Firstly, just to be pedantic, there are no C++ scripts in normal use. C++ compiles, ultimately, to machine code, and the C++ program is properly referred to as a "program" and not a "script".

But to answer your question, you could indeed set up the C++ program to stay in memory, where it listens for connections and sends responses to your Python script. You'd want to study Unix IPC, particularly sockets.

Another way to approach it would be to incorporate what the C++ program does into your Python script, and forget about C++ altogether.

花开浅夏 2024-10-21 17:50:28

如果没有源代码或Python脚本和C++程序的确切规范,很难提供更多信息,但您可以修改C++代码以重复读取数组从标准输入,然后将结果写入标准输出。

然后,您可以使用 Python 子进程模块 从 Python 脚本启动 C++ 程序并进行通信与它。

请注意,简单地在 C++ 程序的 main() 函数周围包裹一个循环不会很有帮助,因为显然主要问题是程序读取其数据(VCD 的数据)所需的时间。你提到过)。

循环需要严格围绕计算结果的代码 - 这意味着您可能必须以允许重复完成结果计算而不会污染下一个运行的方式将其他所有内容分解出来。

Without the source code or the exact specifications of the Python script and the C++ program, it's difficult to provide more information, but you could modify the C++ code to repeatedly read the array from the standard input and then write the results to standard output.

Then you could use the Python subprocess module to launch the C++ program from your Python script and communicate with it.

Note that simply wrapping a loop around the main() function of the C++ program will not be very helpful, because apparently the main issue is the time the program needs in order to read its data (the VCD that you mentioned).

The loop needs to be strictly around the code that computes the result - which means that you may have to factor everything else out in a way that allows the result computation to be done repeatedly without each run contaminating the next ones.

回忆那么伤 2024-10-21 17:50:28

好的,您最好的做法可能是编写一个 Python 的 C/C++ 扩展,它能够调用执行您想要的计算的 C++ 代码。这并不是非常困难,只需要最少量的 C/C++ 编码即可使其工作。关于扩展 Python 的详细解释可以在 Python 页面 http://docs.python.org 找到/extending/extending.html

实际上,您所做的是将 C++ 程序更改为 Python 进程可以链接到 Python 脚本并从中调用的动态库。

如果您需要一些帮助才能使其正常工作,我相信我们可以为您提供帮助。

Okay, your best course of action is probably to write a C/C++ extension to Python that is able to call the C++ code that does the calculation you want. This is not terribly difficult, it will only require a minimal amount of C/C++ coding to make it work. A good explanation of extending Python can be found on the Python page at http://docs.python.org/extending/extending.html

What you in effect do is change your C++ program to be a dynamic library that the Python process can link in and call from the Python script.

If you need a bit of help getting it to work I'm sure we can help you out.

风柔一江水 2024-10-21 17:50:28

我认为最好的方法是为 python 构建 C++ 扩展模块。
有很多方法可以做到这一点。
如果您有 C++ 源代码,您可以尝试 SWIG
之后,您可以直接在 python 中使用 c++ 函数/对象 - 并通过 python 模块管理它们(此处为处理)。这真的很简单。

I think the best way is to build C++ extension module for python.
There are lot of ways to do it.
If you have c++ sources you can try SWIG
After that you can use c++ functions/object directly inside python - and manage them by python modules (here processing). It is really simple.

长亭外,古道边 2024-10-21 17:50:28

我认为你做错了

脚本的作用:C++ 程序(不是我编写的,我不能很好地用 C++ 编程):接受一个 7 数字数组并返回一个数字,简单。 Python 脚本(我的,我可以用 python 编程):生成这 7 个数字数组,将它们提供给 C++ 程序,等待答案并将其添加到列表中。然后它生成下一个数组。

你有这个吗?

python generate_arrays.py | someC++app | python gather_array.py

这允许您使用盒子上每个 CPU 的每个核心并行运行这三个部分。操作系统确保所有三个同时运行。

如果您仍然没有获得 100% CPU 负载,则必须执行类似的操作。

( python generate_arrays.py --even | someC++app >oneFile ) & ( python generate_arrays.py --odd | someC++app > anotherFile )
python gather_array.py oneFile anotherFile

这将运行 pythongenerate_arrays.py 的两个副本和神奇的 C++ 程序的两个副本。

您必须重写您的generate_arrays.py 程序,以便它采用命令行选项。当选项为 --even 时,您将生成 300 万个数组。当选项为 --odd 时,您将生成其他 300 万个数组。

这个(python | c++)& (python | c++) 应该达到 100% cpu 使用率。

I think you're doing it wrong

What the scripts do: C++ program (not made by me, and I can't program in c++ very well): takes a 7 number array and returns a single number, simple. Python script (mine, and I can program a bit in python): generates those 7 number arrays, feeds them to the c++ program, waits for an answer and adds it to a list. It then makes the next array.

You have this?

python generate_arrays.py | someC++app | python gather_array.py

This allows you to run the three parts in parallel, using every Core of every CPU on the box. The OS makes sure that all three run concurrently.

If you're still not getting 100% CPU Load, you'll have to do something like this.

( python generate_arrays.py --even | someC++app >oneFile ) & ( python generate_arrays.py --odd | someC++app > anotherFile )
python gather_array.py oneFile anotherFile

That will run two copies of python generate_arrays.py and two copies of your magical C++ program.

You'll have to rewrite your generate_arrays.py program so that it takes a command-line option. When the option is --even, you generate 3 million arrays. When the options is --odd you generate the other 3 million arrays.

This (python | c++) & (python | c++) should get to 100% cpu use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文