-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Private event loop behaves differently when executed within block #174
Comments
It doesn't behave that way for me -- in the second case, it still prints out the time after 5 seconds. It does the same on my mac in both RStudio and in a terminal. (Note that I stepped through each line for the second case.)
Can you provide the output of
|
@wch, here is
I also tried on R v4.0.5 on console: R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> fn <- function() print(Sys.time())
> tloop <- later::create_loop()
> {print(Sys.time()); later::later(fn, delay = 2, loop = tloop); Sys.sleep(5)}
[1] "2023-05-14 19:07:55 CEST"
> [1] "2023-05-14 19:08:01 CEST"
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=Dutch_Belgium.1252 LC_CTYPE=Dutch_Belgium.1252
[3] LC_MONETARY=Dutch_Belgium.1252 LC_NUMERIC=C
[5] LC_TIME=Dutch_Belgium.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.0.5 later_1.3.0 Rcpp_1.0.8.3 rlang_1.0.2
> Closed the console, then opened again; R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> fn <- function() print(Sys.time())
> tloop <- later::create_loop()
> print(Sys.time())
[1] "2023-05-14 19:08:53 CEST"
> later::later(fn, delay = 2, loop = tloop)
> Sys.sleep(5)
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=Dutch_Belgium.1252 LC_CTYPE=Dutch_Belgium.1252
[3] LC_MONETARY=Dutch_Belgium.1252 LC_NUMERIC=C
[5] LC_TIME=Dutch_Belgium.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.0.5 later_1.3.0 Rcpp_1.0.8.3 rlang_1.0.2
> later::run_now(loop=tloop)
[1] "2023-05-14 19:09:38 CEST"
> |
Thanks for the info. I wonder if it x fn <- function() print(Sys.time())
{
print(Sys.time())
later::later(fn, delay = 2)
Sys.sleep(5)
}
print(Sys.time())
later::later(fn, delay = 2)
Sys.sleep(5) This is what I see:
|
Then I get the same as you yes. This is what I expect from a global event loop, but I don't understand exactly how a private loop should behave. I'll omit a printout to keep the thread readable, but if you need it let me know. |
When a loop is created with When a parent loop runs, it runs its children. So the behavior that I see is what I would expect. I don't know why yours is behaving that way. When you restart and run
|
@DavorJ I finally dug out my Windows machine and tested on it, and I get the same result as you, so it seems likely that there's something not right about how we do things on Windows. |
Here is an odd case that may be helpful in understanding what happens on Windows: object <- function() {
CANCEL <- function() NULL
# LOOP <- later::global_loop()
LOOP <- later::create_loop(parent = later::global_loop())
i <- 0L
task <- function() {
print(i <<- i + 1)
Sys.sleep(1)
CANCEL <<- later::later(schedule, loop = LOOP)
}
schedule <- function() {
if (i < 30L) CANCEL <<- later::later(task, loop = LOOP)
}
destroy <- function(envir) {later::destroy_loop(LOOP)}
reg.finalizer(e = environment(), f = destroy, onexit = TRUE)
list(
'start' = function() schedule(),
'cancel' = function() CANCEL(),
'wait' = function() {
print('Waiting...')
while (!later::loop_empty(loop = LOOP)) {
later::run_now(timeoutSecs = -1, loop = LOOP)
}
print('Done...')
}
)
}
obj <- object()
obj$start()
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
# [1] 6
# [1] 7
# [1] 8
# [1] 9
# [1] 10 This should count to 30. Oddly, on Windows it counts to 10 only, then stops. You can execute Replacing |
Let's define this function and event loop:
If I execute a block of code like this, then
fn
gets exxecuted:But if I execute line by line (important!), I get this:
Why does the
fn
execute in first case, but not in the second case? Is this by design, or a bug?In the second case,
fn
would be execueted only if 2 seconds pass and either:later
is invoked againlater::run_now(loop = tloop)
is executed.The text was updated successfully, but these errors were encountered: