Streaming Communication Model

Implement services in Dubbo Python using client streaming, server streaming, and bidirectional streaming models.

See the full example here

Dubbo-Python supports streaming calls, including ClientStream, ServerStream, and BidirectionalStream modes.

Streaming calls can be divided into write-streams and read-streams. For ClientStream, it’s multiple writes with a single read; for ServerStream, a single write with multiple reads; and BidirectionalStream allows multiple writes and reads.

Write-Stream

Write operations in streaming calls can be divided into single write (ServerStream) and multiple writes (ClientStream and BidirectionalStream).

Single Write

Single write calls are similar to unary mode. For example:

  1. stub.server_stream(greeter_pb2.GreeterRequest(name="hello world from dubbo-python"))

Multiple Writes

For multiple writes, users can write data using either an iterator or writeStream (only one of these options should be used).

  1. Iterator-based Write: Writing via iterator is similar to unary mode, with the main difference being the use of an iterator for multiple writes. For example:

    1. # Use an iterator to send multiple requests
    2. def request_generator():
    3. for i in ["hello", "world", "from", "dubbo-python"]:
    4. yield greeter_pb2.GreeterRequest(name=str(i))
    5. # Call the remote method and return a read_stream
    6. stream = stub.client_stream(request_generator())
  2. Using writeStream: This method requires an empty argument, after which data is written incrementally using write, and done_writing is called to end the write-stream. For example:

    1. stream = stub.bi_stream()
    2. # Use the write method to send messages
    3. stream.write(greeter_pb2.GreeterRequest(name="jock"))
    4. stream.write(greeter_pb2.GreeterRequest(name="jane"))
    5. stream.write(greeter_pb2.GreeterRequest(name="alice"))
    6. stream.write(greeter_pb2.GreeterRequest(name="dave"))
    7. # Call done_writing to notify the server that the client has finished writing
    8. stream.done_writing()

Read-Stream

Read operations for streaming calls can be single read (ClientStream) or multiple reads (ServerStream and BidirectionalStream). A ReadStream is returned in all cases, and data can be read using the read method or an iterator. When using read, please note:

  1. The read method supports a timeout parameter (in seconds).
  2. The read method can return one of three values: the expected data, None (timeout exceeded), or EOF (end of the read-stream).

Single Read

A single call to the read method will retrieve the data, for example:

  1. result = stream.read()
  2. print(f"Received response: {result.message}")

Multiple Reads

Multiple reads can be done by repeatedly calling read, with handling for None and EOF values. Since ReadStream implements __iter__ and __next__, an iterator-based approach can also be used, which automatically handles these values but doesn’t support a timeout.

  1. Using Iterator (Recommended):

    1. def client_stream(self, request_iterator):
    2. response = ""
    3. for request in request_iterator:
    4. print(f"Received request: {request.name}")
    5. response += f"{request.name} "
    6. return greeter_pb2.GreeterReply(message=response)
  2. Multiple Calls to read Method:

    1. # Use read method to receive messages
    2. # If no message arrives within the specified time, returns None
    3. # If the server has finished sending messages, returns EOF
    4. while True:
    5. i = stream.read(timeout=0.5)
    6. if i is dubbo.classes.EOF:
    7. break
    8. elif i is None:
    9. print("No message received")
    10. continue
    11. print(f"Received response: {i.message}")

Feedback

Was this page helpful?

Yes No

Last modified November 7, 2024: docs: add python sdk manual (#3056) (26c58b388ff)