我正在尝试设置下载/上传文件的速度限制,并发现twisted提供了twisted.protocols.policies.ThrottlingFactory来处理这项工作,但我无法做到正确。我设置了readLimit
和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的新手,我不知道如何以另一种方式做到这一点。
答案 0 :(得分:1)
Every producer needs (eventually) to be registered with whatever you want to consume the data。我没有看到在这里任何地方发生注册。也许这就是你遇到的问题?
Twisted已经被用在像Friendster这样的大型项目中,但是所有的回调都与我在python中编写的常规方式不相符(我有一些函数式编程经验)。我切换到gevent。
如果您正在使用gevent库,那么许多细节(提供异步功能的回调/生成器)都会被抽象出来,这样您通常可以通过猴子修补代码并将其写入通常的对象中来实现 - 你习惯的导向风格。如果你正在与一个不熟悉js / lisp等回调密集语言的人一起开展一个项目,我敢打赌他们会喜欢gevent over twisted。
答案 1 :(得分:1)
正如egbutter所说,你必须注册一个制作人。所以不要这样:
tsite.protocol.producer = producer
您必须明确调用 registerProducer 方法:
tsite.protocol.registerProducer( ... )
或者,如果您使用 FileSender 作为制作人,请在我们的案例中调用 beginFileTransfer 方法:
file_to_send = open( ... )
producer.beginFileTransfer(file_to_send, tsite.protocol)