【问题标题】:Reading file to stdout with twisted使用扭曲将文件读取到标准输出
【发布时间】:2015-05-11 11:03:45
【问题描述】:

我们如何读取文件(非阻塞)并将其打印到标准输出(仍然非阻塞)?这是我能想到的最简单的方法,但它让你感觉一定有更好的方法。暴露一些 LineReceiver 的东西——比如逐行修改——功能会更受欢迎。

from twisted.internet import stdio, protocol
from twisted.protocols.basic import FileSender
from twisted.internet import reactor

class FileReader(protocol.Protocol):
    def connectionMade(self):
        fl = open('myflie.txt', 'rb')
        d = FileSender().beginFileTransfer(fl, self.transport)
        d.addBoth(fl.close)
        d.addBoth(lambda _: reactor.stop())

stdio.StandardIO(FileReader())
reactor.run()

【问题讨论】:

标签: python asynchronous twisted


【解决方案1】:

这是 Twisted 的一个弱点。异步文件 I/O 很难做到,而且可能不可能做到“正确”。有一张票已经开放了很长时间:https://twistedmatrix.com/trac/ticket/3983,您可能会找到一个有用的地方继续讨论。

您在那里使用的成语肯定是我们目前提供的最接近正确的成语。

【讨论】:

  • 感谢@Glyph。非常尊重Twisted。大量出色而艰苦的工作。
猜你喜欢
  • 2013-02-24
  • 1970-01-01
  • 2013-03-18
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多