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

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

问题描述

在每两个月我谷歌的从.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.

从.NET调用C ++是通过P / Invoke的非常容易。所以,我的方案是调用与.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,熊猫,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?

推荐答案

我已经用它曾经在方式如下: -

I have used it once in following way:-

有一个C#DLL从中我需要查询数据。所以,我开发的C ++ / CLI模块与该DLL交互和处理,一些代表,并对应于数据事件。那么C ++模块会接受这个数据,处理它,并将其发送给Python模块。

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(更换为普通插座)和谷歌的协议充满缓冲区(序列化和反序列化机制)。蟒蛇什么会做的仅仅是发送请求给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天全站免登陆