I am trying to learn & understand how twisted works, for this I created a basic echo server, the one treats the input as the key for data stored on a redis server, if the input matches a key on the DB, it will print out the value, otherwise print 'key not found'.
I am avoiding the @defer.inlineCallbacks
just to practice more how to work with Deferreds, the code is:
from twisted.internet import reactor, protocol
from txredis.client import RedisClient
REDIS_HOST = 'localhost'
REDIS_PORT = 6379
class Echo(protocol.Protocol):
def connectionMade(self):
self.factory.count += 1
self.transport.write('There are %d clients\n' % self.factory.count)
def dataReceived(self, data):
clientCreator = protocol.ClientCreator(reactor, RedisClient)
d = clientCreator.connectTCP(REDIS_HOST, REDIS_PORT)
def cb_redis_get(r, data):
d = r.get(data.strip())
def cb_result(x):
if x:
self.transport.write('%s\n' % x)
else:
self.transport.write('key not found\n')
d.addBoth(cb_result)
d.addCallback(cb_redis_get, data)
class EchoFactory(protocol.ServerFactory):
count = 0
protocol = Echo
def main():
reactor.listenTCP(1234, EchoFactory())
reactor.run()
if __name__ == "__main__":
main()
When a client connects telnet 0 1234
and enters a word, a connection to the redis server is made, with this approach, if I have 100 concurrent clients, the code will create 100 connections to the redis or memcache server.
This could be the expected behaviour but I would like to know, if could be posible to take advantage of the twisted reactor and when starting the server, create a persistent connection and use it for all the new connections, so that I could just have a single connection per instance and re-use it
If possible, any idea of how to properly implement it and also how to deal with reconnections?