gpt4 book ai didi

python - 使用 Twisted 进行带宽限制

转载 作者:太空狗 更新时间:2023-10-29 19:28:43 24 4
gpt4 key购买 nike

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

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 的新手,我不知道如何做方式。

最佳答案

Every producer needs (eventually) to be registered with whatever you want to consume the data .我在这里的任何地方都看不到注册。也许这就是您遇到的问题?

Twisted 已用于 Friendster 等一些大型项目,但所有回调都不适合我用 Python 编写的通常方式(我有一些函数式编程经验)。我切换到gevent .

如果您正在使用 gevent 库,许多细节(提供异步功能的回调/生成器)都被抽象出来,因此您通常只需猴子修补您的代码并将其写入通常的对象 -你习惯的导向风格。如果您正在与任何不熟悉 js/lisp 等重回调语言的人一起进行项目,我敢打赌他们会更喜欢 gevent 而不是 twisted。

关于python - 使用 Twisted 进行带宽限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13047458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com