python - 使用 twisted/asyncio 缓冲应用程序
问题描述
我有一个简单的应用程序,它接收数据并且必须将其分批推送到另一个服务。
如何使用 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()
解决方案
您没有说什么定义了批次。如果我们假设多行定义一个批次:
def lineReceived(self, line):
self.data.append(line)
if len(self.data) == batch_size:
self.push_to_firehose()
推荐阅读
- javascript - 如何将地图结果传递给 $.ajax jQuery 函数
- postgresql - 如何将我的分钟数据划分为包含 Timescaledb 中每个月的表(PostgreSQL 扩展)
- python - 数数美国拍的电影。IndexError:列表索引超出范围
- c++ - 普通的右值引用和 std::forward 返回的有什么区别?
- jenkins - 在 jenkinsfile 中获取 github 有效负载
- ios - 如何在本机反应中使用 on-press 打开另一个文件
- javascript - 为什么我应该使用 uber 的 fusion.js 而不是 create-react-app?
- git - git filter-branch 错误:在寻找匹配的“”时出现意外的 EOF
- asp.net - CustomValidator 设置为 false,但表单仍在提交
- javascript - 在formdata中添加图像并在webservice javascript中使用