gpt4 book ai didi

带有mysql连接的python3 nameko RPC服务器

转载 作者:行者123 更新时间:2023-11-29 07:37:14 25 4
gpt4 key购买 nike

我尝试使用 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 服务器”。我可以只使用一个连接吗?

最佳答案

您应该使用 DependencyProvider,例如 nameko-sqlalchemy连接到数据库。

__init__ 中实例化 MySQL 连接意味着您将在每次 RPC 方法触发时创建一个新连接,这可能意味着您将耗尽连接。

关于带有mysql连接的python3 nameko RPC服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com