f# - How to convert the download program to async? -


i have following code

open fsharp.data  let downloadfile link =     ......     use os = file.create(...)     http.requeststream(....).reponsestream.copyto(os)  let rec consume() = async {     ......     |> seq.iter (fun x ->         xxx |> seq.iter(fun link ->             downloadfile link     )) } 

i found sync downloading makes code not run concurrently. i'm trying somthing following. how change use fsharp.data http asyncrequeststream? maybe copyto can async too?

open fsharp.data  let downloadfile link = async {     ......     use os = file.create(...)     http.asyncrequeststream(....).reponsestream.copyto(os) // error }  let rec consume() = async {     ......     |> seq.iter (fun x ->          xxx |> seq.iter(fun link ->             downloadfile link |> async.start // do! downloadfile link????    )) } consume() |> async.runsynchronously 

here's skeleton solution, worthy of blank spots in example:

let downloadfile link =      async {         ......         use os = file.create(...)         let! resp = http.asyncrequeststream(....)         return resp.reponsestream.copyto(os)     }  let consume link =      async {         let comps : async<unit> [] =              xxx              |> seq.map (fun link -> downloadfile link)             |> array.ofseq         return! async.parallel comps                     } 

i think should read on asynchronicity , concurrency in general, how use in f# in particular. op seems whole thing bit hazy you.

edit: answer question in comment:

with return! (or let!, or do!) execute nested workflow asynchronously, pick executing current workflow point. is, "below" do! put continuation gets called once thing "after" do! finishes.

whereas async.start fires workflow on (another) background thread , returns without waiting finish.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -