[Twisted-Python] Request for help: throttling serving of static content

Peter Inglesby peter.inglesby at gmail.com
Mon Nov 5 16:12:27 EST 2012


Tomorrow I'm giving a talk to provide an introduction to asynchronous
programming.  I'll be mentioning Twisted in passing, but the meat of the
talk will be about writing a web crawler for finding broken links, first
using single-threaded blocking code, then multi-threaded blocking code,
before demonstrating a non-blocking solution using a select loop.  I can
make the code and slides available if there's interest.

For this to be effective, I was planning on writing a very slow webserver
to serve static content locally.  I'd expected I'd be able to simply wrap
an instance of twisted.web.static.Site in a ThrottlingFactory, and serve
away:

import sys

from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.protocols.policies import ThrottlingFactory
from twisted.web.server import Site
from twisted.web.static import File
from twisted.python import log

if __name__ == '__main__':
    log.startLogging(sys.stdout)
    resource = File(sys.argv[1])
    factory = Site(resource)
    endpoint = TCP4ServerEndpoint(reactor, 4321)
    endpoint.listen(ThrottlingFactory(factory, maxConnectionCount=3,
readLimit=20, writeLimit=20))
    reactor.run()



However, this fails with the following error because I've not registered a
producer with my ThrottlingProtocol:

2012-11-05 21:01:34+0000 [-] Throttling writes on
<twisted.protocols.policies.ThrottlingFactory instance at 0x1016497e8>
2012-11-05 21:01:34+0000 [-] Unhandled Error
 Traceback (most recent call last):
  File "slow_server.py", line 16, in <module>
    reactor.run()
  File
"/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/internet/base.py",
line 1169, in run
    self.mainLoop()
  File
"/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/internet/base.py",
line 1178, in mainLoop
    self.runUntilCurrent()
--- <exception caught here> ---
  File
"/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/internet/base.py",
line 800, in runUntilCurrent
    call.func(*call.args, **call.kw)
  File
"/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/protocols/policies.py",
line 290, in checkWriteBandwidth
    self.throttleWrites()
  File
"/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/protocols/policies.py",
line 324, in throttleWrites
    p.throttleWrites()
  File
"/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/protocols/policies.py",
line 221, in throttleWrites
    self.producer.pauseProducing()
exceptions.AttributeError: 'NoneType' object has no attribute
'pauseProducing'



I'm sure it's not impossible to figure out what I need to do here, but I'd
appreciate any pointers.

Many thanks in advance!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20121105/d5e70c4b/attachment.htm 


More information about the Twisted-Python mailing list