python - how to make a persistent connection to redis or memcache within a twisted server -
I'm trying to learn & amp; Understand how it works, I created a basic eco server, treats an input as the key for the data stored on the server, if The value will match the key 'key is not found'
i when a client This may be expected behavior but I would like to know, if it can be appropriate to take advantage of twisted reactor and when the server starts, make a constant connection and Use it for all new connections, so that I have a connection to each connection. CA and reuse it If possible, how to implement it properly and how to handle redirection? @defer To work more with the .inline callback
, the code is: twisted.internet "lang-py prettyprint-override">
Proto from the import reactor Call txredis.client import RedisClient REDIS_HOST = 'localhost' REDIS_PORT = 6379 square Echo (protocol protocol): Def connection created (self): self.factory.count + = 1 self.transport.write ('% d are clients' N '% self.factory.count) def data (self, data): clientCreator = protocol.ClientCreator (Reactor, RedisClient) D = clientCreator.connectTCP (REDIS_HOST, REDIS_PORT) def cb_redis_get (R, data): D = R J. (Data.stip ()) Diff Cb_result (x): If x: self.transport.write ('% s \ n'% x) Other: self.transport.write ('key not found \ n') d.addBoth (Cb_result) d.addClallback (cb_redis_get), data) square EchoFactory (protocol.ServerFactory): count = 0 protocol = echo def main (): reactor. LISTIN TCP (1234, EchoFactor ()) reactor. () If __name__ == "__main__": main ()
telnet 0 1234
Connects and enters one word, with this approach, the connection to the redis server is created, if I have 100 concurrent clients, the code will create 100 connections for the Redis or memcache server.
Take a look at the library.
I've been using for a twisted project for a few months and I'm quite happy
Wait, okay, maybe whatever I ask I am trying, I am missing.
When I look at your code, I get some strange things, as if it looks like you to make a connection to retrieve the data obtained from your Telnet, which is per telnet Will not be able to make the connection, which will make every data-flush connection on that Telnet connection (this is almost certainly not what you want to do).
See that they are more detailed, for example I'm seeing txredis
What is the need to speak roughly Your argument should be divided into two, meeting one part The Radis Connection (Live) and the second part (which is basically now with you) should be processed on Telnet connections and the command should be pushed on the radis connection (connections). Txredisapi lib will work for you all redis (... and may also be txRedis, I know that well know that lib is not enough, txredisapi example should help you in any way).
(In the last few days this style of question has arrived, you can find help in the previous answer to understand a common separation between the parts located in twisted :)
Comments
Post a Comment