Python implementation Redis client singleton +hbase Client single case

Source: Internet
Author: User



When the business needs a large number of connections to Redis or hbase, a large number of connections will cause a large number of sockets, the result is that the server does not have more ports to allocate, in this case the best solution is to implement the client connection of a singleton mode, keep the connection is always the same. Speaking of which, it may not have been experienced, if you insert 4,000 data per second, this phenomenon is very obvious. The following implementation of the Python implementation of the Redis+hbase singleton mode, there are many improvements, according to their own business to adjust, can be verified by the ID of the print instance:


import happybase
import redis
class _RedisMgrSingleton (type):
    ‘‘ ‘Redis singleton’ ’‘ ‘‘
    def __init __ (self, name, bases, dict):
        super (_RedisMgrSingleton, self) .__ init __ (name, bases, dict)
        self._instance = {}
    def __call __ (self, host, port, db):
        if not self._instance.has_key ((host, port, db)):
            self._instance [(host, port, db)] = super (_RedisMgrSingleton, self) .__ call __ (host, port, db)
        return self._instance [(host, port, db)]

class HbaseSingleton (type):
    Single case of ‘‘ ‘hbase’ ‘‘ ‘
    def __init __ (self, name, bases, dict):
        super (HbaseSingleton, self) .__ init __ (name, bases, dict)
        self._instance = {}
    def __call __ (self, host, table):
        if not self._instance.has_key ((host, table)):
            self._instance [(host, table)] = super (HbaseSingleton, self) .__ call __ (host, table)
        return self._instance [(host, table)]

class RedisMgr:
    "Redis Operation Special Class"
    def __init __ (self, host, port, db, max_connections = 3):
        "eg: host‘ 192.168.2.184 ’port 6379 db 14"
        self.host = host
        self.port = port
        self.db = db
        self.conn = redis.Redis (connection_pool = redis.ConnectionPool (host = host, port = port, db = db, max_connections = max_connections))
    def run_redis_fun (self, funname, * args):
        fun = getattr (self.conn, funname)
        print args
        return fun (* args)
    def pipe (self):
        return self.conn.pipeline (transaction = False)
    __metaclass__ = _RedisMgrSingleton #metaclass implementation singleton



class HbaseOperate (object):
    def __init __ (self, host, table):
        self.host = host
        self.table = table
        self.conn = happybase.Connection (self.host)
        self.table = self.conn.table (self.table)

    def run (self, fun, * args):
        # result = self.table.row (* args)
        funname = getattr (self.table, fun)
        return funname (* args)
    def cells (self, column, info, version):
        return self.table.cells (column, info, versions = version)
    __metaclass__ = HbaseSingleton #metaclass implementation singleton



conn = HbaseOperate (‘xxx.xx.11.8‘, "history_visitor_product")
result = conn.cells (‘chenhuachao‘, ‘info: visiotr’, 3)
print result
print "First time", id (conn)
conn1 = HbaseOperate (‘xxx.xxx.11.8‘, "history_visitor_product")
result1 = conn1.cells (‘chenhuachao‘, ‘info: visiotr’, 6)
print result1
print "Second", id (conn1)
#output
[‘Test10’, ‘test9’, ‘test97’]
First time 38014896
[‘Test10’, ‘test9’, ‘test97’, ‘test17’, ‘test1345 \ n’]
The second 38014896 





This article is from the "people on the Run" blog, please be sure to keep this source http://leizhu.blog.51cto.com/3758740/1812659



Python implementation Redis client singleton +hbase Client single case


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.