Debian Codegraph @

man/later_fd.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/later.R
3 \name{later_fd}
4 \alias{later_fd}
5 \title{Executes a function when a file descriptor is ready}
6 \usage{
7 later_fd(
8 func,
9 readfds = integer(),
10 writefds = integer(),
11 exceptfds = integer(),
12 timeout = Inf,
13 loop = current_loop()
14 )
15 }
16 \arguments{
17 \item{func}{A function that takes a single argument, a logical vector that
18 indicates which file descriptors are ready (a concatenation of \code{readfds},
19 \code{writefds} and \code{exceptfds}). This may be all \code{FALSE} if the
20 \code{timeout} argument is non-\code{Inf}. File descriptors with error conditions
21 pending are represented as \code{NA}, as are invalid file descriptors such as
22 those already closed.}
23
24 \item{readfds}{Integer vector of file descriptors, or Windows SOCKETs, to
25 monitor for being ready to read.}
26
27 \item{writefds}{Integer vector of file descriptors, or Windows SOCKETs, to
28 monitor being ready to write.}
29
30 \item{exceptfds}{Integer vector of file descriptors, or Windows SOCKETs, to
31 monitor for error conditions pending.}
32
33 \item{timeout}{Number of seconds to wait before giving up, and calling \code{func}
34 with all \code{FALSE}. The default \code{Inf} implies waiting indefinitely.
35 Specifying \code{0} will check once without blocking, and supplying a negative
36 value defaults to a timeout of 1s.}
37
38 \item{loop}{A handle to an event loop. Defaults to the currently-active loop.}
39 }
40 \value{
41 A function, which, if invoked, will cancel the callback. The
42 function will return \code{TRUE} if the callback was successfully
43 cancelled and \code{FALSE} if not (this occurs if the callback has
44 executed or has been cancelled already).
45 }
46 \description{
47 Schedule an R function or formula to run after an indeterminate amount of
48 time when file descriptors are ready for reading or writing, subject to an
49 optional timeout.
50 }
51 \details{
52 On the occasion the system-level \code{poll} (on Windows \code{WSAPoll}) returns an
53 error, the callback will be made on a vector of all \code{NA}s. This is
54 indistinguishable from a case where the \code{poll} succeeds but there are error
55 conditions pending against each file descriptor.
56
57 If no file descriptors are supplied, the callback is scheduled for immediate
58 execution and made on the empty logical vector \code{logical(0)}.
59 }
60 \note{
61 To avoid bugs due to reentrancy, by default, scheduled operations only run
62 when there is no other R code present on the execution stack; i.e., when R is
63 sitting at the top-level prompt. You can force past-due operations to run at
64 a time of your choosing by calling \code{\link[=run_now]{run_now()}}.
65
66 Error handling is not particularly well-defined and may change in the future.
67 options(error=browser) should work and errors in \code{func} should generally not
68 crash the R process, but not much else can be said about it at this point.
69 If you must have specific behavior occur in the face of errors, put error
70 handling logic inside of \code{func}.
71 }
72 \examples{
73 \dontshow{if (requireNamespace("nanonext", quietly = TRUE)) withAutoprint(\{ # examplesIf}
74 # create nanonext sockets
75 s1 <- nanonext::socket(listen = "inproc://nano")
76 s2 <- nanonext::socket(dial = "inproc://nano")
77 fd1 <- nanonext::opt(s1, "recv-fd")
78 fd2 <- nanonext::opt(s2, "recv-fd")
79
80 # 1. timeout: prints FALSE, FALSE
81 later_fd(print, c(fd1, fd2), timeout = 0.1)
82 Sys.sleep(0.2)
83 run_now()
84
85 # 2. fd1 ready: prints TRUE, FALSE
86 later_fd(print, c(fd1, fd2), timeout = 1)
87 res <- nanonext::send(s2, "msg")
88 Sys.sleep(0.1)
89 run_now()
90
91 # 3. both ready: prints TRUE, TRUE
92 res <- nanonext::send(s1, "msg")
93 later_fd(print, c(fd1, fd2), timeout = 1)
94 Sys.sleep(0.1)
95 run_now()
96
97 # 4. fd2 ready: prints FALSE, TRUE
98 res <- nanonext::recv(s1)
99 later_fd(print, c(fd1, fd2), timeout = 1)
100 Sys.sleep(0.1)
101 run_now()
102
103 # 5. fds invalid: prints NA, NA
104 close(s2)
105 close(s1)
106 later_fd(print, c(fd1, fd2), timeout = 0)
107 Sys.sleep(0.1)
108 run_now()
109 \dontshow{\}) # examplesIf}
110 }