nim-chronos
nim-chronos copied to clipboard
question: how to simply turn my server to multiple threads
I tried below , found only one single thread actually handle requests.
I read httpbeast
source its thread func use global dispatcher, then I checked chronos almost threaded dispatcher.
when compileOption("threads"):
proc threadFunc(){.thread.} =
let address = "0.0.0.0:" & $port
let flags = {ReuseAddr, ReusePort}
let r = newRouter[ScorperCallback]()
r.addRoute(jsonHandler)
r.addRoute(plaintextHandler)
var server = newScorper(address, r, flags)
onThreadDestruction proc(){.raises: [].} =
try:
server.stop(); waitFor server.closeWait()
except:
discard
server.start()
waitFor server.join()
let numThreads = countProcessors()
var thr = newSeq[Thread[void]](numThreads)
for i in 0..high(thr):
# pinToCpu(thr[i],i+1)
createThread(thr[i], threadFunc)
joinThreads(thr)
the classic way to use multiple threads is to use a single thread for accepting incoming connections then pass those connections to worker threads that handle them
Thanks! I think I need look into createStreamServer since the callback handle accepted connection.
I tried something like this https://github.com/bung87/scorper/blob/827233a807d5a1eb3cc50ccd7ca10a52199b6f92/src/scorper/http/streamserver.nim#L897-L919
it does not work, could you help me with this?