| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/multi.R |
| 3 |
\name{multi} |
| 4 |
\alias{multi} |
| 5 |
\alias{multi_add} |
| 6 |
\alias{multi_run} |
| 7 |
\alias{multi_set} |
| 8 |
\alias{multi_list} |
| 9 |
\alias{multi_cancel} |
| 10 |
\alias{new_pool} |
| 11 |
\alias{multi_fdset} |
| 12 |
\title{Async Concurrent Requests} |
| 13 |
\usage{ |
| 14 |
multi_add(handle, done = NULL, fail = NULL, data = NULL, pool = NULL) |
| 15 |
|
| 16 |
multi_run(timeout = Inf, poll = FALSE, pool = NULL) |
| 17 |
|
| 18 |
multi_set( |
| 19 |
total_con = 50, |
| 20 |
host_con = 6, |
| 21 |
max_streams = 10, |
| 22 |
multiplex = TRUE, |
| 23 |
pool = NULL |
| 24 |
) |
| 25 |
|
| 26 |
multi_list(pool = NULL) |
| 27 |
|
| 28 |
multi_cancel(handle) |
| 29 |
|
| 30 |
new_pool(total_con = 100, host_con = 6, max_streams = 10, multiplex = TRUE) |
| 31 |
|
| 32 |
multi_fdset(pool = NULL) |
| 33 |
} |
| 34 |
\arguments{ |
| 35 |
\item{handle}{a curl \link{handle} with preconfigured \code{url} option.} |
| 36 |
|
| 37 |
\item{done}{callback function for completed request. Single argument with |
| 38 |
response data in same structure as \link{curl_fetch_memory}.} |
| 39 |
|
| 40 |
\item{fail}{callback function called on failed request. Argument contains |
| 41 |
error message.} |
| 42 |
|
| 43 |
\item{data}{(advanced) callback function, file path, or connection object for writing |
| 44 |
incoming data. This callback should only be used for \emph{streaming} applications, |
| 45 |
where small pieces of incoming data get written before the request has completed. The |
| 46 |
signature for the callback function is \code{write(data, final = FALSE)}. If set |
| 47 |
to \code{NULL} the entire response gets buffered internally and returned by in |
| 48 |
the \code{done} callback (which is usually what you want).} |
| 49 |
|
| 50 |
\item{pool}{a multi handle created by \link{new_pool}. Default uses a global pool.} |
| 51 |
|
| 52 |
\item{timeout}{max time in seconds to wait for results. Use \code{0} to poll for results without |
| 53 |
waiting at all.} |
| 54 |
|
| 55 |
\item{poll}{If \code{TRUE} then return immediately after any of the requests has completed. |
| 56 |
May also be an integer in which case it returns after n requests have completed.} |
| 57 |
|
| 58 |
\item{total_con}{max total concurrent connections.} |
| 59 |
|
| 60 |
\item{host_con}{max concurrent connections per host.} |
| 61 |
|
| 62 |
\item{max_streams}{max HTTP/2 concurrent multiplex streams per connection.} |
| 63 |
|
| 64 |
\item{multiplex}{use HTTP/2 multiplexing if supported by host and client.} |
| 65 |
} |
| 66 |
\description{ |
| 67 |
AJAX style concurrent requests, possibly using HTTP/2 multiplexing. |
| 68 |
Results are only available via callback functions. Advanced use only! |
| 69 |
For downloading many files in parallel use \link{multi_download} instead. |
| 70 |
} |
| 71 |
\details{ |
| 72 |
Requests are created in the usual way using a curl \link{handle} and added |
| 73 |
to the scheduler with \link{multi_add}. This function returns immediately |
| 74 |
and does not perform the request yet. The user needs to call \link{multi_run} |
| 75 |
which performs all scheduled requests concurrently. It returns when all |
| 76 |
requests have completed, or case of a \code{timeout} or \code{SIGINT} (e.g. |
| 77 |
if the user presses \code{ESC} or \code{CTRL+C} in the console). In case of |
| 78 |
the latter, simply call \link{multi_run} again to resume pending requests. |
| 79 |
|
| 80 |
When the request succeeded, the \code{done} callback gets triggered with |
| 81 |
the response data. The structure if this data is identical to \link{curl_fetch_memory}. |
| 82 |
When the request fails, the \code{fail} callback is triggered with an error |
| 83 |
message. Note that failure here means something went wrong in performing the |
| 84 |
request such as a connection failure, it does not check the http status code. |
| 85 |
Just like \link{curl_fetch_memory}, the user has to implement application logic. |
| 86 |
|
| 87 |
Raising an error within a callback function stops execution of that function |
| 88 |
but does not affect other requests. |
| 89 |
|
| 90 |
A single handle cannot be used for multiple simultaneous requests. However |
| 91 |
it is possible to add new requests to a pool while it is running, so you |
| 92 |
can re-use a handle within the callback of a request from that same handle. |
| 93 |
It is up to the user to make sure the same handle is not used in concurrent |
| 94 |
requests. |
| 95 |
|
| 96 |
The \link{multi_cancel} function can be used to cancel a pending request. |
| 97 |
It has no effect if the request was already completed or canceled. |
| 98 |
|
| 99 |
The \link{multi_fdset} function returns the file descriptors curl is |
| 100 |
polling currently, and also a timeout parameter, the number of |
| 101 |
milliseconds an application should wait (at most) before proceeding. It |
| 102 |
is equivalent to the \code{curl_multi_fdset} and |
| 103 |
\code{curl_multi_timeout} calls. It is handy for applications that is |
| 104 |
expecting input (or writing output) through both curl, and other file |
| 105 |
descriptors. |
| 106 |
} |
| 107 |
\examples{ |
| 108 |
results <- list() |
| 109 |
success <- function(x){ |
| 110 |
results <<- append(results, list(x)) |
| 111 |
} |
| 112 |
failure <- function(str){ |
| 113 |
cat(paste("Failed request:", str), file = stderr()) |
| 114 |
} |
| 115 |
# This handle will take longest (3sec) |
| 116 |
h1 <- new_handle(url = "https://hb.cran.dev/delay/3") |
| 117 |
multi_add(h1, done = success, fail = failure) |
| 118 |
|
| 119 |
# This handle writes data to a file |
| 120 |
con <- file("output.txt") |
| 121 |
h2 <- new_handle(url = "https://hb.cran.dev/post", postfields = "bla bla") |
| 122 |
multi_add(h2, done = success, fail = failure, data = con) |
| 123 |
|
| 124 |
# This handle raises an error |
| 125 |
h3 <- new_handle(url = "https://urldoesnotexist.xyz") |
| 126 |
multi_add(h3, done = success, fail = failure) |
| 127 |
|
| 128 |
# Actually perform the requests |
| 129 |
multi_run(timeout = 2) |
| 130 |
multi_run() |
| 131 |
|
| 132 |
# Check the file |
| 133 |
readLines("output.txt") |
| 134 |
unlink("output.txt") |
| 135 |
} |
| 136 |
\seealso{ |
| 137 |
Advanced download interface: \link{multi_download} |
| 138 |
} |