【发布时间】:2018-06-20 08:53:10
【问题描述】:
我尝试使用 nameko rpc 服务器从 mysql 读取数据。这是服务器代码。
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)
这是客户端代码:
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)
我的问题是每次我调用 rpc 服务时,它都会建立一个新的连接。我有时会收到 mysql 错误(99)“无法连接 mysql 服务器”。我可以只使用一个连接吗?
【问题讨论】:
-
您没有包含异常消息
-
@DashWinterson,对不起,我不能不上传照片。异常消息是“OperationalError(2003, “Can't connect to MySQL server on 'mysql.server' (99)”
标签: python mysql python-3.x nameko