After some investigation I found a way to make it work! But I don't know if it 
is the best solution, or if it is correct. I write a small program to 
illustrate the idea.
    
    
    import asynchttpserver, asyncdispatch
    import os
    import threadpool
    import random
    import strutils
    import times
    
    proc mySyncProc(n: int): int =
      var count = 0
      while true:
        let t = rand(1000..2000)
        sleep(t)
        count += 1
        if count == n:
          return count
      return 0
    
    ### Is this correct? ###
    proc testSync(): int =
      let messageFlowVar = spawn mySyncProc(rand(10..20))
      while not messageFlowVar.isReady(): ## what happen if never is ready???
        asyncdispatch.poll() ## This prevents the blocking???
      return ^messageFlowVar
    
    proc main() =
      var server = newAsyncHttpServer()
      proc cb(req: Request) {.async.} =
        let start = now()
        let r = testSync()
        await req.respond(Http200, "Hello World $1 Duration: $2" % [$r, $(now() 
- start)])
      
      waitFor server.serve(Port(8080), cb)
    
    main()
    
    
    Run

Reply via email to