Hello everyone!

I am trying to make a bunch of http calls using the async http client. Code:
    
    
    import httpClient, asyncdispatch
    
    proc downloadAll(urls: seq[string]) {.async.} =
      var client = newAsyncHttpClient()
      for url in urls:
        var future = client.get(url)
        yield future
        if future.failed:
          echo "Problem!"
        else:
          echo "OK"
    
    let urls = @[
      "http://127.0.0.1:5000/json";,
      "http://127.0.0.1:5001/json";,
      "http://127.0.0.1:5002/json";,
    ]
    waitFor downloadAll(urls)
    
    
    Run

My expectation was that the yield would cause the event loop to schedule the 
get request and move on to the next iteration. However this code works 
sequentially. I know this is happening sequentially because localhost runs an 
app that sleeps for 5 seconds before serving the content and it takes 15s for 
this code to finish - I'd expect something close to 5s

I'd like to better understand why this happens. Does yield not do what I think 
it does inside a loop?

p.s. FWIW, I have another version based on previous threads on this forum that 
works that uses two procs to get the job done but I was wondering if there was 
a simpler way to go about it.

Thanks! Deepak

Reply via email to