Git Product home page Git Product logo

Comments (8)

wlandau avatar wlandau commented on June 7, 2024

So it looks like until() decrements the cv even when it hits the timeout. So I can create an even smaller reprex:

library(nanonext)
#> Warning: package 'nanonext' was built under R version 4.3.1
cv <- cv()
cv_value(cv)
#> [1] 0
cv_signal(cv)
cv_signal(cv)
cv_value(cv)
#> [1] 2
until(cv, 10) # decrements the cv
cv_value(cv)
#> [1] 1

Created on 2023-10-03 with reprex v2.0.2

@shikokuchuo, is the decrement always guaranteed to happen on every call to until(). If so, I might be able to work with this by defining my own version:

crew_until <- function(condition, seconds_timeout) {
  on.exit(nanonext::cv_signal(condition))
  nanonext::until(cv = condition, msec = 1000 * seconds_timeout)
}

I will test this.

from nanonext.

wlandau avatar wlandau commented on June 7, 2024

Hmm..., it looks like nanonext::until() cannot decrement the cv any further if it is already 0, so then cv_signal(condition) would be incorrect. And writing if (cv_value(condition) > 0L) cv_signal(condition) is a race condition because what if a task completes after if() but before cv_signal()?

On my end, the only other solution I can think of is to preemptively call cv_signal() a few thousand times before I submit any tasks at all, but that seems really messy, and I am not sure what edge cases that would cause.

So then would it be possible for until() to not signal the cv in the first place?

from nanonext.

wlandau avatar wlandau commented on June 7, 2024

Hmm... maybe I am misunderstanding the intended use cases of wait() and until(). I can see now that this is the intended behavior in the docs. In addition, I notice that until() only waits if cv_value(cv) is 0. Any positive value will not cause a wait:

library(nanonext)
cv <- cv()
cv_signal(cv)
until(cv, 10000) # returns control instantly

This creates a challenge for me when using mirai::nextget("cv"). I would like to be able to wait on different kinds of conditions. Mainly:

  • Wait until a cv value changes, regardless of its current value, without changing its value further.
  • Wait until cv_value(cv) reaches a certain fixed number. controller$wait() and controller$map() could be simplified a lot with something like nanonext::until(cv, msec, value_wake_up = 7L, decrement_value = FALSE).

from nanonext.

wlandau avatar wlandau commented on June 7, 2024

And I think a manual cv_decrement() could also add a stronger meaning to nextget("cv"). Currently, cv_value(nextget("cv")) is the number of resolved tasks in the lifetime of the crew controller, regardless of whether they have been returned with pop(). If nanonext were to support a manual way to decrement a cv, then I could decrement it on each controller$pop(). Now, the cv would indicate the number of resolved tasks currently available for collection, which is much more powerful and would allow me to make controller$wait() simpler and more efficient.

from nanonext.

shikokuchuo avatar shikokuchuo commented on June 7, 2024

until(cv, 1) decrements your cv safely.

from nanonext.

shikokuchuo avatar shikokuchuo commented on June 7, 2024

until() is correct and behaves as documented. If it is something else you are after please open a new feature request. It will not be implemented as a change to until(). Thanks.

from nanonext.

wlandau avatar wlandau commented on June 7, 2024

Sorry, that was a lot of thinking out loud on my part.

from nanonext.

shikokuchuo avatar shikokuchuo commented on June 7, 2024

No worries! My comment just meant that any new functionality should belong in a new function. These synchronisation primitives are basic building blocks and it would not be right to amend them however convenient it may seem.

from nanonext.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.