[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
consumer.
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!
~P
from twisted.internet import reactor, defer
from twisted.web2 import http, responsecode
import Queue
TIMEOUT = 10
def respond(result, httprequest):
httpresponse = http.Response(responsecode.OK, {}, stream=result)
httprequest.writeResponse(httpresponse)
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):
self.get_queue(xSig).put(value)
def get_queue(self, xSig):
try:
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!"
%(xSig))
def timeout(deferred, request):
xSig = request.args['X-Signature'][0]
try:
self.defer.pop(xSig)
except KeyError:
print 'Timeout called and xSig `%s` was not in cache!' %
xSig
respond('ACK',request)
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']
try:
d = self.defer.pop(xSig)
d.callback(data['message'])
except KeyError:
self.push_data(xSig,data)
def maybedefer(self, httprequest):
xSig = httprequest.args['X-Signature'][0]
try:
data = self.get_data(xSig)
result = http.Response(
responsecode.OK,
{},
stream=data['message']
)
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
_cbFinishRender
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
_processingFailed
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
_processingReallyFailed
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