如何使用 python .Net vs ZeroMQ 或其他将 Python 包公开给 C#

How to expose a Python package to C# using python .Net vs ZeroMQ or other

我正在开发一个用 Python3 编写的应用程序,它由一个 Python library/package(包含核心功能)和一个 Python 应用程序组成将提供一个 cli shell 并处理用户命令。

此外,Python 包中包含的功能必须公开给用 C# 编写的现有 gui 应用程序(使用 Microsoft .Net 框架)。

我对如何做到这一点进行了大量研究,并提出了一些可能的解决方案。

  1. 使用 Python.Net 在 C# 应用程序中实施 Python 脚本,导入我的 python 包并调用所需的 methods/attributes。我自己还不能让它在 monodevelop 上工作,但这似乎是一个流行的选择,尽管关于我的用例的文档不多。
  2. 使用 CFFI 将我的 Python 库作为 DLL 嵌入。这个选项似乎不需要很多工作,但很难看出我将如何维护我的 interfaces/what 我正在向使用 C# 中的 DLL 的人公开。许多与我的用例有关的文档似乎也不支持此选项。
  3. 创建一个小型 Python 应用程序,它导入我的 python 包并通过 ZeroMQ or gRPC 公开其功能。这似乎是具有充足文档的最灵活的选项,但是我担心延迟,因为最终该工具用于硬件控制。

请注意,我不太精通 C#,将在 linux 中完成大部分开发工作。

我真的很想得到关于哪个选项将在我的库的干净界面和低 latency/good 性能(强调后者)之间提供最佳平衡的反馈。

你说你的 python 应用程序有一个 cli,所以另一个可能的选择是让你的 C# 应用程序通过命令行与你的 python 应用程序交互。

您需要通过命令行参数公开 python 功能(无论如何您可能已经这样做了)并且您的 python 应用程序需要能够 return 结果作为 json 数据,这可能是从 C# 使用它的最简单方法。

这完全取决于您的 C# gui 和 python 应用程序之间的交互需要有多复杂。

目标~ 10 [ms]下的延迟SuT-稳定性?

感谢您添加了有关相当广泛的延迟上限的详细信息~ 10 .. 100 [ms]
+

…this is actually replacing something that was previously implemented in C. The idea is that if the interface layer of the library and the cli are implemented in Python it would be easier for users to build off the core functionality for their use case. Some of the more demanding control loops may have to be implemented as a static C library or rust library which we would call in with python. In any case the top layer is still implemented in Python which will have to interface with C#
( = the most important takeaway from here
The need to understand both
The Costs of wished-to-have ease of user-extensions & refactoring the architecture
+
Who pays these Costs
)


在我们开始搜索 解决方案 之前:

为了安全和专业地完成这项工作,您很可能会喜欢 this,不要重复不知情决定的常见错误,其中一般评论来自大量的第一手制作经验 ~ 80 [us]

下具有控制回路的系统

地图 你控制系统的 - 内部 生态系统(资源) & exo-system(与外部世界的互动)

接下来是架构:

如果没有对玩具的应有了解,就没有人能决定“适可而止的建筑”。

了解 latency-motivated device requires us to first know ( read + test + benchmark also it's jitter/wander envelope(s) under (over)-loaded conditions of the System-under-Test ). Not knowing this will lead to but a blind & facts unsupported belief, our SuT will never ever headbang into the wall of reality 中的设备情况,这将证明自己是错误的,通常是在最不愉快的时刻。

不可挽回的错误和不良做法,因为到目前为止所有的应计成本都已经烧掉了...

了解和测试是勾画架构之前的核心步骤 - 细节很重要(ref. How much does one loose in h2d/d2h latencies [us]? - why these principal costs are so weakly reported? Does that mean those costs do not exist? No. They do exist and your control-loops will pay 'em each and every time... so better know about all such hidden costs, paid in the TimeDOMAIN, well beforehand ...before Architecture 被设计和起草。)


不要犹豫去分布式(合理 :

向 NASA 阿波罗任务设计学习
- 它分布很深

- 适当的工程帮助到达月球
- 它拯救了两个民族自豪感和这些第一个也是迄今为止唯一的外星人的生活
归功于Ms.Margaret HAMILTON's wisdom in defining her design rules and her changing the minds of the proper engineering of the many control-loops-systems'协调策略 )

要么 ZeroMQ ( zmq, being a mature, composable, well scaling, architecture of principally distributed many-to-many behaviours, developed atop a set of a few Trivial Scalable Formal Communication Pattern Archetypes ) or it's Marting SUSTRIK co-fathered younger and light-weighted sister, , may help one a lot to compose a smart macro-system, where individual component's strengths (or monopolies of having none substitute for) may get interconnected into a still-within-latency-thresholds stable, priority-aware macro-system,原则上不能(或不想,由于其他一些原因 - 成本经济,上市时间,法律限制是手头上的第一个) 设计一个单一的 一体化 系统。

虽然乍一看这听起来可能使问题复杂化,但人们可能很快就会意识到,它确实起到了相反的作用:

  • 在另一个重新发明轮子上不燃烧燃料(是的,投资者的钱)(s ...)
  • 最常使用经过行业验证的工具提高可靠性,当然,如果使用得当……
  • 性能扩展 可能是一个很好的副作用,而不是因为太晚重构 恐慌 噩梦

没有提及此类工具的独立发展及其进一步扩展带来的积极好处。

我的系统处于类似的困境 - #C 暂时不适合我(封闭源应用程序的依赖性对于我们的成功来说即使不是致命的也太昂贵了)。

  • CLI: 称为 远程键盘 是分离第一个 python 的确切示例,其中远程可以被解读为跨大西洋键盘
  • ML: 是镇上控制最差的延迟元素,因此需要融合
  • core-App: 使用行业标准 DLL 扩展到 系统知道(只有剥离的核心逻辑保留在原地,其他一切都分布式,以最小化所有控制循环的延迟并让处理不同级别的优先级)
  • 非阻塞附加组件:从核心应用程序卸载
  • core-App-(1+N)-Hot-Standby-Shading: 被引入到原本单片的C/S exo-system

这里是否需要添加更多内容以实现分布式和独立于原始供应商锁定?

选择了汗水、泪水和鲜血 - 在 ZeroMQ 成熟的日子里开始 v2.x,我没有后悔这样做 并且不能想象一下在没有这样做的情况下满足以上所有条件。