使用Web服务来调用使用多处理模块的python脚本

时间:2019-05-17 13:28:00

标签: c# python multiprocessing asmx

我正在使用asmx网络服务来调用使用多处理模块的python脚本。当我使用Web服务调用python脚本时,它可以独立运行,但可以执行,但工作人员挂起,好像python脚本中的pool.close()无法正常工作。有什么想法吗?

    C# CODE
        [WebMethod]
    public string DailyRouting()
    {
        if (Debugger.IsAttached)
            System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
        try
        {
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = "C:\\Python27\\ArcGIS10.4\\python.exe";

            ////pass these to Arguements property of ProcessStartInfo instance
            start.Arguments = string.Format("{0} ", "C:\\Users\\****\\Desktop\\ArcPy\\RouteAnalyst\\routeAnalysis_v55_WEB.py");

            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            start.CreateNoWindow = true;

            Stopwatch sw = new Stopwatch();
            sw.Restart();
            using (Process process = Process.Start(start))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    //result = reader.ReadLine();
                    string result = reader.ReadToEnd();
                    int result2 = process.ExitCode;
                    //Console.Write(result);
                }
            }
            sw.Stop();
            var Elapsed_Time = sw.Elapsed;
            return "Routing time: " + Elapsed_Time;
        }
        catch (System.Exception ex)
        {
            string message = ex.Message.ToString();
            return "Error: " + message;
        }

    }

python脚本片段

func = partial(testMethod)

# declare number of cores to use 
cpuNum = multiprocessing.cpu_count()
# Create the pool object
pool = multiprocessing.Pool(processes=cpuNum, maxtasksperchild=1)
# Fire off list to worker function.  
# res is a list that is created with what ever the worker function is returning  
results=pool.map(func,StationsList, chunksize=1)  

pool.close()  
pool.join()

0 个答案:

没有答案