It hasn't been fixed yet.
But that's a pretty extreme case I think? Are you having problems in your application because of it?
Thank you, dom96. This is my simple code:
import asyncdispatch, threadpool
var started = false
proc process() {.async.} =
if started: return
started = true
echo "start process"
while started:
echo "..."
await sleepAsync(5000)
echo "stop process"
proc main() {.async.} =
var msg = spawn stdin.readLine()
while true:
if msg.isReady():
case ^msg:
of "stop": started = false
of "start": asyncCheck process()
of "bye":
if started: echo"enter stop before" else: return
else: echo"invalid command"
msg = spawn stdin.readLine()
await sleepAsync(500)
waitFor main()
After a few seconds cpu load ~50%. What am I doing wrong?
import asyncdispatch, threadpool
var started = false
proc process() {.async.} =
if started: return
started = true
echo "start process"
while started:
var client = newAsyncHttpClient()
var resp = await client.getContent("http://google.com")
echo $len(resp)
#doSomething
client.close
await sleepAsync(5000)
echo "stop process"
proc main() {.async.} =
var msg = spawn stdin.readLine()
while true:
if msg.isReady():
case ^msg:
of "stop": started = false
of "start": asyncCheck process()
of "bye":
if started: echo"enter stop before" else: return
else: echo"invalid command"
msg = spawn stdin.readLine()
await sleepAsync(500)
waitFor main()
This code has async IO. The result is the same cpu load 50%.Hrm, try this as a workaround:
import asyncdispatch, threadpool, os
var started = false
proc process() {.async.} =
if started: return
started = true
echo "start process"
while started:
var client = newAsyncHttpClient()
var resp = await client.getContent("http://google.com")
echo $len(resp)
#doSomething
client.close
await sleepAsync(5000)
echo "stop process"
proc main() =
var msg = spawn stdin.readLine()
while true:
if msg.isReady():
case ^msg:
of "stop": started = false
of "start": asyncCheck process()
of "bye":
if started: echo"enter stop before" else: return
else: echo"invalid command"
msg = spawn stdin.readLine()
if hasPendingOperations():
poll(500)
else:
sleep(500)
main()
--
if hasPendingOperations():
poll(500)
else:
sleep(500)
--
This works in part. When I enter start, the load is again 50%. When I enter stop, the load is ok.
I rewritten in my file the waitFor procedure:
proc waitFor*[T](fut: Future[T]; ms: int = 50): T =
while not fut.finished:
poll()
sleep ms
fut.read
It works for me.