dan*_*.ch 10 python twisted bandwidth-throttling
我想设置的下载/上传文件速度的限制,发现扭曲提供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()
Run Code Online (Sandbox Code Playgroud)
UPDATE
所以在我运行之后的某个时间:
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'
Run Code Online (Sandbox Code Playgroud)
我知道我不应该像我知道的那样分配制作人tsite.protocol.producer = producer
,我是Twisted的新手,我不知道如何以另一种方式做到这一点.
每个生产者(最终)都需要注册您想要使用的数据。我在这里没有看到注册发生。也许这就是您遇到的问题?
Twisted 已被用在像 Friendster 这样的一些大型项目上,但是所有的回调都不太适合我用 python 编写的常用方式(而且我在函数式编程方面有一些经验)。我切换到gevent。
如果您正在使用 gevent 库,则许多细节(提供异步功能的回调/生成器)都会被抽象出来,因此您通常可以只需要猴子修补代码并以通常的面向对象风格编写它即可。用于。如果您正在与任何不熟悉像 js/lisp 这样的回调密集型语言的人一起开发一个项目,我敢打赌他们会更喜欢 gevent 而不是twisted。
归档时间: |
|
查看次数: |
1853 次 |
最近记录: |