Is there any advantage to using spawn tasks instead of asynchronous processing 
of http requests? Is it faster to process the requests? My idea was to use the 
same approach that golang uses with goroutines. Many Nim libraries does not 
have asynchronous support.

Code from asynchttpserver: 
    
    
    #
    # 
https://github.com/nim-lang/Nim/blob/version-1-4/lib/pure/asynchttpserver.nim#L342
    #
    proc serve*(server: AsyncHttpServer, port: Port,
                callback: proc (request: Request): Future[void] {.closure, 
gcsafe.},
                address = "";
                assumedDescriptorsPerRequest = -1) {.async.} =
      listen server, port, address
      while true:
        if shouldAcceptRequest(server, assumedDescriptorsPerRequest):
          var (address, client) = await server.socket.acceptAddr()
          asyncCheck processClient(server, client, address, callback)
        else:
          poll()
    
    
    Run

The idea is spawns a new task for the new requests instead of asynchronous 
processing. 
    
    
    proc serve*(server: HttpServer, port: Port,
                callback: proc (request: Request) {.closure, gcsafe.},
                address = "") =
      listen server, port, address
      
      while true:
        var client: Socket
        var address = ""
        server.socket.acceptAddr(client, address)
        spawn processClient(server client, address, callback) # <=======
    
    
    Run

Reply via email to