如何在 VB.Net 和 Python 之间创建应用程序编程接口 (API)?

How to create an Application Programming Interface (API) between VB.Net and Python?

我在 VB.Net 中有一个 GUI,它需要与在 Python 中编写的算法进行交互,全部用于 Windows 10 Professional 上的 运行。 IDE 是 Visual Studio 2017.

VB.Net GUI 和 Python 如何通信? 数据需要来回传递。 GUI 将接受用户命令,然后执行命令Python。 Python 将有需要与 GUI 通信的事件。

为了阐明架构,GUI 应用程序和 Python 应用程序是两个不同的程序 运行 同时运行。

GUI 有两个主要功能:1) 获取用户命令并告诉 Python 执行它们。 2) Monitor/poll Python 用于数据或事件的应用程序,并通过 GUI 控件(文本框等)更新(告诉)用户。

Python主要做两件事:1)运行s 连续控制算法,受GUI管理。 2) 向 GUI 报告数据和事件。

TCP 套接字是 100% 可靠的,但我必须编写和调试专有的消息传递方案,我已经做了很多并且很有趣,但我想也许我可以在更少的时间内完成同样的事情一些其他同样可靠的方法...例如 COM,如下所示。

异步使用示例场景:

1:

Python 应用程序正在 运行 运行一个机器人进程。同时,用户按下 GUI 命令以查看最新指标:GUI 的 VB.NET 在 Python 中调用一个 COM 函数,其中 returns 一个指标数据数组。

2:

Python app 运行ning 机器人进程完成了一个步骤,现在需要手动操作。 Python 应用程序在 VB.NET 中调用 COM 函数并传递一个需要手动操作的参数。

MSDN Python 脚本集成

这是我建议您阅读的内容,它是 MSDN 上关于从 C# 到 Pyhton 通信的指南

基本上调用 python 脚本并读取输出。

https://code.msdn.microsoft.com/windowsdesktop/C-and-Python-interprocess-171378ee


铁Python

IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .NET Framework. IronPython can use the .NET Framework and Python libraries, and other .NET languages can use Python code just as easily.

http://ironpython.net/


Python 对于 .NET

Python for .NET (pythonnet) is a package that gives Python programmers nearly seamless integration with the .NET 4.0+ Common Language Runtime (CLR) on Windows and Mono runtime on Linux and OSX.

但是这个限制你只能使用某个版本的 python。

http://pythonnet.github.io/


其他解决方案

  • 在客户端 (python) 服务器 (c#) 中使用套接字,反之亦然。
  • 使用本地数据库或数据库文件,如 mdf。

我找到答案 here。 这家伙简明扼要地展示了 VB.NET 如何调用 IronPython 脚本函数以及如何传递和检索 VB 数据。甜

我认为 IronPython 与常规 Python 相比的价值在于 IronPython 与 VB 传递给的任何类型的 .NET 数据兼容它。

现在,我可以使用 Python 的最高开发 efficiency/productivity 创建一个具有 VB.NET 最高质量和可靠性的 GUI。