nim-chronos icon indicating copy to clipboard operation
nim-chronos copied to clipboard

Interval

Open dryajov opened this issue 4 years ago • 9 comments

Adds an interval primitives triggered at specific intervals of Duration.

Additionally, adds a missing addTimer with Duration instead of Moment, which is deprecated.

dryajov avatar Dec 06 '19 16:12 dryajov

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)

cheatfate avatar Jan 29 '20 12:01 cheatfate

One more thing, your code is using already deprecated primitives.

cheatfate avatar Jan 29 '20 12:01 cheatfate

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.

dryajov avatar Jan 29 '20 15:01 dryajov

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 avatar Jan 29 '20 16:01 dryajov

@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 avatar Jan 29 '20 19:01 cheatfate

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

dryajov avatar Jan 30 '20 15:01 dryajov

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.

dryajov avatar Jan 30 '20 15:01 dryajov

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?

cheatfate avatar Feb 02 '20 08:02 cheatfate

any updates? I need api match std asyncdispatch's addTimer(oneshot=false)

bung87 avatar May 30 '21 02:05 bung87