问题
Is it possible to open a communication stream between two R sessions on two different computers?
I use sockets to connect sessions if both are on the same computer. I suppose with two different computers, I should try web sockets. httpuv
supports R as a web socket server, but unfortunately, I could not find any up-to-date package that supports a client web socket in R.
I'm not tied to using web sockets. Any solution that enables communicating between computers in a real time manner would work.
回答1:
I have used sockets to communicate between computers using R.
server example:
import socket
server <- function(){
while(TRUE){
writeLines("Listening...")
con <- socketConnection(host="localhost", port = 6011, blocking=TRUE,
server=TRUE, open="r+")
data <- readLines(con, 1)
print(data)
response <- toupper(data)
writeLines(response, con)
close(con)
}
}
server()
client example:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 6011))
while 1:
data = raw_input ( "Enter text to be upper-cased, q to quit\n" )
client_socket.send(data)
if ( data == 'q' or data == 'Q'):
client_socket.close()
break;
else:
data = client_socket.recv(5000)
print "Your upper cased text: " , data
来源:https://stackoverflow.com/questions/35849356/how-to-connect-two-computers-using-r