通过C ++桥从.NET调用Python [英] Calling Python from .NET via C++ bridge

查看:162
本文介绍了通过C ++桥从.NET调用Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每几个月一次,我google了从.NET调用Python,当我太累了,无法重新创建/端口的东西,并在Python中找到一些很酷的代码。两个流行的解决方案是启动一个外部进程或使用IronPython

Once per a couple of months I google for "Call Python from .NET" when I am too tired to reinvent/port stuff and find some cool code in Python. The two popular solutions are to start an external process or to use IronPython

IronPython不是我的选择,因为我可能想使用Python的原因通常是一些库取决于NumPy / ScyPy。

IronPython is not an option for me since the reason I might want to use Python is usually some libraries that depend on NumPy/ScyPy.

最近我开始学习C ++的基础知识,并发现可以在C ++中嵌入Python,并发现了一个带有示例的文章

Recently I started to learn the very basics of C++ and have found that one could embed Python in C++, and have found an article with an example.

通过P / Invoke调用C ++非常容易。所以我的方案是使用.NET中的数据调用一些Python脚本并返回结果。

Calling C++ from .NET is very easy via P/Invoke. So my scenario is to call some Python script with data from .NET and get results back.

在我开始挖掘之前,我想问一下是否有人试过这种方法,是可行的还是有更好的方法?

Before I start digging deeper I want to ask if someone has tried this approach, is this feasible or there are better ways?

具体来说,


  • Python嵌入式工作,例如NumPy,ScyPy,Pandas,PyMC等?

  • 嵌入式Python与C ++ / CLI的工作方式与标准C ++相同?

  • 如何将复杂数据传递给Python:序列化为JSON或类型之间是否有映射?

性能是一个问题:情况是在实时数据的滑动窗口上做一些数值工作。我认为外部程序对于频繁的呼叫非​​常慢 - 如果我错了,请更正我。

Performance is a concern: the main use case is to do some numeric work on a sliding window of real time data. I think external process is very slow for frequent calls - please correct me if I am wrong?

推荐答案

以下方式: -

有一个C#dll,我需要从中查询数据。所以,我开发了C ++ / CLI模块与该dll交互,并处理一些代理和事件对应的数据。

There was a C# dll from which I need to query for data. So, I developed C++/CLI module to interact with that dll and handled some delegates and events corresponding to that data. Then C++ module would accept this data, process it and send it to python module.

python和c ++之间的间隙是使用zmq(替换纯sockets)和google协议来填充的缓冲区(序列化和反序列化机制)。 python会做的只是发送请求到C + +然后转移到C + + / CLI模块,它获取数据将推送通过zmq到python ...

Gap between python and c++ was filled using zmq ( replacement for plain sockets ) and google protocol buffers ( serialization and deserialization mechanism ). What python would do is just send request to C++ which then transfers that to C++/ CLI module which on getting data would push through zmq to python...

这篇关于通过C ++桥从.NET调用Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆