10

我正在尝试设置下载/上传文件的速度限制,发现twisted 提供了twisted.protocols.policies.ThrottlingFactory来处理这项工作,但我做错了。我设置了readLimitand writeLimit,但文件仍在以最大速度下载。我究竟做错了什么?

from twisted.protocols.basic import FileSender
from twisted.protocols.policies import ThrottlingFactory
from twisted.web import server, resource
from twisted.internet import reactor
import os

class DownloadPage(resource.Resource):
    isLeaf = True

    def __init__(self, producer):
        self.producer = producer

    def render(self, request):
        size = os.stat(somefile).st_size
        request.setHeader('Content-Type', 'application/octet-stream')
        request.setHeader('Content-Length', size)
        request.setHeader('Content-Disposition', 'attachment; filename="' + somefile + '"')
        request.setHeader('Accept-Ranges', 'bytes')

        fp = open(somefile, 'rb')
        d = self.producer.beginFileTransfer(fp, request)

        def err(error):
            print "error %s", error

        def cbFinished(ignored):
            fp.close()
            request.finish()
        d.addErrback(err).addCallback(cbFinished)

        return server.NOT_DONE_YET


producer = FileSender()
root_resource = resource.Resource()
root_resource.putChild('download', DownloadPage(producer))
site = server.Site(root_resource)
tsite = ThrottlingFactory(site, readLimit=10000, writeLimit=10000)
tsite.protocol.producer = producer
reactor.listenTCP(8080, tsite)
reactor.run()

更新

所以在我运行它之后的某个时候:

2012-10-25 09:17:03+0600 [-] Unhandled Error
Traceback (most recent call last):
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/application/app.py", line 402, in startReactor
        self.config, oldstdout, oldstderr, self.profiler, reactor)
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/application/app.py", line 323, in runReactorWithLogging
        reactor.run()
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1169, in run
        self.mainLoop()
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1178, in mainLoop
        self.runUntilCurrent()
    --- <exception caught here> ---
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/internet/base.py", line 800, in runUntilCurrent
        call.func(*call.args, **call.kw)
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/protocols/policies.py", line 334, in unthrottleWrites
        p.unthrottleWrites()
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/protocols/policies.py", line 225, in unthrottleWrites
        self.producer.resumeProducing()
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/protocols/basic.py", line 919, in resumeProducing
        self.consumer.unregisterProducer()
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/web/http.py", line 811, in unregisterProducer
        self.transport.unregisterProducer()
      File "/home/chambylov/environments/transfer/local/lib/python2.7/site-packages/twisted/protocols/policies.py", line 209, in unregisterProducer
        del self.producer
    exceptions.AttributeError: ThrottlingProtocol instance has no attribute 'producer'

我知道我不应该像我知道的那样分配生产者tsite.protocol.producer = producer,我是 Twisted 的新手,我不知道如何以另一种方式做到这一点。

4

2 回答 2

1

每个生产者都需要(最终)注册您想要使用数据的任何内容。我在这里看不到任何地方的注册。也许这就是你遇到的问题?

Twisted 已经在 Friendster 等一些大型项目中使用过,但是所有的回调都不适合我用 python 编写的通常方式(而且我有一些函数式编程经验)。我切换到gevent

如果您正在使用 gevent 库,则许多细节(提供异步功能的回调/生成器)都被抽象出来,因此您通常可以只用猴子修补您的代码并以您通常的面向对象风格编写它用于。如果您正在与不熟悉 js/lisp 之类的大量回调语言的人一起进行项目,我敢打赌他们会喜欢 gevent 而不是 twisted。

于 2012-11-30T14:51:32.143 回答
1

正如egbutter所说,您必须注册生产者。所以代替这个:

tsite.protocol.producer = producer

您必须显式调用registerProducer方法:

tsite.protocol.registerProducer( ... )

或者,如果您使用FileSender作为生产者,调用它的beginFileTransfer方法,在我们的例子中:

file_to_send = open( ... )
producer.beginFileTransfer(file_to_send, tsite.protocol)
于 2013-09-20T11:09:22.173 回答