【问题标题】:Buffer app using twisted/asyncio使用 twisted/asyncio 缓冲应用程序
【发布时间】:2018-04-25 14:46:43
【问题描述】:

我有一个简单的应用程序,它接收数据并且必须将其分批推送到另一个服务。

如何使用 twisted 或 asyncio 实现它?

目前,我有下一个使用扭曲的代码:

from twisted.internet import protocol, reactor, endpoints
from twisted.protocols import basic


class FirehoseProtocol(basic.LineReceiver):
    def __init__(self):
        self.data = []

    def lineReceived(self, line):
        self.data.append(line)

    def push_to_firehose(self):
        pass  # TODO


class EchoFactory(protocol.ServerFactory):
    protocol = FirehoseProtocol


endpoints.serverFromString(reactor, "tcp:5001").listen(EchoFactory())

reactor.run()

【问题讨论】:

    标签: python python-3.x twisted python-asyncio


    【解决方案1】:

    你没有说什么定义了批次。如果我们假设多行定义一个批次:

    def lineReceived(self, line):
        self.data.append(line)
        if len(self.data) == batch_size:
            self.push_to_firehose()
    

    【讨论】:

    • 确切地说,批处理意味着 N 行,其中 N 是动态的,将根据行大小进行计算。但我假设它会阻止接收新行,对吧?该应用程序需要非常高负载,因此它必须每秒至少提供 300 条记录。
    • 您认为什么会阻止? Twisted 中的 API 不会阻塞。这就是 Twisted 的意义所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多