接收流时中断 python grpc 客户端
Interrupt python grpc client when receiving stream
我正在玩 gRPC,我不知道如何在接收流时关闭客户端和服务器之间的连接。客户端和服务端都写成Python.
例如,我的服务器从队列中读取消息并生成每条消息。我的想法是客户端订阅服务器并开始接收这些消息。
我的问题是:
- 我想在按下 CTRL+C 时终止客户端,但它卡在当前代码中。怎么才能做好呢?
- 服务端如何知道客户端已经停止监听?
我的 nbi.proto 文件:
syntax = "proto3";
service Messenger {
rpc subscribe(Null) return (stream Message) {}
}
message Null {}
message Message {
string value = 1;
}
Python 客户:
import test_pb2_grpc as test_grpc
import test_pb2 as test
import grpc
def run():
channel = grpc.insecure_channel('localhost:50051')
stub = test_grpc.MessengerStub(channel)
stream = stub.subscribe(test.Null())
try:
for e in stream:
print e
except grpc._channel._Rendezvous as err:
print err
except KeyboardInterrupt:
stub.unsuscribe(test.Null)
Python 服务器:
import test_pb2_grpc as test_grpc
import test_pb2 as test
from Queue import Empty
import grpc
class Messenger(test_grpc.MessengerServicer):
def __init__(self, queue):
self.queue = queue
def subscribe(self, request, context):
while True:
try:
yield self.queue.get(block=True, timeout=0.1)
except Empty:
continue
except Exception as e:
logger.error(e)
break
return
I would like to get the client killed whenever CTRL+C is pressed, but
it gets stuck with the current code. How can it be done properly?
KeyboardInterrupt
应该足以终止客户端应用程序。大概进程挂在了stub.unsubscribe
。如果您使用客户端断开回调,也许您不需要显式取消订阅。
How does the server realize that the client has stopped listening?
您可以 add a callback to the context object 将其传递给您的 Messenger.subscribe
方法。回调在客户端断开连接时调用。
顺便说一下,您可以使用 empty.proto 代替 Null
类型。
我正在玩 gRPC,我不知道如何在接收流时关闭客户端和服务器之间的连接。客户端和服务端都写成Python.
例如,我的服务器从队列中读取消息并生成每条消息。我的想法是客户端订阅服务器并开始接收这些消息。
我的问题是:
- 我想在按下 CTRL+C 时终止客户端,但它卡在当前代码中。怎么才能做好呢?
- 服务端如何知道客户端已经停止监听?
我的 nbi.proto 文件:
syntax = "proto3";
service Messenger {
rpc subscribe(Null) return (stream Message) {}
}
message Null {}
message Message {
string value = 1;
}
Python 客户:
import test_pb2_grpc as test_grpc
import test_pb2 as test
import grpc
def run():
channel = grpc.insecure_channel('localhost:50051')
stub = test_grpc.MessengerStub(channel)
stream = stub.subscribe(test.Null())
try:
for e in stream:
print e
except grpc._channel._Rendezvous as err:
print err
except KeyboardInterrupt:
stub.unsuscribe(test.Null)
Python 服务器:
import test_pb2_grpc as test_grpc
import test_pb2 as test
from Queue import Empty
import grpc
class Messenger(test_grpc.MessengerServicer):
def __init__(self, queue):
self.queue = queue
def subscribe(self, request, context):
while True:
try:
yield self.queue.get(block=True, timeout=0.1)
except Empty:
continue
except Exception as e:
logger.error(e)
break
return
I would like to get the client killed whenever CTRL+C is pressed, but it gets stuck with the current code. How can it be done properly?
KeyboardInterrupt
应该足以终止客户端应用程序。大概进程挂在了stub.unsubscribe
。如果您使用客户端断开回调,也许您不需要显式取消订阅。
How does the server realize that the client has stopped listening?
您可以 add a callback to the context object 将其传递给您的 Messenger.subscribe
方法。回调在客户端断开连接时调用。
顺便说一下,您可以使用 empty.proto 代替 Null
类型。