[Twisted-Python] Is this possible to catch this error? (returned by protocols/memcached.py)

Attila Nagy bra at fsn.hu
Fri Jun 3 08:54:14 EDT 2011


Hi,

Here's a sample application, which gets a key from memcached:
from twisted.internet import reactor, protocol, defer
from twisted.application import internet, service
from twisted.protocols.memcache import MemCacheProtocol, DEFAULT_PORT

mc = None

class MemCacheFactory(protocol.ReconnectingClientFactory):
     protocol = MemCacheProtocol
     maxDelay = 5 # wait no more than 5 seconds to retry

     def buildProtocol(self, addr):
         p = self.protocol(self)
         p.factory = self
         p.persistentTimeOut = p.timeOut = 5
         global mc
         mc = p
         return p

def memc():
     factory = MemCacheFactory()
     reactor.connectTCP('localhost', 11211, factory)
     reactor.callLater(1,query)

@defer.inlineCallbacks
def query():
     res = yield mc.get('test')
     print "XXX",res

application = service.Application('memc')
serviceCollection = service.IServiceCollection(application)
reactor.callLater(0,memc)

If memcached is running on localhost:11211, it writes the "test" key's 
value:
$ twistd -noy memct.py
2011-06-03 14:48:27+0200 [-] Log opened.
2011-06-03 14:48:27+0200 [-] twistd 10.1.0 (/usr/local/bin/python2.7 
2.7.1) starting up.
2011-06-03 14:48:27+0200 [-] reactor class: 
twisted.internet.selectreactor.SelectReactor.
2011-06-03 14:48:27+0200 [-] Starting factory 
<__builtin__.MemCacheFactory instance at 0x802a38710>
2011-06-03 14:48:28+0200 [MemCacheProtocol,client] XXX (0, None)

Now I want to test the code for timeouts, where the TCP connection to 
memcached is open, but there is no answer.
So instead of memcached, I start a netcat on port 11211:
$ nc -l 11211
And start the above program. Now I get:
$ twistd -noy memct.py
2011-06-03 14:50:08+0200 [-] Log opened.
2011-06-03 14:50:08+0200 [-] twistd 10.1.0 (/usr/local/bin/python2.7 
2.7.1) starting up.
2011-06-03 14:50:08+0200 [-] reactor class: 
twisted.internet.selectreactor.SelectReactor.
2011-06-03 14:50:08+0200 [-] Starting factory 
<__builtin__.MemCacheFactory instance at 0x802a38710>
2011-06-03 14:50:14+0200 [-] Unhandled error in Deferred:
2011-06-03 14:50:14+0200 [-] Unhandled Error
         Traceback (most recent call last):
         Failure: twisted.internet.defer.TimeoutError: Connection timeout

2011-06-03 14:50:14+0200 [MemCacheProtocol,client] 
<twisted.internet.tcp.Connector instance at 0x804027dd0> will retry in 2 
seconds
2011-06-03 14:50:14+0200 [MemCacheProtocol,client] Stopping factory 
<__builtin__.MemCacheFactory instance at 0x802a38710>

As you can see, the connection is timed out, and protocols/memcached.py 
lost its connection. But how could I catch this around the "mc.get" call?
I guess this error should raise an exception, so enclosing mc.get into a 
try-except would make possible to catch this. But currently this is not 
the case?

How can this be solved?

Thanks,



More information about the Twisted-Python mailing list