File: early-signaling.R

package info (click to toggle)
r-cran-future 1.11.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,380 kB
  • sloc: sh: 14; makefile: 2
file content (138 lines) | stat: -rw-r--r-- 3,804 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
source("incl/start.R")

options(future.debug = FALSE)

message("*** Early signaling of conditions ...")

message("*** Early signaling of conditions with sequential futures ...")

plan(sequential)
f <- future({ stop("bang!") })
r <- resolved(f)
stopifnot(r)
v <- tryCatch(value(f), error = identity)
stopifnot(inherits(v, "error"))

message("- with lazy evaluation ...")
f <- future({ stop("bang!") }, lazy = TRUE)
r <- resolved(f)
stopifnot(r)
v <- tryCatch(value(f), error = identity)
stopifnot(inherits(v, "error"))

plan(sequential, earlySignal = TRUE)
f <- tryCatch(future({ stop("bang!") }), error = identity)
stopifnot(inherits(f, "error"))

message("- with lazy evaluation ...")

## Errors
f <- future({ stop("bang!") }, lazy = TRUE)
r <- tryCatch(resolved(f), error = identity)
stopifnot(inherits(r, "error"))
v <- tryCatch(value(f), error = identity)
stopifnot(inherits(v, "error"))

## Warnings
f <- future({ warning("careful!") }, lazy = TRUE)
res <- tryCatch({
  r <- resolved(f)
}, condition = function(w) w)
str(res)
stopifnot(inherits(res, "warning"))

## Messages
f <- future({ message("hey!") }, lazy = TRUE)
res <- tryCatch({
  r <- resolved(f)
}, condition = function(w) w)
stopifnot(inherits(res, "message"))

## Condition
f <- future({ signalCondition(simpleCondition("hmm")) }, lazy = TRUE)
res <- tryCatch({
  r <- resolved(f)
}, condition = function(w) w)
stopifnot(inherits(res, "condition"))

message("*** Early signaling of conditions with sequential futures ... DONE")


message("Number of available cores: ", availableCores())

message("*** Early signaling of conditions with multisession futures ...")

plan(multisession)
f <- future({ stop("bang!") })
Sys.sleep(0.5)
r <- resolved(f)
stopifnot(r)
v <- tryCatch(value(f), error = identity)
stopifnot(inherits(v, "error"))

plan(multisession, earlySignal = TRUE)
f <- future({ stop("bang!") })
Sys.sleep(0.5)
print(f)
r <- tryCatch(resolved(f), error = identity)
stopifnot(inherits(r, "error") || inherits(f, "SequentialFuture"))
v <- tryCatch(value(f), error = identity)
stopifnot(inherits(v, "error"))


message("*** Early signaling of conditions with multisession futures ... DONE")


if (supportsMulticore()) {
  message("*** Early signaling of conditions with multicore futures ...")
  
  plan(multicore)
  f <- future({ stop("bang!") })
  Sys.sleep(0.5)
  r <- resolved(f)
  stopifnot(r)
  v <- tryCatch(value(f), error = identity)
  stopifnot(inherits(v, "error"))
  
  plan(multicore, earlySignal = TRUE)
  f <- future({ stop("bang!") })
  Sys.sleep(0.5)
  print(f)
  r <- tryCatch(resolved(f), error = identity)
  stopifnot(inherits(r, "error") || inherits(f, "SequentialFuture"))
  v <- tryCatch(value(f), error = identity)
  stopifnot(inherits(v, "error"))
  
  ## Errors
  f <- future({ stop("bang!") }, earlySignal = TRUE)
  Sys.sleep(0.5)
  r <- tryCatch(resolved(f), error = identity)
  stopifnot(inherits(r, "error") || inherits(f, "SequentialFuture"))
  v <- tryCatch(value(f), error = identity)
  stopifnot(inherits(v, "error"))
  
  ## Warnings
  f <- future({ warning("careful!") }, earlySignal = TRUE)
  Sys.sleep(0.5)
  res <- tryCatch({ r <- resolved(f) }, condition = function(w) w)
  #stopifnot(inherits(res, "warning"))
  
  ## Messages
  f <- future({ message("hey!") }, earlySignal = TRUE)
  Sys.sleep(0.5)
  res <- tryCatch({ r <- resolved(f) }, condition = function(w) w)
  #stopifnot(inherits(res, "message"))
  
  ## Condition
  f <- future({ signalCondition(simpleCondition("hmm")) }, earlySignal = TRUE)
  Sys.sleep(0.5)
  res <- tryCatch({ r <- resolved(f) }, condition = function(w) w)
  #stopifnot(inherits(res, "condition"))
  
  message("*** Early signaling of conditions with multicore futures ... DONE")
}


message("*** Early signaling of conditions ... DONE")

source("incl/end.R")