The dbWrite proc below eats about 9% CPU on an i9-12900. That seems a bit much 
to me. Eventually it has to run on a HP T620 thin client.

How can I reduce it?
    
    
    [...]
    
    proc dbWrite(
      dbWriteStatement: Table[string, SqlStatement],
      datatype: Table[string, string],
      dataStream: FutureStream
    ){.async.} =
      var
        unfinished = true
        queueLen:int
      while unfinished:
        queueLen = len dataStream
        if queueLen > 0:
          exec dbWriteStatement["begin"]
          while queuelen > 0:
            dec queueLen
            #....
            dbWriteStatement[topic].exec(topic, data)
            #....
            await sleepAsync(0)
          exec dbWriteStatement["commit"]
          await sleepAsync(0)
        elif finished dataStream:
          unfinished = false
        await sleepAsync(0)
    
    
    proc mqttSub(topics: seq[string], dataStream: FutureStream) {.async.} =
      ctx.set_host("192.168.1.111", 1883)
      await ctx.start()
      
      proc on_data(topic: string, message: string) =
        echo $getTime().utc.format("yyyy-MM-dd'T'HH:mm:ss'.'fff"), "; ", topic, 
"; ", message
        discard dataStream.write(topic & ":" & message)
      
      for topic in topics:
        await ctx.subscribe(topic, 0, on_data)
      setControlCHook(handler)
    
    
    when isMainModule:
      asyncCheck mqttSub(topics, toDbStream)
      asyncCheck dbWrite(insertQueries, topicdatatype, toDbStream)
      runForever()
    
    
    Run

Reply via email to