英文:
When making a go RPC call , the return type is channel
问题
首先,这是一个中华人民共和国的服务器。请注意其中一个返回类型是chan
:
func (c *Coordinator) FetchTask() (*chan string, error) {
// ...
return &reply, nil
}
然后客户端发起一个RPC调用。通常调用者会得到一个类型为*chan string
的通道。
call("Coordinator.FetchTask", &args, &reply)
这是我的问题。如果服务器不断向通道中写入数据:
for i := 0; i < 100; i++ {
reply <- strconv.Itoa(i)
}
客户端能够连续从通道中读取数据吗?
for {
var s string = <-reply
}
我猜客户端不能,因为服务器和客户端不在同一内存中。它们通过互联网进行通信。因此,即使变量reply
是一个指针,它在服务器和客户端中指向不同的地址。
我对此不太确定。你对此有什么看法?非常感谢!
顺便问一下,有没有办法在服务器和客户端之间实现一个真正的有状态通道?
英文:
Firstly, here is a PRC server. Please notice one of the return type is chan
:
func (c *Coordinator) FetchTask() (*chan string, error) {
// ...
return &reply, nil
}
Then the client makes a RPC call. Typically the caller will get a channel which type is *chan string
.
call("Coordinator.FecthTask", &args, &reply)
Here is my question. If the server continuously write into the channel:
for i := 0; i < 100; i++ {
reply<- strconv.Itoa(i)
}
Can the client continuously read read from the channel?
for {
var s string = <-reply
}
I guess the client can't, cuz server and client are not in the same memory. They communicate via Internet. Therefore, even the variable reply
is a pointer, it points different address in server and client.
I'am not sure about it. What do you think of it? Thanks a lot!!!!
BTW, is there anyway to implement a REAL, stateful channel between server and client?
答案1
得分: 2
正如你已经提到的,通道是内存变量,无法在其他应用程序或系统中使用。另一方面,gRPC将传递和解析二进制数据,在这种情况下,再次传递通道指针,只会返回服务器内存中的指针地址。客户端接收到该地址后,将尝试指向本地机器内存中的该地址,这可能是任何类型的数据,不幸的是。
如果你想传递一组数据(比如一个字符串数组),你可以使用服务器流式传输或双向流式传输。
另一方面,如果你想实现一种稳定的、保持连接的方式,你也可以考虑使用WebSockets。
英文:
As you already mentioned, channels are in memory variables and it is not possible to use them in other apps or systems. In the other hand gRPC will pass and parse binary data which in this case again passing a channel pointer, will only returns the pointer address in server's memory. After client receiving that address it will try to point to that address in local machine's memory which will can be any sort of data unfortunately.
If you want to push a group of data (let's say an array of strings) you can use a Server streaming or Bidirectional streaming.
In the other hand if you want to accomplish some sort of stable and keep-alive connection you can consider websockets too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论