嵌入 C 时维护 Python 对象

发布于 2024-09-26 20:53:13 字数 361 浏览 0 评论 0原文

由于控制器上的重构/返工,我不得不在 C 应用程序中嵌入 Python 解释器。我现在可以调用 python 函数并将对象传递/获取到 Python 中。

python 代码是机器人的控制器(当前是模拟的),现在需要使用一些 C 代码来进行比较。

以前,Python 代码创建对象、读取传感器、运行控制代码并将输出写入电机。除了控制代码之外的所有这些现在都需要在 C 中完成。我遇到的问题是在 init 函数(在 python 中)中创建的对象,当我运行控制代码时,这些对象不再存在。

解决这个问题的最佳方法是什么?我的想法是从 init 函数返回控制器,并将对它们的引用存储在 C 中,每次调用时将引用传递给控制器​​。

感谢您的任何帮助。

Due to refactoring/reworking on a controller I've had to embed a Python Interpreter inside a C application. I can now call python functions and pass/get Objects into Python fine.

The python code is a controller for a robot (currently simulated), this now needs make use of some C code for comparisons I'm making.

Previously the Python code created objects, read sensors, ran control code and wrote the outputs to motors. All of this except the control code now needs to be done in C. The problem I have is that Objects which are created in an init function (in python) which, when I come to run the control code no longer exist.

What is the best way to solve this? My idea was to return the controllers from the init function and store references to them in the C, passing the reference to the controller each time it is called.

Thanks for any help.

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

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

发布评论

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

评论(1

空‖城人不在 2024-10-03 20:53:13

这可能不是您想要的答案,但除了在 C 应用程序中嵌入解释器之外,还有其他使用 C 和 Python 的方法。

也就是说,你为什么不做相反的事情呢?为 Python 创建 C 库?您可以在 Python 中控制应用程序的总体流程(这要舒服得多),并在您认为合适的时候调用 C 代码。

再说一次,我并没有真正解决你的实际问题,所以请随意忽略我。

This may not be the answer you want, but there are ways of working with C and Python other than embedding an interpreter inside a C application.

Namely, why don't you do the opposite? Create C libraries for Python? You can control the general flow of your application in Python, which is much more comfortable, and call C code whenever you see fit.

Again, I'm not really addressing your actual question, so feel free to ignore me.

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