本文整理汇总了Python中twisted.protocols.basic.FileSender.resumeProducing方法的典型用法代码示例。如果您正苦于以下问题:Python FileSender.resumeProducing方法的具体用法?Python FileSender.resumeProducing怎么用?Python FileSender.resumeProducing使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.protocols.basic.FileSender
的用法示例。
在下文中一共展示了FileSender.resumeProducing方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FileReader
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import resumeProducing [as 别名]
class FileReader(AccumulatingProtocol):
def __init__(self, filename, transform=None, delay=0, verbose=False):
self.f = open(filename, 'rb')
self.transform = transform
self.delay = delay
self.producer = FileSender()
self.logger = gogo.Gogo(__name__, verbose=verbose).logger
def cleanup(self, *args):
self.f.close()
self.producer.stopProducing()
def resumeProducing(self):
chunk = self.file.read(self.CHUNK_SIZE) if self.file else ''
if not chunk:
self.file = None
self.consumer.unregisterProducer()
if self.deferred and self.delay:
callLater(self.delay, self.deferred.callback, self.lastSent)
elif self.deferred:
self.deferred.callback(self.lastSent)
self.deferred = None
return
def connectionLost(self, reason):
self.logger.debug('connectionLost: %s', reason)
self.cleanup()
def connectionMade(self):
self.logger.debug('Connection made from %s', self.transport.getPeer())
args = (self.f, self.transport, self.transform)
self.d = self.closedDeferred = self.producer.beginFileTransfer(*args)
while not self.d.called:
self.producer.resumeProducing()
self.d.addErrback(self.logger.error)
self.d.addBoth(self.cleanup)