Search code examples
pythonmysqlpython-3.xnameko

python3 nameko RPC server with mysql connection


I try to use a nameko rpc server to read data from mysql. here is the server code.

class SendService:
    name = 'url_feature_rpc_service'
    def __init__(self):
        print('new connection')
        self.db = MySQLdb.connect(host="localhost", user="user", passwd="123456", db="today_news", charset='utf8')
        self.cursor = self.db.cursor()

    @rpc
    def get_feature(self, url):
        sql = 'select title_seg, entity_seg, title_entity_vec from article_feature where url_md5 = md5(\'{}\')'.format(url)
        self.cursor.execute(sql)
        result = self.cursor.fetchone()
        if result == None:
            return ''
        return '\t'.join(result)

and here is the client code:

with ClusterRpcProxy(config) as cluster_rpc:
    for line in sys.stdin:
        line = line.strip()
        try:
            result = cluster_rpc.url_feature_rpc_service.get_feature(line)
        except Exception as e:
            print(e)

my question is every time I call the rpc service, it will set up a new connection. and I sometimes get the mysql error(99)"cannot connect mysql server". can I use only one connection?


Solution

  • You should be using a DependencyProvider such as nameko-sqlalchemy to be connecting to a database.

    Instantiating the MySQL connection inside __init__ means you'll create a new connection every time the RPC method fires, probably means you're running out of connections.