Guest User

Untitled

a guest
Feb 9th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.79 KB | None | 0 0
  1. import asyncdispatch, asynchttpserver, os, locks
  2.  
  3. proc parseRequest(req: Request) {.async.} =
  4.   os.sleep(100)
  5.   await req.respond(Http200, "sdlkfj")
  6.  
  7.  
  8. # TODO remove gcsafe, once Threads are introduced
  9. proc cb(req: Request) {.async.} =
  10.   # prints only the id of the first started server
  11.   echo getThreadId()
  12.   await parseRequest(req)
  13.  
  14.  
  15. proc start*() =
  16.   # printes different ids
  17.   echo getThreadId()
  18.   let server = newAsyncHttpServer(true, true)
  19.   waitFor server.serve(Port(5000), cb)
  20.  
  21.  
  22. when isMainModule:
  23.   when compileOption("threads"):
  24.     let
  25.       cores: int = countProcessors()
  26.     var
  27.       threads = newSeq[Thread[void]](cores)
  28.     for i in 0 ..< cores:
  29.       createThread[void](threads[i], mas.start)
  30.     joinThreads(threads)
  31.   else:
  32.     echo "compile with threads"
Advertisement
Add Comment
Please, Sign In to add comment