首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Python代码与c#代码集成?

如何将Python代码与c#代码集成?
EN

Software Engineering用户
提问于 2019-01-22 12:00:59
回答 2查看 72.1K关注 0票数 7

我希望将代码python (分层聚类算法)与代码C#集成起来。

(的想法)

该项目是将相似的人分成几个类,并使用该算法进行分类。我们使用语言c# (asp.net),并希望有一种方法将算法链接到代码。

EN

回答 2

Software Engineering用户

回答已采纳

发布于 2019-01-22 12:10:57

好吧,我们已经做到了。我建议您使用微服务架构:例如,您的代码调用Python服务,它计算数据并返回结果。或者,如果操作很长(大多数情况下都是集群),则使用消息队列作为两个服务之间的代理。

您可以使用库C#直接调用http://pythonnet.github.io/中的python代码。当它起作用时,它的效果令人惊讶。如果不是的话,你就处在一个充满伤害的世界里--尤其是有时候它无法加载(至少是确定的,但诊断起来很有趣)。此外,由于python,您只能使用一个Python进程。此外,在Python之间编组数据也是非常重要的,并且会导致有趣的调试会话,例如试图在C#中插入IENumerable的内部。

总之,微服务模式更易于构建、推理、调试和构建。唯一的缺点是,你现在有2或3个移动部分,这可能是一个交易的破坏者。

票数 17
EN

Software Engineering用户

发布于 2019-01-22 12:38:22

这篇文章可能被证明是有见地的:https://code.msdn.microsoft.com/windowsdesktop/C-and-Python-interprocess-171378ee

它基本上详细说明了您编写了一个python脚本,它使用命令行参数并打印返回值。然后,您的C#代码使用所需的参数调用此脚本,并从中获取值。

代码语言:javascript
复制
using System; 
using System.IO; 
using System.Diagnostics; 

namespace CallPython 
{ 
    /// <summary> 
    /// Used to show simple C# and Python interprocess communication 
    /// Author      : Ozcan ILIKHAN 
    /// Created     : 02/26/2015 
    /// Last Update : 04/30/2015 
    /// </summary> 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            // full path of python interpreter 
            string python = @"C:\Continuum\Anaconda\python.exe"; 

            // python app to call 
            string myPythonApp = "sum.py"; 

            // dummy parameters to send Python script 
            int x = 2; 
            int y = 5; 

            // Create new process start info 
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python); 

            // make sure we can read the output from stdout 
            myProcessStartInfo.UseShellExecute = false; 
            myProcessStartInfo.RedirectStandardOutput = true; 

            // start python app with 3 arguments  
            // 1st arguments is pointer to itself,  
            // 2nd and 3rd are actual arguments we want to send 
            myProcessStartInfo.Arguments = myPythonApp + " " + x + " " + y; 

            Process myProcess = new Process(); 
            // assign start information to the process 
            myProcess.StartInfo = myProcessStartInfo; 

            Console.WriteLine("Calling Python script with arguments {0} and {1}", x,y); 
            // start the process 
            myProcess.Start(); 

            // Read the standard output of the app we called.  
            // in order to avoid deadlock we will read output first 
            // and then wait for process terminate: 
            StreamReader myStreamReader = myProcess.StandardOutput; 
            string myString = myStreamReader.ReadLine(); 

            /*if you need to read multiple lines, you might use: 
                string myString = myStreamReader.ReadToEnd() */           

            // wait exit signal from the app we called and then close it. 
            myProcess.WaitForExit(); 
            myProcess.Close(); 

            // write the output we got from python app 
            Console.WriteLine("Value received from script: " + myString); 

        } 
    } 
} 
票数 3
EN
页面原文内容由Software Engineering提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://softwareengineering.stackexchange.com/questions/385936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档