{"id":22154594,"url":"https://github.com/ericflo/txconnpool","last_synced_at":"2025-10-19T07:13:02.043Z","repository":{"id":818953,"uuid":"530798","full_name":"ericflo/txconnpool","owner":"ericflo","description":"A generalized connection pooling library for Twisted.","archived":false,"fork":false,"pushed_at":"2018-08-07T00:34:46.000Z","size":122,"stargazers_count":34,"open_issues_count":2,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-03T08:37:26.655Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ericflo.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-02-22T20:18:50.000Z","updated_at":"2025-01-20T18:25:00.000Z","dependencies_parsed_at":"2022-08-16T11:00:41.412Z","dependency_job_id":null,"html_url":"https://github.com/ericflo/txconnpool","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ericflo/txconnpool","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericflo%2Ftxconnpool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericflo%2Ftxconnpool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericflo%2Ftxconnpool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericflo%2Ftxconnpool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ericflo","download_url":"https://codeload.github.com/ericflo/txconnpool/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericflo%2Ftxconnpool/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267130108,"owners_count":24040398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-02T01:49:46.273Z","updated_at":"2025-10-19T07:13:01.958Z","avatar_url":"https://github.com/ericflo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"txconnpool\n==========\n\nA generalized connection pooling library for Twisted.\n\n\nExample Description\n-------------------\n\nAssume that we've got a web application, which performs some expensive \ncomputations, and then caches them in a memcached_ server.  The simple way to\nachieve this in Twisted is to create a ClientCreator_ for the MemCacheProtocol_\nand whenever we need to communicate with the server, we can simply use that.\n\nThis works for low volumes of queries, but let's say that now we start hitting \nmemcached a lot--several times per web request, of which we are receiving many\nper second.  Very quickly, the connection overhead can become a problem.\n\nInstead of creating a new connection for every query, it would be much better \nto maintain a pool of open connections, and simply reuse those open \nconnections; queuing up any queries if all of the connections are in use.  With\ntxconnpool, setting this up can be quite easy.\n\n\nExample Implementation\n----------------------\n\nFirst we need to create a few classes of boilerplate, to transform a\nMemCacheProtocol_ into a PooledMemcachedProtocol, and then create a pool::\n\n    from twisted.protocols.memcache import MemCacheProtocol\n\n    from txconnpool.pool import PooledClientFactory, Pool\n\n    class PooledMemCacheProtocol(MemCacheProtocol):\n        \"\"\"\n        A MemCacheProtocol that will notify a connectionPool that it is ready\n        to accept requests.\n        \"\"\"\n        factory = None\n    \n        def connectionMade(self):\n            \"\"\"\n            Notify our factory that we're ready to accept connections.\n            \"\"\"\n            MemCacheProtocol.connectionMade(self)\n\n            self.factory.connectionPool.clientFree(self)\n\n            if self.factory.deferred is not None:\n                self.factory.deferred.callback(self)\n                self.factory.deferred = None\n\n    class MemCacheClientFactory(PooledClientFactory):\n        protocol = PooledMemCacheProtocol\n\n    class MemCachePool(Pool):\n        clientFactory = MemCacheClientFactory\n    \n        def get(self, *args, **kwargs):\n            return self.performRequest('get', *args, **kwargs)\n\n        def set(self, *args, **kwargs):\n            return self.performRequest('set', *args, **kwargs)\n\n        def delete(self, *args, **kwargs):\n            return self.performRequest('delete', *args, **kwargs)\n\n        def add(self, *args, **kwargs):\n            return self.performRequest('add', *args, **kwargs)\n\n\nNow, with this having been created, we can go ahead and use it::\n\n    from twisted.internet.address import IPv4Address\n    \n    addr = IPv4Address('TCP', '127.0.0.1', 11211)\n    mc_pool = MemCachePool(addr, maxClients=20)\n    \n    d = mc_pool.get('cached-data')\n    \n    def gotCachedData(data):\n        flags, value = data\n        if value:\n            print 'Yay, we got a cache hit'\n        else:\n            print 'Boo, it was a cache miss'\n    \n    d.addCallback(gotCachedData)\n\n\n.. _memcached: http://memcached.org/\n.. _ClientCreator: http://twistedmatrix.com/documents/current/api/twisted.internet.protocol.ClientCreator.html\n.. _MemCacheProtocol: http://twistedmatrix.com/documents/current/api/twisted.protocols.memcache.MemCacheProtocol.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericflo%2Ftxconnpool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fericflo%2Ftxconnpool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericflo%2Ftxconnpool/lists"}