Используйте веб-сервис для вызова скрипта Python, который использует многопроцессорный модуль - PullRequest
0 голосов
/ 17 мая 2019

Я использую веб-сервис asmx для вызова скрипта Python, который использует многопроцессорный модуль. Хотя скрипт python работает нормально как отдельный, когда я вызываю его с помощью веб-службы, он выполняется, но рабочие зависают, как будто pool.close () в скрипте python не работает. Есть идеи почему?

    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()
...