conn_create_fifo | R Documentation |
Create a FIFO for inter-process communication Note that these functions are currently experimental.
conn_create_fifo(
filename = NULL,
read = NULL,
write = NULL,
encoding = "",
nonblocking = TRUE
)
conn_connect_fifo(
filename,
read = NULL,
write = NULL,
encoding = "",
nonblocking = TRUE
)
filename |
File name of the FIFO. On Windows it the name of the
pipe within the |
read |
If |
write |
If |
encoding |
Encoding to assume. |
nonblocking |
Whether this should be a non-blocking FIFO.
Note that blocking FIFOs are not well tested and might not work well with
|
conn_create_fifo()
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.
Use conn_file_name()
to query the name of the FIFO, and
conn_connect_fifo()
to connect to the other end.
conn_connect_fifo()
connects to a FIFO created with
conn_create_fifo()
, typically in another process. filename
refers
to the name of the pipe on Windows.
On Windows, conn_connect_fifo()
may be successful even if the
FIFO does not exist, but then later poll()
or read/write operations
will fail. We are planning on changing this behavior in the future,
to make conn_connect_fifo()
fail immediately, like on Unix.
you use sockets if you can. See conn_create_unix_socket()
.
This case is simpler. To wait for a writer to connect to the FIFO
you can use poll()
as usual. Then use conn_read_chars()
or
conn_read_lines()
to read from the FIFO, as usual. Use
conn_is_incomplete()
after a read to check if there is more data,
or the writer is done.
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
conn_write()
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.)
On Windows, using conn_write()
to write to a FIFO without a reader
fails with an error. This is not great, we are planning to improve it
later.
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.)
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.