nim-chronos
nim-chronos copied to clipboard
Interval
Adds an interval
primitives triggered at specific intervals of Duration
.
Additionally, adds a missing addTimer
with Duration
instead of Moment
, which is deprecated.
Why we need such primitive? And why not use:
proc runWithTimeInterval(time: Duration, callback: AnyOtherCallbackType): Future[void] =
while true:
await sleepAsync(time)
if not callback():
break
proc someProc() {.async.} =
asyncCheck runWithTimeInterval(1.seconds, someProcedure)
One more thing, your code is using already deprecated primitives.
Why we need such primitive? And why not use:
I need to schedule callbacks at intervals.
One more thing, your code is using already deprecated primitives.
This been sitting in review for some time, you just got around to reviewing it. Don't remember, but my best guess is that they weren't deprecated at the time or some other limitation of the implementation.
Also, I'd appreciate a more promptly response for the PRs next time. It's been almost two months and I just got your attention, meanwhile this had to be hacked into libp2p directly.
Also, if addTimer
is internal, how should callbacks triggered in the future be scheduled? Neither of sleepAsync()
, wait()
, withTimeout()
, allow you to schedule things to run in the future AFAIS?
@dryajov you can check my code example how to schedule code to be running in future.
await sleepAsync(someTime)
# schedule your code here
or if you need to schedule your code periodically you can do
while true:
await sleepAsync(someTime)
# schedule your code here
Sorry, that i have not answered you in time.
@cheatfate I don't think your examples will work. This assume you need to wait for something to execute, I don't need to wait for it, I need this to be triggered at some point in the future, like a cleanup task or something similar. That's why addTimer/setTimer
and addInterval/setInterval
are core scheduling primitives, you've something like this available in every language that implements the asynchronous paradigm.
I mean, runWithTimeInterval
would definitely work, but this should be part of chronos, addTimer
should be kept public as well, as there is no alternative to it.
python.asyncio
do not have interval functions as part of core library.
boost.asio
do not have interval functions as part of core library.
Why chronos
should have such primitives?
any updates? I need api match std asyncdispatch's addTimer(oneshot=false)