123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace WWPipeLine.Commons
- {
- public class PythonHelp
- {
- public static string PythonPath
- {
- get
- {
- if (System.IO.File.Exists(Paths.ApplicationPy))
- {
- return Paths.ApplicationPy;
- }
- else
- {
- return "python.exe";
- }
- }
- }
- /// <summary>
- /// Python执行脚本
- /// </summary>
- /// <param name="type"></param>
- /// <param name="model"></param>
- ///
- /// <returns></returns>
- public static EnumHelper.EPyStatus RunPythonScripts(EnumHelper.EBusinessType type, object model)
- {
- var status = EnumHelper.EPyStatus.Normal;
- if (model == null)
- return EnumHelper.EPyStatus.ArgsError;
- Process p = new Process();//开启一个新进程
- try
- {
- string filepath = Paths.ApplicationPythonScripts + "InitServer.py";
- p.StartInfo.FileName = string.Format("\"{0}\" ", PythonPath.Replace("\\", "/"));
- string sArguments = string.Format(" \"{0}\" \"{1}\" {2} ", filepath, Newtonsoft.Json.JsonConvert.SerializeObject(model).Replace("\"", "\\\""), ((int)type).ToString());
- p.StartInfo.Arguments = sArguments;
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- Commons.Cache.CurrentThreadID = p.Id;
- p.WaitForExit();
- status = (EnumHelper.EPyStatus)p.ExitCode;
- }
- catch (Exception ex)
- {
- string msg = string.Empty;
- switch (ex.GetType().Name)
- {
- case "Win32Exception":
- msg = "python环境异常";
- status = EnumHelper.EPyStatus.EnvError;
- break;
- case "NullReferenceException":
- msg = "python参数传递异常";
- LogHelper.Error("python参数传递异常," + ex);
- status = EnumHelper.EPyStatus.ArgsError;
- break;
- default:
- msg = "python执行异常";
- status = EnumHelper.EPyStatus.RunError;
- break;
- }
- LogHelper.Error(msg + "," + ex);
- }
- finally
- {
- p.Dispose();
- }
- Commons.Cache.CurrentThreadID = 0;
- return status;
- }
- }
- }
|