You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ZeroRPC Python服务端与Node客户端流传输故障排查求助

解决ZeroRPC Python服务端与Node客户端的流传输(发布/订阅)问题

Hey, I’ve dealt with ZeroRPC’s sparse docs and tricky streaming behavior before—let’s walk through fixing your issue where only the first message shows up. Here’s the breakdown of your questions and the fixes:

1. Python服务端:必须持续生成流数据(是的,你需要主动推送更多值)

Your hunch is right—your Python service needs to keep emitting values to maintain the stream. ZeroRPC recognizes methods that use yield as streaming endpoints, so you can’t just return a single value and call it done.

Here’s a corrected example for your PublishWhatever method, simulating continuous pub/sub-style data:

import zerorpc
import time

class PubSubService:
    def PublishWhatever(self):
        # 模拟持续产生的订阅数据,比如从队列、数据库或实时事件获取
        stream_messages = ["love", "kindness", "courage", "curiosity"]
        for msg in stream_messages:
            yield f"STREAM {msg}"
            time.sleep(1)  # 模拟真实场景的间隔推送

if __name__ == "__main__":
    server = zerorpc.Server(PubSubService())
    server.bind("tcp://0.0.0.0:4242")
    server.run()

The yield keyword tells ZeroRPC to keep the connection open and send each subsequent value as it’s generated. Without this, the stream closes after the first value is sent.

2. Node客户端:无需手动递归,只需监听流的more标记

You don’t need to write recursive functions on the Node side—ZeroRPC handles the streaming under the hood via the callback’s more parameter. Your client just needs to react to each data chunk and know when the stream ends.

Here’s the fixed Node client code:

const zerorpc = require("zerorpc");

const client = new zerorpc.Client();
client.connect("tcp://127.0.0.1:4242");

client.invoke("PublishWhatever", (err, data, more) => {
    if (err) {
        console.error("Stream error:", err);
        client.close();
        return;
    }

    console.log(data); // 打印每次收到的流数据

    // When `more` is false, the stream is finished—clean up the client
    if (!more) {
        console.log("Stream completed");
        client.close();
    }
});

The more flag is critical: when it’s true, ZeroRPC will automatically trigger the callback again for the next data chunk. When it’s false, you know the stream has ended and can close the connection.

Quick Recap of ZeroRPC Streaming for Pub/Sub

ZeroRPC’s streaming works like a one-way data pipeline:

  • Python services use yield to produce a continuous sequence of values
  • Node clients use the invoke callback with the more parameter to consume each value as it arrives
  • If either side stops producing/consuming correctly, the stream drops early (which is exactly what happened in your code!)

内容的提问来源于stack exchange,提问作者Eric Burel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:09:52