EVOLUTION-MANAGER
Edit File: processx_fifos.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Processx FIFOs</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for conn_create_fifo {processx}"><tr><td>conn_create_fifo {processx}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Processx FIFOs</h2> <h3>Description</h3> <p><a href="https://lifecycle.r-lib.org/articles/stages.html#experimental"><img src="../help/figures/lifecycle-experimental.svg" alt='[Experimental]' /></a> </p> <p>Create a FIFO for inter-process communication Note that these functions are currently experimental. </p> <h3>Usage</h3> <pre> conn_create_fifo( filename = NULL, read = NULL, write = NULL, encoding = "", nonblocking = TRUE ) conn_connect_fifo( filename, read = NULL, write = NULL, encoding = "", nonblocking = TRUE ) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>filename</code></td> <td> <p>File name of the FIFO. On Windows it the name of the pipe within the <code style="white-space: pre;">\\?\pipe\</code> namespace, either the full name, or the part after that prefix. If <code>NULL</code>, then a random name is used, on Unix in the R temporary directory: <code><a href="../../base/html/tempfile.html">base::tempdir()</a></code>.</p> </td></tr> <tr valign="top"><td><code>read</code></td> <td> <p>If <code>TRUE</code> then connect to the read end of the FIFO. Exactly one of <code>read</code> and <code>write</code> must be set to <code>TRUE</code>.</p> </td></tr> <tr valign="top"><td><code>write</code></td> <td> <p>If <code>TRUE</code> then connect to the write end of the FIFO. Exactly one of <code>read</code> and <code>write</code> must be set to <code>TRUE</code>.</p> </td></tr> <tr valign="top"><td><code>encoding</code></td> <td> <p>Encoding to assume.</p> </td></tr> <tr valign="top"><td><code>nonblocking</code></td> <td> <p>Whether this should be a non-blocking FIFO. Note that blocking FIFOs are not well tested and might not work well with <code><a href="poll.html">poll()</a></code>, especially on Windows. We might remove this option in the future and make all FIFOs non-blocking.</p> </td></tr> </table> <h3>Details</h3> <p><code>conn_create_fifo()</code> creates a FIFO and connects to it. On Unix this is a proper FIFO in the file system, in the R temporary directory. On Windows it is a named pipe. </p> <p>Use <code><a href="processx_connections.html">conn_file_name()</a></code> to query the name of the FIFO, and <code>conn_connect_fifo()</code> to connect to the other end. </p> <p><code>conn_connect_fifo()</code> connects to a FIFO created with <code>conn_create_fifo()</code>, typically in another process. <code>filename</code> refers to the name of the pipe on Windows. </p> <p>On Windows, <code>conn_connect_fifo()</code> may be successful even if the FIFO does not exist, but then later <code>poll()</code> or read/write operations will fail. We are planning on changing this behavior in the future, to make <code>conn_connect_fifo()</code> fail immediately, like on Unix. </p> <h3>Notes</h3> <h4>In general Unix domain sockets work better than FIFOs, so we suggest</h4> <p>you use sockets if you can. See <code><a href="processx_sockets.html">conn_create_unix_socket()</a></code>. </p> <h4>Creating the read end of the FIFO</h4> <p>This case is simpler. To wait for a writer to connect to the FIFO you can use <code><a href="poll.html">poll()</a></code> as usual. Then use <code><a href="processx_connections.html">conn_read_chars()</a></code> or <code><a href="processx_connections.html">conn_read_lines()</a></code> to read from the FIFO, as usual. Use <code><a href="processx_connections.html">conn_is_incomplete()</a></code> <em>after</em> a read to check if there is more data, or the writer is done. </p> <h4>Creating the write end of the FIFO</h4> <p>This is somewhat trickier. Creating the (non-blocking) FIFO does not block. However, there is no easy way to tell if a reader is connected to the other end of the FIFO or not. On Unix you can start using <code><a href="processx_connections.html">conn_write()</a></code> to try to write to it, and this will succeed, until the buffer gets full, even if there is no reader. (When the buffer is full it will return the data that was not written, as usual.) </p> <p>On Windows, using <code><a href="processx_connections.html">conn_write()</a></code> to write to a FIFO without a reader fails with an error. This is not great, we are planning to improve it later. </p> <p>Right now, one workaround for this behavior is for the reader to connunicate to the writer process independenctly that it has connected to the FIFO. (E.g. another FIFO in the opposite direction can do that.) </p> <h3>See Also</h3> <p><a href="https://processx.r-lib.org/dev/articles/internals.html">processx internals</a> </p> <h3>Examples</h3> <pre> # Example for a non-blocking FIFO # Need to open the reading end first, otherwise Unix fails reader <- conn_create_fifo() # Always use poll() before you read, with a timeout if you like. # If you read before the other end of the FIFO is connected, then # the OS (or processx?) assumes that the FIFO is done, and you cannot # read anything. # Now poll() tells us that there is no data yet. poll(list(reader), 0) writer <- conn_connect_fifo(conn_file_name(reader), write = TRUE) conn_write(writer, "hello\nthere!\n") poll(list(reader), 1000) conn_read_lines(reader, 1) conn_read_chars(reader) conn_is_incomplete(reader) close(writer) conn_read_chars(reader) conn_is_incomplete(reader) close(reader) </pre> <hr /><div style="text-align: center;">[Package <em>processx</em> version 3.8.0 <a href="00Index.html">Index</a>]</div> </body></html>