简体   繁体   English

进程在 python 代码执行之前终止

[英]process terminates before python code execution

I have a .net core application and I created a process to run my.bat file which includes commands for a python script extracting gabor features from a video.我有一个 .net 核心应用程序,我创建了一个运行 my.bat 文件的进程,其中包括 python 脚本从视频中提取 gabor 特征的命令。 I want to hold the program until python code finishes(until all features are extracted)我想保留程序直到 python 代码完成(直到提取所有特征)

I want to fully extact featues before I return to View(info).However, instead of waiting for the python script, code directly goes to return statment我想在返回 View(info) 之前完全提取特征。但是,不是等待 python 脚本,而是代码直接返回语句

        public async Task<IActionResult> ShowVideo(VideoInfo info)
        {
            info.name = info.url;
            string path = "/videos/" + info.url;
            info.url = path;
            info.possibleVideoList = GenerateVideoDropDown();

            await ExtractFramesAndGenerateSubtitles(info);

            return View(info);
        }
        public async Task ExtractFramesAndGenerateSubtitles(VideoInfo info)
        {
            string videPath = Path.Combine(Environment.WebRootPath, "videos/") + info.name;
            var outputPath = "frames";
            string pathName = info.possibleVideoList[info.name];

            //Move file to the correct location so the Gabor feature extraction code can use
            ChangeFileNameAndMove(videPath);

            // TODO: Can be deleted later
            //Exctract frames
            GenerateFrames(pathName, outputPath);

            //Run the Gabor feature extraction
            ExecuteCommandSync("runGabor.bat");

        }
        public async Task ExecuteCommandSync(object command)
        {
            var timeoutSignal = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            System.Diagnostics.ProcessStartInfo procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);

            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
           
            procStartInfo.CreateNoWindow = false;
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;
            //proc.Start();
            if (proc.Start())
            {
                proc.EnableRaisingEvents = true;
                proc.BeginOutputReadLine();
                proc.BeginErrorReadLine();

                await proc.WaitForExitAsync(timeoutSignal.Token);
                //allow std out to be flushed
                await Task.Delay(100);
            }
            // Get the output into a string
            string result = proc.StandardOutput.ReadToEnd();

        }

Add await before your "ExecuteCommandSync("runGabor.bat");"在“ExecuteCommandSync("runGabor.bat");”之前添加await like below:如下所示:

 await ExecuteCommandSync("runGabor.bat");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM