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%.