[Twisted-Python] Why do I get an AlreadyCalledError?

Conrad Winchester conradwinchester at me.com
Sat Feb 28 06:21:06 MST 2009


Hi all,

I  am writing a restful twisted (8.1.0) server and am trying to return  
useful error statuses from requests. The problem is that even  
following the documentation extremely carefully I am getting an  
AlreadyCalled Error every time I generate a failure. Here is my main  
class (leaving out code that I feel is not relevant to the question:

from xml.dom import minidom
from twisted.web import server, resource

from FWGException import FWGException

class DeferredPostHandler(resource.Resource,minidom.Document):

     NOT_IMPLEMENTED = 501
     BAD_REQUEST = 400
	
     def writeError(self, failure, request, *args, **kw):
         failure.trap(FWGException)
         request.setResponseCode(self.errorCode)
         request.write("Error handling request: %s" %  
failure.getErrorMessage())
         request.finish()

     def writeResult(self, result, request, *args, **kw):
         self.writexml(request)
         request.finish()

     def render_GET(self, request):
         self.process=self.db.runInteraction(self.processForm,request)
         self.process.addCallbacks(self.writeResult, self.writeError,  
[request], {}, [request], {})
         return server.NOT_DONE_YET


I have a handler that extends this

from DeferredPostHandler import DeferredPostHandler
from twisted.web import http
from twisted.python.failure import Failure
from FWGException import FWGException

class CollectionAdminHandler(DeferredPostHandler):

     def __init__(self, task, db, config):
         DeferredPostHandler.__init__(self,db)
         self.config = config
         self.task = task
         self.errorCode = http.OK

     def processForm(self, cursor, request):

         if self.task == 'add':
             self.addCollection(cursor, request)

         elif self.task == 'delete':
             self.deleteCollection(cursor, request)

         else:
             self.errorCode = DeferredPostHandler.NOT_IMPLEMENTED
             fail = Failure(FWGException("Unsupported Task"))
             self.process.errback(fail)

     def addCollection(self, cursor, request):
         fail = None
         if self.isValidAddRequest(request):
             pass
         else:
             self.errorCode = DeferredPostHandler.BAD_REQUEST
             fail = Failure(FWGException("Missing Parameter"))

         if fail:
             self.process.errback(fail);

     def deleteCollection(self, cursor, request):
         print 'Delete Collection'

     def isValidAddRequest(self, request):
         return 'title' in request.args and 'category' in request.args  
and 'date' in request.args


Unfortunately whenever I call


self.process.errback(fail)

I get the following stack trace in my log


Traceback (most recent call last):
   File "startserver.py", line 23, in <module>
     reactor.run()
   File "/Library/Python/2.5/site-packages/Twisted-8.1.0-py2.5- 
macosx-10.5-i386.egg/twisted/internet/base.py", line 1048, in run
     self.mainLoop()
   File "/Library/Python/2.5/site-packages/Twisted-8.1.0-py2.5- 
macosx-10.5-i386.egg/twisted/internet/base.py", line 1057, in mainLoop
     self.runUntilCurrent()
--- <exception caught here> ---
   File "/Library/Python/2.5/site-packages/Twisted-8.1.0-py2.5- 
macosx-10.5-i386.egg/twisted/internet/base.py", line 677, in  
runUntilCurrent
     f(*a, **kw)
   File "/Library/Python/2.5/site-packages/Twisted-8.1.0-py2.5- 
macosx-10.5-i386.egg/twisted/internet/defer.py", line 243, in callback
     self._startRunCallbacks(result)
   File "/Library/Python/2.5/site-packages/Twisted-8.1.0-py2.5- 
macosx-10.5-i386.egg/twisted/internet/defer.py", line 298, in  
_startRunCallbacks
     raise AlreadyCalledError
twisted.internet.defer.AlreadyCalledError:


I can not understand why this is happening :-( Can anybody help?

BTW I am using POST instead of PUT because my client can't do PUT

Conrad Winchester



-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20090228/31fabaf3/attachment.html>


More information about the Twisted-Python mailing list