I'm learning nim async programming by reading the book "Nim In Action", and here is the example code of 3.17:
import asyncdispatch, asyncfile
var file = openAsync("/etc/passwd")
let dataFut = file.readAll()
dataFut.callback =
proc (future: Future[string]) =
echo(future.read())
asyncdispatch.runForever()
but I get "No handles or timers registered in dispatcher" error on the compiler. How can I fix it?
You can just do this:
import asyncdispatch, asyncfile
var file = openAsync("/etc/passwd")
let dataFut = file.readAll()
echo waitFor(dataFut)
or
import asyncdispatch, asyncfile
var file = openAsync("/etc/passwd")
let dataFut = file.readAll()
dataFut.callback =
proc (future: Future[string]) =
echo(future.read())
while asyncdispatch.hasPendingOperations():
asyncdispatch.poll()
@dom Isn't it a good reason to put
while asyncdispatch.hasPendingOperations():
asyncdispatch.poll()
into runForever or waitFor?You've advised me to do so in my bug report somewhere.
I don't really understand why there should be a 100% CPU usage while waiting for callbacks to fire? Neither asyncio python, nor even 2.7's twisted and tornado main loops never caused 100% cpu usage when waiting for callbacks.
We're awaiting for some future, which might be somewhere, so main loop is knowing that it has at least 1 waitpoint and then it suddenly fails with exception that we're not running anything... That's weird. Might we register all awaited futures somewhere and remove them from list on fire(complete of failed)? This will avoid throwing an exception while we're awaiting something, but will throw it if there's really no any task pending in main loop.