Need help regarding Async and fsi

后端 未结 5 958
我寻月下人不归
我寻月下人不归 2020-12-17 05:24

I\'d like to write some code that runs a sequence of F# scripts (.fsx). The thing is that I could have literally hundreds of scripts and if I do that:

let sh         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 05:46

    In response to Tomas's answer, would this be a workable solution to the race condition involved in starting the process, and then subscribing to its Exited event?

    type Process with
        static member AsyncStart psi =
            let proc = new Process(StartInfo = psi, EnableRaisingEvents = true)
            let asyncExit = Async.AwaitEvent proc.Exited
            async {
                proc.Start() |> ignore
                let! args = asyncExit
                return proc
            }
    

    Unless I'm mistaken, this would subscribe to the event prior to starting the process, and package it all up as an Async result.

    This would allow you to rewrite the rest of the code like this:

    let shellExecute program args = 
      // Configure process to redirect output (so that we can read it)
      let startInfo = 
        new ProcessStartInfo(FileName = program, Arguments = args, 
            UseShellExecute = false,
            WindowStyle = ProcessWindowStyle.Hidden, 
            RedirectStandardOutput = true)
    
      // Start the process
      Process.AsyncStart(startInfo)
    
    let fsi = "PATH TO FSI.EXE"
    
    let rec loop scripts = async { 
        match scripts with 
        | [] -> printf "FINISHED"
        | script::scripts ->
            // Start the proces in background
            use! p = shellExecute fsi script 
            // Read the output produced by the process, the exit code
            // is available in the `ExitCode` property of `Process`
            let output = p.StandardOutput.ReadToEnd()
            printfn "\nPROCESSED: %s, CODE: %d\n%A" script p.ExitCode output
            // Process the rest of the scripts
            return! loop scripts 
    } 
    

    If that does the job, it's certainly a lot less code to worry about than Vladimir's Async.GetSubject.

提交回复
热议问题