[Twisted-Python] tracebacks in twisted web2

Paul Perez paul at paulperez.net
Tue Sep 25 22:04:10 EDT 2007

Dear List:

I am having a heck of a time trying to figure out why this traceback is
happening, and I was hoping that you could shed some light on the situation.
I am sorry for bothering you with this simple thing, but I can't seem to
figure it out!

The code and traceback are below. The traceback only happens when I call
httprequest.writeResponse(httpresponse) from a callback. When the same code
is called inline and not in a deferred, it works just fine. Even stranger is
that the response is written to the client even though the traceback occurs.

An explanation of what I am trying to do:

A consumer requests data. If there is data in the queue when the consumer
first checks, return the result right away. If there is no data present,
return a deferred and wait. When the producer inserts data into the queue,
check if there is a consumer waiting. If so, begin callback chain on

The code works fine (no traceback) in the case where there is no deferred
needed (in QueueCache.maybedefer). When there is a deferred, the traceback
happens during the callback (in QueueCache.put). Again, the consumer
actually gets the correct result, but the traceback still occurs!

Any ideas?

Thanks in advance!


from twisted.internet import reactor, defer
from twisted.web2     import http, responsecode
import Queue


def respond(result, httprequest):
    httpresponse = http.Response(responsecode.OK, {}, stream=result)

class QueueCache(object):
    def __init__(self):
        self.queue = {}
        self.defer = {}

    def get_data(self, xSig):
        return self.get_queue(xSig).get_nowait()

    def push_data(self, xSig, value):

    def get_queue(self, xSig):
            return self.queue[xSig]
        except KeyError:
            q = Queue.Queue()
            self.queue[xSig] = q
            return q

    def defer_request(self, xSig, httprequest):
        if self.defer.has_key(xSig):
            raise Exception("Client %s already has a deferred request!"

        def timeout(deferred, request):
            xSig = request.args['X-Signature'][0]
            except KeyError:
                print 'Timeout called and xSig `%s` was not in cache!' %

        d = defer.Deferred()
        d.addCallback(respond, httprequest)
        d.setTimeout(TIMEOUT, timeout, httprequest)
        self.defer[xSig] = d
        return d

    def put(self, data):
        xSig = data['X-Signature']
            d = self.defer.pop(xSig)
        except KeyError:

    def maybedefer(self, httprequest):
        xSig = httprequest.args['X-Signature'][0]
            data = self.get_data(xSig)
            result = http.Response(
        except Queue.Empty:
            result = self.defer_request(xSig, httprequest)
        return result

2007-09-25 18:38:38-0700 [-] Original exception:
2007-09-25 18:38:38-0700 [-] Unhandled Error
        Traceback (most recent call last):
          File "C:\Projects\PyServer\twisted\internet\defer.py", line 304,
in _startRunCallbacks

          File "C:\Projects\PyServer\twisted\internet\defer.py", line 317,
in _runCallbacks

          File "C:\Projects\PyServer\twisted\internet\defer.py", line 281,
in _continue

          File "C:\Projects\PyServer\twisted\internet\defer.py", line 277,
in unpause

        --- <exception caught here> ---
          File "C:\Projects\PyServer\twisted\internet\defer.py", line 317,
in _runCallbacks

          File "C:\Projects\PyServer\twisted\web2\server.py", line 518, in

        exceptions.TypeError: html is not a resource or a response

2007-09-25 18:38:39-0700 [-] Unhandled error in Deferred:
2007-09-25 18:38:39-0700 [-] Unhandled Error
        Traceback (most recent call last):
          File "C:\Projects\PyServer\twisted\internet\defer.py", line 317,
in _runCallbacks

          File "C:\Projects\PyServer\twisted\web2\server.py", line 476, in

          File "C:\Projects\PyServer\twisted\internet\defer.py", line 200,
in addErrback

          File "C:\Projects\PyServer\twisted\internet\defer.py", line 182,
in addCallbacks

        --- <exception caught here> ---
          File "C:\Projects\PyServer\twisted\internet\defer.py", line 317,
in _runCallbacks

          File "C:\Projects\PyServer\twisted\web2\server.py", line 492, in

          File "C:\Python24\lib\site-packages\twisted\web2\http.py", line
446, in writeResponse
            self.chanRequest.writeHeaders(response.code, response.headers)
          File "C:\Projects\PyServer\twisted\web2\channel\http.py", line
431, in writeHeaders

          File "C:\Projects\PyServer\twisted\web2\channel\http.py", line
466, in _writeHeaders

        exceptions.AttributeError: 'HTTPChannelRequest' object has no
attribute 'transport'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20070925/0424e51c/attachment.htm 

More information about the Twisted-Python mailing list