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()
dbWritestatement is a table with prepared statements. But your comment set me on track again.
Sorry for wasting your time, the code above was reduced too much. The real problem is that in the code below (_, msg) = await dataStream.read sits too deep in the loop(s).
A await futureStream.hasContent() without consuming a value would be nice to have.
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
let
(_, msg) = await dataStream.read
smsg = msg.split(":")
topic = smsg[0]
payload = smsg[1]
[...]
dbWriteStatement[topic].exec(topic, data)
[...]
await sleepAsync(0)
exec dbWriteStatement["commit"]
await sleepAsync(0)
await sleepAsync(0)
if finished dataStream:
unfinished = false
handler()
await sleepAsync(0)
You can syntax highlight code here by doing this:
'''nim
while true:
echo "hi"
'''
(use backticks ` instead of ' I just don't know how to escape them)
while true:
echo "hi"
the posts are to old to update. I'll try to remember.
A bit of rearranging the code made it all work smooth now.