multithreading - F# spawn a new async request every second -


my goal (before expand on it) start async task (in case, fetching url) once second. idea if async task takes 3 seconds, behave this:

0sec: start thread 0 1sec: start thread 1 2sec: start thread 2 3sec: start thread 3 3sec: finish thread 0 4sec: start thread 4 4sec: finish thread 1 

here's code far. worktime , async.sleep code added make problem more apparent.

let fetchurlasync (thread: int) (url: string) =     async {         let req = webrequest.create(url)         let! resp = req.asyncgetresponse()         use stream = resp.getresponsestream()         use reader = new system.io.streamreader(stream)          let worktime = system.random().next(5000,8000)         sprintf "start work %i" thread |> console.writeline         do! async.sleep worktime         sprintf "end work %i (time taken: %i)" thread worktime |> console.writeline         return reader.readtoend()     }  seq.initinfinite(fun -> fetchurlasync "http://www.google.com"                           |> async.startastask                           |> ignore                           thread.sleep 1000                 ) |> seq.iter(id) 

the issue there seems maximum of 2 threads running @ same time. why happening , doing wrong?

also, first time doing async. please let me know if see way improve code (is using thread.sleep right thing here?)

the problem system.net.servicepointmanager.defaultconnectionlimit default set 2. raise value higher before requests:

system.net.servicepointmanager.defaultconnectionlimit <- 10 

Comments