I'm learning async programming and struggling how to run multiple heavy 
processes.

I understand how I can implement it with threadpool,
    
    
    import threadpool, times, os, strformat
      proc sleepEcho(time: int) =
        sleep(time)
        echo "slept for ", time, "ms"
      
      let starttime = epochTime()
      
      spawn sleepEcho(2) # I want this,
      spawn sleepEcho(3) # and this to start at once
      
      sync()
      
      echo fmt"Time taken: {(epochTime() - starttime)*1000:.2f}(ms)"
    
    
    Run

which echos
    
    
      slept for 2ms
      slept for 3ms
      Time taken: 3.00(ms)
    
    
    Run

I've been strugling for a while how to implement this with async.

The following code is what I've tried,
    
    
    import times, asyncdispatch, os, strformat
      proc sleepEcho(time: int) {.async.} =
        sleep(time)
        echo "slept for ", time, "ms"
      
      let starttime = epochTime()
      
      discard sleepEcho(2) # I want this,
      discard sleepEcho(3) # and this to start at once
      
      
      echo fmt"Time taken: {(epochTime() - starttime)*1000:.2f}(ms)"
    
    
    Run

whose result was
    
    
      slept for 2ms
      slept for 3ms
      Time taken: 5.21(ms)
    
    
    Run

which showed that the second sleepEcho starts AFTER the first sleepEcho 
terminated.

Reply via email to